<aside> š” Question 1
Discuss Towers of Hanoi puzzle.
Solution: The Towers of Hanoi is a mathematical puzzle.
def toh(n,source,auxiliary,destination):
if n==0:
return
toh(n - 1, source, destination, auxiliary)
print("Move disk %d from peg %d to peg %d" % (n, source, destination))
toh(n - 1, auxiliary, source, destination)
def towerOfHanoi(n):
# Write your code here
# Return a 2-D array
toh(n,1,2,3)

https://whimsical.com/recursion-K1nZYz3VMFpSRcjW22H9uE
</aside>
<aside> š” Question 2
Family Structure
Problem Statement: Aakash is a member of Ninja club. He has a weird family structure. Every male member (M) gives birth to a male child first and then a female child, whereas every female (F) member gives birth to a female child first and then to a male child. Aakash analyses this pattern and wants to know what will be the Kth child in his Nth generation. Can you help him?
Example:
<aside> š” n=2 k=2 n=3 k=4
Output will be:
Solution:

def kthChildNthGeneration(n, k):
# if the its 1st gen or 1st child we know it,s akash i.e Male
if n == 1 or k == 1:
return "Male"
# Now determine the parent of k (not the gender just the number)
parent = (k + 1) // 2
# now we determine the parent gender
# we will call recursion to do it for us
# to check parent gender we will go back in genrations hence n - 1
# this was the reason we dtermined the prents number(which child ore prev gen he was)
# which will be our new k
parent_gender = kthChildNthGeneration(n-1, parent)
#after determining parent gender we will see if the given child is the first child
# if yes then we will print the parent gender
# now to do that we did (k + 1) // 2 to get the parent
# reversing that step will give the number of 1st child of that parent
# if that number is equal to k we return the parents gender
if k == (2*parent) -1:
return parent_gender
else:
# if not then it's the second child
#we check parents gender and
#return opposite gender
if parent_gender == "Male":
return "Female"
else:
return "Male"
</aside>
<aside> š” Question 3
Given an integerĀ n, returnĀ *trueĀ if it is a power of three. Otherwise, returnĀ false*.
An integerĀ nĀ is a power of three, if there exists an integerĀ xĀ such thatĀ n == 3x.
Example 1:
Input: n = 27
Output: true
Explanation: 27 = 33
Example 2:
Input: n = 0
Output: false
Explanation: There is no x where 3x = 0.
To determine if a given value is a power of 3, you can use the following problem-solving approach:
Let's go through an example using the value 81:
Since the final result is 1, we can conclude that 81 is a power of 3.
Let's try another example with the value 64:
By following this process, you can determine whether a given value is a power of 3 or not, without the need for writing a program.
def Power_of_three(n):
if n==1:
return True
if n<1 or n%3 !=0:
return False
return Power_of_three(n/3)
</aside>
<aside> š” Question 4
Given an integerĀ n, returnĀ *trueĀ if it is a power of two. Otherwise, returnĀ false*.
An integerĀ nĀ is a power of two, if there exists an integerĀ xĀ such thatĀ n == 2x.
Example 1: Input: n = 1
Output: true
Example 2: Input: n = 16
Output: true
Example 3: Input: n = 3
Output: false
To check if a value is a power of 2 using problem-solving, you can use the following approach:
Let's go through an example using the value 16:
Since the final result is 1, we can conclude that 16 is a power of 2.
Let's try another example with the value 15:
By following this process, you can determine whether a given value is a power of 2 or not, without the need for writing a program.
def Power_of_two(n):
if n==1:
return True
if n<1 or n%2 !=0:
return False
return Power_of_two(n/2)
</aside>
<aside> š” Question 5
Given a number n, find the sum of the first natural numbers.
Example 1:
Input: n = 3
Output: 6
Example 2:
Input : 5
Output : 15
Note:
Algorithm:
Make sure to test your solution with different test cases to ensure its correctness and efficiency.
To find the sum of the first n natural numbers using recursion, you can follow this problem-solving approach:
Here's an example of a recursive function to find the sum of the first n natural numbers:
def sum_of_naturals(n):
if n == 0:
return 0
return n + sum_of_naturals(n-1)
</aside>
<aside> š” Question 6
</aside>
<aside> š” Question 7
</aside>
<aside> š” Question 7
</aside>
<aside> š” Question 8
</aside>