Skip to content

Instantly share code, notes, and snippets.

@joerick
Created May 8, 2015 19:50
Show Gist options
  • Save joerick/39b7a030ec6771ce4d2d to your computer and use it in GitHub Desktop.
Save joerick/39b7a030ec6771ce4d2d to your computer and use it in GitHub Desktop.
test_list = [1,2,5,2]
def for_loop(the_list):
result = 0
for number in the_list:
result += number
return result
def while_loop(the_list):
result = 0
i = 0
while i < len(the_list):
result += the_list[i]
i += 1
return result
def recursion(the_list):
try:
head = the_list[0]
except IndexError:
return 0
tail = the_list[1:]
return head + recursion(tail)
print sum(test_list)
print for_loop(test_list)
print while_loop(test_list)
print recursion(test_list)
def my_zip(list_1, list_2):
i = 0
result = []
while i < len(list_1) and i < len(list_2):
result.append(list_1[i])
result.append(list_2[i])
i += 1
return result
print my_zip([1,2,3], ['a','b','c'])
def fib():
sequence = [0, 1]
while len(sequence) < 100:
sequence.append(sequence[-1] + sequence[-2])
return sequence
import pprint
pprint.pprint(fib())
import itertools
def max_combination(a_list):
str_list = [str(el) for el in a_list]
result = 0
for permutation in itertools.permutations(str_list):
str_number = ''.join(permutation)
number = int(str_number)
if result < number:
result = number
return result
print max_combination([50, 2, 1, 9, 98])
operators = ['+', '-', '']
for op1 in operators:
for op2 in operators:
for op3 in operators:
for op4 in operators:
for op5 in operators:
for op6 in operators:
for op7 in operators:
for op8 in operators:
expression = (
'1' + op1 +
'2' + op2 +
'3' + op3 +
'4' + op4 +
'5' + op5 +
'6' + op6 +
'7' + op7 +
'8' + op8 +
'9')
result = eval(expression)
if result == 100:
print expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment