Question
[Computer Science / Python Algorithm Question] I need help in Computer Science for Python Algorithm . If you need more information, pleas let me know.
[Computer Science / Python Algorithm Question]
I need help in Computer Science for Python Algorithm. If you need more information, pleas let me know. Thank you for your time.
- Please find the number of ways to have n (where n1) cookies in the basket
- Assume that each time you put either one cookie or two cookies in the basket
- For example, there are 3 ways to have three cookies in the basket
- you can first put in two cookies then one cookie in the basket
- or you first put in one cookie then two cookies in the basket
- or you put in one cookie three times in the basket
- Your solution should have the complexity below,
- O(n) time complexity
- O(1) space complexity
- the logic should summarize the main idea of your algorithm
- the code should
- be bug-free (while the output produced by your solution being the same as the provided output does not necessarily mean your code is bug-free, it is very likely that there is a bug in your code when the two kinds of output are different)
- satisfy the required time and space complexity
=====================================================================
# Implementation
def fun_2(n):
"""
Please find the number of ways to have n (where n >= 1) cookies in the basket
Assume that each time you put either one cookie or two cookies in the basket
Parameters
----------
n: an integer >= 1
Returns
----------
the number of ways: an integer
"""
# Implement
=====================================================================
# Test for i in range(1, 20): print(i, fun_2(i))
1 1
2 2
3 3
4 5
5 8
6 13
7 21
8 34
9 55
10 89
11 144
12 233
13 377
14 610
15 987
16 1597
17 2584
18 4181
19 6765
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started