• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design/Analysis Algorithm

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Convert mystery_recursive into a linear-time dynamic programming algorithm mystery_iterative using a loop (rather than memoized recursion) in Python. (This code is not solving any particularly interesting problem; it’s just an example.)

def mystery_recursive(x): if len(x) == 0: return 1
if len(x) == 1: return x[0]
return max(mystery_recursive(x[:-1]) + len(x), mystery_recursive(x[:-2]) + x[-1] * x[-2])
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall copy this post into another forum so as to give you more exposure.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your question? Can't you create a dictionary((0,1), (1, x[0]), and calculate the function values for n = 2 to whatever, using the dictionary and the given recursion , updating the dictionary as you go?
 
Davin Thomas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of the help Marshall Ritchie but, problem solved. Eh.
 
reply
    Bookmark Topic Watch Topic
  • New Topic