Recursion vs Nested Loops – Fast vs Memory Efficient

by Security Dude

I have been working on extending the Python Study Groups “reduce lambda map lambda” brain teaser. My first reaction is to brute force the algorithm to giving me the secrets in spelling “HACKER SCHOOL”. So far there are many combinations in spelling each word. I have used a nested loop to generate each of the numbers and lazily used the try/except to handle errors. I have read some of the comments regarding the pros and cons of recursion vs nested loops. Here’s my code so far:

import string
def transpose_numbers_letters(list_of_items): 
  out = [ 1 - reduce(lambda x, y: x+y, magic[1:])]
  map (lambda x: out.append(out[-1]+x), magic)
  x = ''.join(map(lambda x: string.uppercase[x], out))
  return x
def bruteforce_numbers_to_words():
  for a in range(-25, 26):
    for b in range(-25, 26):
      for c in range(-25, 26):
        for d in range(-25, 26):
          for e in range(-25, 26):
            magic = [a,b,c,d,e]
            try:
              result = transpose_numbers_letters(magic)
              if result == 'SCHOOL':
                return magic
            except:
                pass
 
print bruteforce_numbers_to_words()

 

Advertisements