question for the code that results. You are doing this question to practice the concepts of recursion. (a) The Fibonacci sequence is a well-known sequence of integers that follows a pattern that can be seen to occur in many different areas of nature. The sequence looks like 0,1,1, 2, 3, 5, 8, 13, 21, 34,55,.... That is, the sequence starts with 0 followed by 1, and then every number to follow is the sum of the previous two numbers. The Fibonacci numbers can be expressed as a mathematical function as fol- lows: 0 if n=0 f(n) 1 if n=1 f(n-1) + f(n-2) if n > 1 Translate this function into Python, and test it. The function must be recursive. Your function should only accept a non-negative n integer as input, and return the nth Fibonacci num- ber. No other parameters are allowed. It should not display anything. (b) The Moosonacci sequence is a less well-known sequence of integers that follows a pattern that is rarely seen to occur in nature. The sequence looks like this: 0,1,2,3,6,11,20, 37, 68, 125... That is, the sequence starts with 0 followed by 1, and then 2: then every number to follow is the sum of the previous three numbers. For example: m(3) = 3 = 0+1+2 m(4) = 6=1+2+3 m(5) = 11 = 2+3+6 Write a recursive Python function to calculate the nth number in the Moosonacci sequence. As with the Fibonacci sequence, we'll start the sequence with m(0) = 0. Your function should only accept a non-negative n integer as input, and return the nth Moosonacci number. No other parameters are allowed. It should not display anything.
(c) Design a recursive Python function named substr that takes as input a string s. a target character c, and a replacement character r, that returns a new string with every occurrence of the character t replaced by the character r. For example: >>> substr('l', 'x', 'Hello, world!') 'Hexxo, worxd!' >>> substr('o', 'i', 'Hello, world!') 'Helli, wirld!' >>> substr('z', 'q', 'Hello, world!') Hello, world!' Your function should accept two single character strings and an arbitrary string as input, and return a new string with the substitutions made. It should not display anything. If the target does not appear in the string the returned string is identical to the original string. All of your functions should be well tested. What to Hand In • A Python program named a8q2.py containing your recursive functions. • A Python script named a8q2_testing.py containing your test script. Be sure to include your name, NSID, student number, course number and laboratory section at the top of all documents. Evaluation This is just a warm up, and the functions are very simple. • 2 marks: Fibonacci function. Full marks if it is recursive, zero marks otherwise. • 2 marks: Moosonacci function. Full marks if it is recursive, zero marks otherwise. . 4 marks: substr function. Full marks if it is recursive, and if it works, zero marks otherwise. 3 marks: Your functions are tested and have good coverage.
(a) the delegation metaphor, and (b) the relationship between the work of the delegates. You're not do- ing this (a) the delegation metaphor, and (b) the relationship between the work of the delegates. You're not do- ing this questio
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am