Question: [Python Question] Please find the number of ways to have n (where n1) cookies in the basket Assume that each time you put either one
- [Python Question]
- 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
=====================================================================
# 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
"""
=====================================================================
# 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
Get step-by-step solutions from verified subject matter experts
