![]() | |
Teachers Assignments Assignments Other Pages | Project 6: Assigned: Fri Sep 27 2024 In this project, you will write some sweet recursive fruitful functions! Do not use any loops to write these functions. Part 0, 1 points: Write the factorial function, as covered in class. Tester. Part 1, 1 points: Write the fibonacci function, as covered in class. Tester. Part 2, 3 points: Write a function, >>> repeat_string("monkey", 3)
'monkeymonkeymonkey'
>>> repeat_string("antelope", 0)
''
>>> repeated = repeat_string("banana", 2)
>>> print(repeated)
bananabanana Python already has an operator that does this automatically for you, but give it a try recursively. If you can't get this one, the rest will be nearly impossible. Tester.Part 3, 2 points: Let's write fruitful versions for some of the void-recursive functions we wrote before. We'll name this one appropriately. Write >>> is_prime_helper(200, 150)
False
>>> is_prime_helper(200, 1)
True Tester.Part 4, 2 points: Now, of course, write the wrapper function: Part 5, 1 points: Create a function, >>> triangular_number(0)
0
>>> triangular_number(1)
1
>>> triangular_number(2)
3
>>> triangular_number(3)
6
>>> triangular_number(4)
10 (I plan to do this problem with everyone in class.) Tester.Part 6, 0 points: Expert Practice: Part 7, 0 points: Expert Practice: Exercise 6.2. If this notation isn't clear to you, just ask me; I'm happy to walk you through it. This one's not actually all that hard once you get past the cases notation, but once you do it, the rest of the project will be extremely easy. Part 8, 2 points: Exercise 6.3. Copy the three given functions in and use them to code up Part 9, 2 points: Exercise 6.4. (You can assume that a and b are both positive integers.) >>> is_power(1, 3)
True
>>> is_power(10, 4)
False
>>> is_power(81, 3)
True Hint: the base case is the same no matter what b is. Tester.Part 10, 2 points: Exercise 6.5. This process is called Euclid's algorithm. Again, please assume that a and b are both integers. Once you have this one written, it will look completely obvious. (Also, why didn't they teach us this in grade school???) >>> gcd(90, 40)
10
>>> x = gcd(40, 90)
>>> print(x)
10 Tester.Submitting your Project: Make sure all your code is in a file labelled with your user name (everything before the @ in your school email address) followed by |