Skip to content

Instantly share code, notes, and snippets.

@textbook
Created November 9, 2012 11:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save textbook/4045328 to your computer and use it in GitHub Desktop.
Save textbook/4045328 to your computer and use it in GitHub Desktop.
MIT 6.00x - MidTerm1 answers by jonrsharpe
# 3. Iterative/recursive log function
def myLog(x, b):
a = 0
while b ** a <= x:
a += 1
return a - 1
# 4. Interlace two strings iteratively
def laceStrings(s1, s2):
shorter, longer = sorted([s1, s2], key=len)
output = []
for i in range(len(shorter)):
output.append(s1[i])
output.append(s2[i])
output += longer[len(shorter):]
return "".join(output)
# 5. Interlace two strings recursively
def laceStringsRecur(s1, s2):
def helpLaceStrings(s1, s2, out):
if s1 == '':
return out + s2
if s2 == '':
return out + s1
else:
return helpLaceStrings(s1[1:], s2[1:], out + s1[0] + s2[0])
return helpLaceStrings(s1, s2, '')
# 7. McNuggets
def McNuggets(n):
if n < 0:
return False
elif n == 0:
return True
else:
return any(McNuggets(n - x) for x in [20, 9, 6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment