This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Design/Analysis Algorithm  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall copy this post into another forum so as to give you more exposure.
 
Saloon Keeper
Posts: 3255
128
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for all of the help Marshall Ritchie but, problem solved. Eh.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!