Question

Suppose I will go on a n miles road trip, and I could choose to go...

Suppose I will go on a n miles road trip, and I could choose to go 1, 3 or 5 miles in each stage. Thus, my stages will look like a sequence such as 1, 3, 3, 5, 3, 1, 1,....., which add up to n. Give a dynamic programming algorithm that calculates how many different ways are there of doing this trip. Note that, you don't need to output sequences of each trip, just the number of different ways.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Function to calculate number of ways for walking 'n' miles with 1,3 or 5 steps
def calculate_path(n) :
   paths = [1] * (n+1)    #Initializing an array of size n+1 with 1. paths[i] will give number of ways to walk "i" miles.
   paths[0]=paths[1]=paths[2]=1   #Base cases : For 0/1/2 miles number of ways is only 1
   paths[3] = 2   #Base case : For 3 miles, number of ways is 2 (111,3)
   paths[4] = 3   #Base case : For 4 miles, number of ways is 3 (1111,13,31)
   for i in range(5,n+1) :  
       #Since the last stage for any mile will be either 1 step or 3 steps or 5 steps
       #So, we can calculate total steps by adding number of ways for (n-1)miles , (n-3)miles and (n-5)miles.
       paths[i] = paths[i-1] + paths[i-3] + paths[i-5]  
   return paths[n]       #Final number of ways will be stored at paths[n]
      


Sample Input 1 : 6
Sample Output 1 : 8  
Sample Input 2 : 10
Sample Output 2 : 47

Add a comment
Know the answer?
Add Answer to:
Suppose I will go on a n miles road trip, and I could choose to go...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5) There is an s miles long road, with n possible locations for advertisements at distances...

    5) There is an s miles long road, with n possible locations for advertisements at distances di,... , dn from the beginning of the road. Putting an advertise- ment at the i'th location brings in revenue pi. Two advertisements cannot be located closer than distance from each other. Find a set of locations for the advertisement bringing in maximal revenue. a) Design a dynamic programming algorithm for this problem b) Use your algorithm to find the optimal solution for distances...

  • There are n trading posts numbered 1 to n as you travel downstream. At any trading...

    There are n trading posts numbered 1 to n as you travel downstream. At any trading post i you can rent a canoe to be returned at any of the downstream trading posts j>i. You are given a cost array R(i,j) giving the cost of these rentals for all 1≤i<j≤n. We can assume that R(i,i)=0, and that you can't go upriver (so perhaps R(i,j)= ∞ if i>j). For example, one cost array with n=4 might be the following. The problem...

  • Exercise 12.6 At each stage, one can either pay 1 and receive a coupon that is equally likely to be any of n types, or one can stop and receive a final reward of jr if one's current collection of...

    Exercise 12.6 At each stage, one can either pay 1 and receive a coupon that is equally likely to be any of n types, or one can stop and receive a final reward of jr if one's current collection of coupons contains exactly j distinct types. Thus, for instance, if one stops after having previously obtained six coupons whose successive types were 2, 4, 2, 5, 4, 3, then one would have earned a net return of 4r -6. The...

  • Suppose n activities apply for using a common resource. Activity ai (1 ≤ i ≤ n)...

    Suppose n activities apply for using a common resource. Activity ai (1 ≤ i ≤ n) has a starting time S[i] and a finish time F[i] such that 0 < S[i] < F[i]. Two activities ai and aj (1 ≤ i, j ≤ n) are compatible if intervals [S[i], F[i]) and [S[j], F[j]) do not overlap. We assume the activities have been sorted such that S [1] ≤ S [2] ≤ …≤ S[n]. (a) Design an O(n2) dynamic programming algorithm...

  • We are given n,x1,x2,...,xn,d1,d2,...,dn,D. The graph is not given and it should be constructed. The time...

    We are given n,x1,x2,...,xn,d1,d2,...,dn,D. The graph is not given and it should be constructed. The time it takes to construct a graph is part of the overall time complexity, so it should be included. The solution is your algorithm, which includes the graph construction. It is fine if the algorithm consists of several parts, which perform different tasks. The algorithm should return the actual path.The proof and run-time analysis should be provided for the entire solution/algorithm. Please show your wrok....

  • I need this using C++. In this project, you will implement the dynamic programming-based solution to...

    I need this using C++. In this project, you will implement the dynamic programming-based solution to find the longest common subsequence (LCS) of two sequences. Your inputs will be the two sequences (as Strings) and the outputs are the longest common subsequence (printed as a String) and the final matrix (printed as a two-dimensional array) depicting the length of the longest common subsequences (as shown in the slides) for all possible subsequences of the two input sequences. The two input...

  • In terms of the programming language required, the algorithm needs to be written in pseudocode Dynamic Programming Consider the following problem based on the transformation of a sequence (or collect...

    In terms of the programming language required, the algorithm needs to be written in pseudocode Dynamic Programming Consider the following problem based on the transformation of a sequence (or collection) of coloured disks Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, c. For example, the colour mapping might be 0-red. 1-yellow, 2-blue, 3-pink. , c-black For a given sequence of coloured disks e.g., ( 0,1,2,3,4...

  • In terms of the programming language required, the algorithm needs to be written in pseudocode Dynamic Programming Cons...

    In terms of the programming language required, the algorithm needs to be written in pseudocode Dynamic Programming Consider the following problem based on the transformation of a sequence (or collection) of coloured disks Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, c. For example, the colour mapping might be 0-red. 1-yellow, 2-blue, 3-pink. , c-black For a given sequence of coloured disks e.g., ( 0,1,2,3,4...

  • i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transf...

    i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transformation of a sequence (or collection) of coloured disks. Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, cl. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink,. c-black For a given sequence of coloured disks eg.,0,1,2,3,4), at each time step (or iteration) you are only...

  • We are given a color picture consisting of an m?n array AŒ1::m;1::n? of pixels, where each...

    We are given a color picture consisting of an m?n array AŒ1::m;1::n? of pixels, where each pixel specifies a triple of red, green, and blue (RGB) intensities. Sup- pose that we wish to compress this picture slightly. Specifically, we wish to remove one pixel from each of the m rows, so that the whole picture becomes one pixel narrower. To avoid disturbing visual effects, however, we require that the pixels removed in two adjacent rows be in the same or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT