
r/programminghorror

▲ 54 r/programminghorror
i sure do love not using local variables!
getfenv getfenv getfenv getfenv!
u/DroneCyn — 4 days ago
▲ 63 r/programminghorror
Rate my python implementation of "hello world" part 2
import os, random
def greet():
with open(os.getcwd() + "/solution.py", "r", encoding="UTF-8") as f:
lines = f.readlines()
req = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
temp = []
chars = [char for line in lines for char in line]
while temp != req:
random_index = random.randint(0, len(chars) - 1)
if req[len(temp)] == chars[random_index]:
temp.append(chars[random_index])
chars.pop(random_index)
return "".join(temp)
u/ferro31_ — 14 days ago
▲ 13 r/programminghorror
Rate my python "Hello, World!" implementation
# Write a function `greet` that returns "hello world!"
import os
def check_if_contains(l1, l2):
contains_all = True
for ind in range(len(l2)):
if l1[ind] != l2[ind]:
contains_all = False
return contains_all
def get_length(l, substract):
i = 0
for _ in range(len(l)):
i += 1
if substract:
return i - 1
if not substract:
return i
def append_item(l, c):
l.append(None)
length = get_length(l, True)
l[length] = c
def build_string(l):
string_builder = ""
for ind in range(len(l)):
string_builder += l[ind]
return string_builder
def greet():
with open(os.getcwd() + "/solution.py", "r", encoding="UTF-8") as f:
lines = f.readlines()
req = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
temp = []
for line in lines:
for char in line:
append_item(temp, char)
if check_if_contains(req, temp):
if get_length(req, False) == get_length(temp, False):
return build_string(temp)
else:
temp = []
u/ferro31_ — 14 days ago