Skip to content

Instantly share code, notes, and snippets.

@Helloman892
Created January 21, 2018 20:46
Show Gist options
  • Save Helloman892/b21ef8e4ec6b515a2981a764d194f2d7 to your computer and use it in GitHub Desktop.
Save Helloman892/b21ef8e4ec6b515a2981a764d194f2d7 to your computer and use it in GitHub Desktop.
basically i'm better or something idk
from random import randint, shuffle, choice
from timeit import Timer
def f(x):
y = list(range(1, x + 1)) # creates list
def inner(lst):
# the condition below checks if the parities of the list are all 0 or 1
# and continues if they aren't
while(sum([i % 2 for i in lst]) not in (0, len(lst))):
shuffle(lst)
tmp = lst[:len(lst) // 2] # cuts list in half after the shuffle - eliminates one for loop
del lst[len(lst) // 2:]
for i, j in zip(tmp, lst):
z = randint(0, 2 * x) # ensures that the value is truly random within the bounds
k = z // 2
if (i + j) % 2: # if same parity, (i + j) % 2 == 0, else 1
lst.append(k + (not z % 2))
else:
lst.append(k + z % 2)
lst += tmp
del lst[:len(lst) // 3]
return lst
return "input is not even" if x % 2 else inner(y)
def lyric(x):
def is_even(num):
return num % 2 == 0
input_num = x
try:
input_num = int(input_num)
except ValueError:
print("Invalid input.")
exit(0)
if not is_even(input_num):
print("Number is not even.")
exit(0)
else:
pool = list(range(1, input_num + 1))
# print(pool)
while True:
current_pool = pool
shuffle(current_pool)
groups = [current_pool[x:x + 2] for x in range(0, len(current_pool), 2)]
for group in groups:
parities = [is_even(x) for x in group]
if len(set(parities)) == 1:
pool.append(choice(list(range(2, input_num + 1, 2))))
else:
pool.append(choice(list(range(1, input_num + 1, 2))))
shuffle(pool)
pool = pool[0:int(len(pool) * 0.6) + 1]
# print(pool)
if len(set([is_even(x) for x in pool])) == 1:
break
return pool
x = 10
print("hm's:")
for i in range(2, x + 1, 2):
timer = Timer(stmt=f"print('{i}: {len(f(i)) == i}', end=' ')")
print(str(timer.timeit(number=1) * 1e6)[:6])
print("lyric's:")
for i in range(2, x + 1, 2):
timer = Timer(stmt=f"print('{i}: {len(lyric(i)) == i}', end=' ')")
print(str(timer.timeit(number=1) * 1e6)[:6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment