• 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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: 63793
209
  • 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.
 
Rancher
Posts: 3119
110
  • 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.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!