0% found this document useful (0 votes)
46 views3 pages

Practical 2

The document contains code snippets for solving various problems: 1) A program to calculate nCr and nPr using recursive factorial functions. It takes n and r as input and prints the results. 2) A function to check if a number is prime. It takes a number as input and prints if it is prime or not. 3) Code with an issue of assigning a string to an integer within a nested function. 4) A program to check if a string is a palindrome by ignoring case and non-alphabetic characters. 5) A recursive function to solve the Tower of Hanoi problem but it does not display output as the print statement is commented. Suggestion is to add a return

Uploaded by

Ayush Sheth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views3 pages

Practical 2

The document contains code snippets for solving various problems: 1) A program to calculate nCr and nPr using recursive factorial functions. It takes n and r as input and prints the results. 2) A function to check if a number is prime. It takes a number as input and prints if it is prime or not. 3) Code with an issue of assigning a string to an integer within a nested function. 4) A program to check if a string is a palindrome by ignoring case and non-alphabetic characters. 5) A recursive function to solve the Tower of Hanoi problem but it does not display output as the print statement is commented. Suggestion is to add a return

Uploaded by

Ayush Sheth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical 2

1. Write a program to find nCr and nPr. Scan the value of n and r from the user. Use
recursive function for factorial calculation.

def factorial(n):
"""Recursive function to calculate factorial"""
if n == 0:
return 1
else:
return n * factorial(n-1)

# Get input from user


n = int(input("Enter value of n: "))
r = int(input("Enter value of r: "))

# Calculate nCr and nPr


ncr = factorial(n) / (factorial(r) * factorial(n-r))
npr = factorial(n) / factorial(n-r)

# Print the results


print("nCr =", ncr)
print("nPr =", npr)

Output:

Enter value of n: 5
Enter value of r: 4
nCr = 5.0
nPr = 120.0

2. Create a function isPrime(n) which returns 1 if n is prime and 0 if not prime. Scan
the value of n from the user.

def isPrime(n):
"""Function to check if a number is prime"""
if n < 2:
return 0
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return 0
return 1

Practical 2 1
# Get input from user
n = int(input("Enter a number: "))

# Check if n is prime
if isPrime(n):
print(f"{n} is prime")
else:
print(f"{n} is not prime")

Output:

Enter a number: 5
5 is prime

Enter a number: 6
6 is not prime

3. Execute the sample program given below. Note down your understanding about the
scope of variables.

def g(x):
def h():
x = 'abc'
x = x + 1
print('in g(x): x =', x)
h()
return x
x = 3
z = g(x)

The above code defines two functions, g(x) and h() , and then assigns a value to
x and calls g(x) . However, there is an issue with the code that will cause it to raise

a TypeError.

The issue is in the line x = x + 1 inside the h() function. Here, x is a string that is
being concatenated with an integer value 1. This is not a valid operation, and it will
raise a TypeError. Instead, if the intention is to increment the integer value of x by
1, x should be an integer to begin with.

4. Write a program with a function isPal () to check whether the input string is a
palindrome or not.

Practical 2 2
def isPal(s):
"""Function to check if a string is a palindrome"""
s = s.lower()
# Remove non-alphanumeric characters from string
s = ''.join(char for char in s if char.isalnum())
return s == s[::-1]

# Get input from user


s = input("Enter a string: ")

# Check if s is a palindrome
if isPal(s):
print(f"{s} is a palindrome")
else:
print(f"{s} is not a palindrome")

Output:

Enter a string: mom


mom is a palindrome

5. Execute the following program and explain what it does and Suggest a possible
improvement in the implementation.

def printMove(fr, to):


print('move from ' + str(fr) + ' to ' + str(to))
def Towers(n, fr, to, spare):
if n == 1:
printMove(fr, to)
else:
Towers(n-1, fr, spare, to)
Towers(1, fr, to, spare)
Towers(n-1, spare, to, fr)
#print(Towers(4, 'P1', 'P2', 'P3'))

The above code defines two functions, printMove(fr, to) and Towers(n, fr, to,
spare) , and then calls the Towers() function with arguments 4, 'P1', 'P2', and 'P3'.

However, there is no output when the code is run, because the Towers() function
does not return anything and the call to print() is commented out.

Practical 2 3

You might also like