<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)

flow chart of how recursion works in tower of honai game.

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:

  1. Start with the given value and divide it by 3 repeatedly until you obtain a result less than 3.
  2. If the final result is exactly 1, then the original value is a power of 3. Otherwise, it is not.

Let's go through an example using the value 81:

  1. Start with 81 and divide by 3: 81 / 3 = 27.
  2. Continue dividing by 3: 27 / 3 = 9, and 9 / 3 = 3.
  3. Finally, divide 3 by 3: 3 / 3 = 1.

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:

  1. Divide 64 by 3: 64 / 3 = 21.33 (approximately).
  2. The result is not an integer less than 3, so we can conclude that 64 is not a power of 3.

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:

  1. Start with the given value.
  2. Keep dividing the value by 2 repeatedly until you obtain a result less than 2.
  3. If the final result is exactly 1, then the original value is a power of 2. Otherwise, it is not.

Let's go through an example using the value 16:

  1. Start with 16 and divide by 2: 16 / 2 = 8.
  2. Continue dividing by 2: 8 / 2 = 4, and 4 / 2 = 2.
  3. Finally, divide 2 by 2: 2 / 2 = 1.

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:

  1. Divide 15 by 2: 15 / 2 = 7.5 (approximately).
  2. The result is not an integer less than 2, so we can conclude that 15 is not a power of 2.

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:

  1. Read the input value n.
  2. Calculate the sum using the formula sum = (n * (n + 1)) / 2.
  3. Return the sum as the output.

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>