0% found this document useful (0 votes)
7 views

2ndweekassignment 1

The document discusses different approaches to calculating the Fibonacci sequence in Python code: using a native iterative approach, a recursive approach, and a dynamic programming approach. It also includes Python code to print all permutations of a given string and check if a number is prime.

Uploaded by

divya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

2ndweekassignment 1

The document discusses different approaches to calculating the Fibonacci sequence in Python code: using a native iterative approach, a recursive approach, and a dynamic programming approach. It also includes Python code to print all permutations of a given string and check if a number is prime.

Uploaded by

divya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

2ndweekassignment

September 14, 2023

0.1 Question 1:
i) Write a program on the Fibonacci series in Python using the native approach.

[1]: def fibonacci(n):


prev = 0
present = 1
future = 0
for i in range(n):
future = prev + present
prev= present
present = future
return future

[2]: n = int(input("Enter number of Fibonacci numbers to be printed: "))


print("Fibonacci numbers are: ")
for i in range(n):
print(fibonacci(i))

Enter number of Fibonacci numbers to be printed: 5


Fibonacci numbers are:
0
1
2
3
5
ii) If you have to write the same code using the recursion concept, what will be the differences
in the code? State your observation and write the code
[3]: def fibonacci_recursive(n):
if n == 0 or n == 1:
return n
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)

[4]: n = int(input("Enter number of Fibonacci numbers to be printed Byusing␣


↪Recursion: "))

print("Fibonacci numbers are: ")


for i in range(n):

1
print(fibonacci_recursive(i))

Enter number of Fibonacci numbers to be printed Byusing Recursion: 5


Fibonacci numbers are:
0
1
1
2
3
Observation:
In the iterative approach, we have more efficient code compared to recursive approach. In Time
complexity in iterative: 1. The time complexity is O(n)
Recursive approachis less efficien in case of large values of ntbecause we are calling the same work
multiple time causing more redundant calculations. 1. The time complexity is exponential, O(2^n)
iii) If you to write the same code using the dynamic programming concept, what will be the
differences in the code? State your observation andd write the code.
[5]: def fibonacci_dynamic(n):
fib_array = [0] * (n + 1)
if n > 0:
fib_array[1] = 1
for i in range(2, n + 1):
fib_array[i] = fib_array[i - 1] + fib_array[i - 2]
return fib_array[n]

[6]: n = int(input("Enter number of Fibonacci numbers to be printed By using Dynamic␣


↪Programming concept: "))

print("Fibonacci numbers are: ")


for i in range(n):
print(fibonacci_dynamic(i))

Enter number of Fibonacci numbers to be printed By using Dynamic Programming


concept: 5
Fibonacci numbers are:
0
1
1
2
3
Observation:
1. To calculate the nth Fibonacci number, We need the values of the (n-1)th and (n-2)nd Fi-
bonacci numbers.

2
2. These smaller subproblems are solved repeatedly in a recursive approach, leading to expo-
nential time complexity.
3. DP addresses this issue by solving each subproblem once and storing its result for future use

1 Question 2
• Write a Python program to print all permutations of a given string
[7]: def permute_string(input_string, current_permutation):
if len(input_string) == 0:
print(current_permutation, end=" ")
return

for i in range(len(input_string)):
character = input_string[i]
left_substr = input_string[:i]
right_substr = input_string[i + 1:]
remaining = left_substr + right_substr
permute_string(remaining, current_permutation + character)

input_string = input("Enter the string: ")


print("All possible permutations are:")
permute_string(input_string, "")

Enter the string: cat


All possible permutations are:
cat cta act atc tca tac

2 Question 3
• Write a Python program to check prime number
[8]: def check_prime(n):
if n <= 1:
return False

is_prime = True
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
is_prime = False
break

return is_prime

[11]: check_prime(11)

3
[11]: True

You might also like