• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Design/Analysis Algorithm

 
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: 65474
249
  • 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: 3462
149
  • 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.
 
This tiny ad is wafer thin:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!