Assignment
Assignment #: 4
Topic : Python Basics
Level 1: Easy
1. Write a Python program that takes two float variables and then print their sum
Sample test Cases:
Test Cases Test Case1: Test Case 2:
Input Number1:5.2 Number1: -20. 5
Number2:10.2 Number2: 30.7
Output Sum: 15.4 Sum: 51.2
2. Write a Python program that reads the user's name, age, and city, then prints
these details in a formatted manner.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Name: John Name: Alice
Age: 25 Age: 30
City: New York City: Los Angeles
Output User Details: User Details:
Name: John Name: Alice
Age: 25 Age: 30
City: New York City: Los Angeles
3. Accept two inputs from the user : item’s weight (floating points' values ) and
number of purchase (floating points' values) and calculate the average value of the
items (with two decimal places).
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Enter the weight of the item: Enter the weight of the item: 8.5
10 Enter the number of purchases: 2
Enter the number of purchases: 5
Output Average value of the Average value of the
item: 2.00 item: 4.25
4. Write a Python program that reads a temperature in Celsius from the user and converts it
to Fahrenheit.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input 25 0
Output 77.0 32.0
5. Write a Python program that reads a string representing an integer from the user and
converts it to an actual integer.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input “123” “456”
Output 123 456
6. Write a Python program that reads the radius of a circle from the user, calculates the area,
and prints the result. Use type conversion to ensure the radius is a float.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input 5 10
Output 78.54 314.16
7. Write a Python program that reads three different mathematical expressions from the user
and evaluates each one.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Expression 1: 2 + 3 Expression 1: 7 - 2
Expression 2: 4 * 5 Expression 2: 8 / 4
Expression 3: 6 / 2 Expression 3: 3 * 3
Output Result 1: 5 Result 1: 5
Result 2: 20 Result 2: 2.0
Result 3: 3.0 Result 3: 9
8. Write a Python program that reads the principal amount, rate of interest, and time period
from the user, then calculates and prints the simple interest.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Principal: 1000 Principal: 2000
Rate of Interest: 5 Rate of Interest: 3
Time Period: 2 Time Period: 4
Output Simple Interest: 100.0 Simple Interest: 240.0
9. Write a Python program that reads the weight (in kilograms) and height (in meters) of a
person, then calculates and prints their BMI.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Weight: 70 Weight: 60
Height: 1.75 Height: 1.6
Output BMI: 22.86 BMI: 23.44
10. Write a Python program that reads the principal amount, rate of interest, time period,
and number of times interest applied per time period from the user, then calculates and prints
the compound interest.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Principal: 1000 Principal: 2000
Rate of Interest: 5 Rate of Interest: 3
Time Period: 2 Time Period: 4
Number of times interest applied Number of times interest applied
per year: 4 per year: 2
Output Compound Interest: 104.08 Compound Interest: 252.45
11 Given are two integers values as input from the user. Your task here is to swap
two numbers using a temporary variable.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Enter two integers: 5 10 Enter two integers: -8 3
Output After swapping: After swapping:
First Number: 10 First Number: 3
Second Number: 5 Second Number: -8
12. Write a Python program that reads a time duration in seconds from the user and converts
it to hours, minutes, and seconds.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input 3661 7322
Output 1 hour, 1 minute, 1 second 2 hours, 2 minutes, 2 seconds
13. Write a Python program that reads a decimal number from the user and converts it to
binary, octal, and hexadecimal.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input 255 100
Output Binary: 11111111 Binary: 1100100
Octal: 377 Octal: 144
Hexadecimal: FF Hexadecimal: 64
14. Write a Python program that reads two integers from the user, verifies if both inputs are
valid integers, performs addition, subtraction, multiplication, and division, and prints the
results. Handle invalid inputs gracefully.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input Enter first number: 15 Enter first number: 20
Enter second number: xyz Enter second number: 5
Output Invalid input. Sum: 25
Please enter a valid number. Difference: 15
Product: 100
Division: 4.0
15. You are given a positive integer arrivalTime denoting the arrival time of a train
in hours, and another positive integer delayedTime denoting the amount of delay in
hours. Return the time when the train will arrive at the station.
Note that the time in this problem is in 24-hours format.
Example: Input: arrivalTime = 15, delayedTime = 5,Output: 20
Explanation: Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will
reach at 15+5 = 20 (20:00 hours).
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input arrivalTime = 15, arrivalTime = 13,
delayedTime = 5 delayedTime = 11
Output 20 0
Level 2: Medium
1. Write a Python program to calculate the number of leap years in a century.
Prompt the user to enter the century (e.g., 19 for 1901-2000). Validate the input as
a positive integer and calculate and print the number of leap years.
Sample test Cases:
Test Cases Test Case1:
Input Enter century (e.g., 19 for 1901-2000): 21
Output Number of leap years in the 21st century: 24
2. Arithmetic Number
Problem statement: Given three integers ‘A' denoting the first term of an arithmetic
sequence, 'C' denoting the common difference of an arithmetic sequence and an
integer 'B'. you need to tell whether 'B' exists in the arithmetic sequence or not.
Return 1 if B is present in the sequence. Otherwise, returns 0.
Constraints
-10^9 ≤ A, B, C ≤ 10^9
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input A = 1, B = 3, C = 2 A = 1, B = 2, C = 3
Output 1 0
3. Alternating Digit Sum
Problem statement: You are given a positive integer n. Each digit of n has a sign according to
the following rules:
▪ The most significant digit is assigned a positive sign.
▪ Each digit has an opposite sign to its adjacent digits.
Return the sum of all digits with their corresponding sign.
Constraints
1 <= n <= 109
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n = 521 n = 111
Output 4 1
4. Count Integers with Even Digit Sum
Problem statement: Given a positive integer num, return the number of positive integers less
than or equal to num whose digit sums are even. The digit sum of a positive integer is the
sum of all its digits.
Constraints
1 <= num <= 1000
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input num=4 num=30
Output 2 14
5. Count the Digits That Divide a Number
Problem statement: Given an integer num, return the number of digits in num that divide
num. An integer val divides nums if nums % val == 0.
Constraints
1 <= num <= 10^9
num does not contain 0 as one of its digits.
Sample test Cases
Test Cases Test Case 1: Test Case 2: Test Case 3:
Input num=7 num=121 num=1248
Output 1 2 4
6. Number of handshakes
Problem statement: The user is asked to take a number as integer n and find out the possible
number of handshakes. For example, if there are n number of people in a meeting and find
the possible number of handshakes made by the person who entered the room after all were
settled.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input 3 5
Output Possible number of handshakes: 3 Possible number of handshakes: 10
7. Divisor Game
Problem statement: Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number n on the chalkboard. On each player's turn, that player makes a
move consisting of:
● Choosing any x with 0 < x < n and n % x == 0.
● Replacing the number n on the chalkboard with n - x.
Also, if a player cannot make a move, they lose the game.
Return true only if Alice wins the game, assuming both players play optimally.
Constraints
1 <= n <= 1000
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n=2 n=3
Output true false
8. Nim Game
Problem Statement: You are playing the following Nim Game with your friend: Initially, there
is a heap of stones on the table. You and your friend will alternate taking turns, and you go
first. On each turn, the person whose turn it is will remove 1 to 3 stones from the heap. The
one who removes the last stone is the winner. Given n, the number of stones in the heap,
return true if you can win the game assuming both you and your friend play optimally,
otherwise return false.
Constraints
1 <= n <= 2^31 – 1
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n=4 n=1
Output false true
9. Arranging Coins
Problem Statement: You have n coins and you want to build a staircase with these coins. The
staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase
may be incomplete.
Given the integer n, return the number of complete rows of the staircase you will build.
Constraints
1 <= n <= 2^31 – 1
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n=5 n=8
Output 2 3
10. Recycling Pens
Problem statement: You have 'N' empty pens whose refills have been used up. You have 'R'
rupees in your pocket. You have two choices of operations that you can perform each time.
1) Recycle 1 empty pen and get 'K' rupees as a reward.
2) Buy 1 refill for 'C' rupees and combine it with 1 empty pen to make one usable pen.
Your task is to find the maximum number of usable pens that you can get.
For example if you have 'N' = 5 , 'R' = 10 , 'K' = 2 , 'C' = 3. You can recycle one pen and get
2 rupees as a reward so you will have a total of 12 rupees. Now you can buy 4 refills and
combine it with 4 pens to make it usable. So your answer is 4.
Constraints
1 <= T <= 10^5
1 <= N <= 10^9
0 <= R <= 10^9
1 <= K <= 10^9
1 <= C <= 10^9
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input 3 3
10 10 5 5 10 10 1 10
15 11 3 5 5055
3 20 20 2 6042
Output 6 1
7 2
3 4
11. Pass the Pillow
Problem Statement: There are n people standing in a line labeled from 1 to n. The first person
in the line is holding a pillow initially. Every second, the person holding the pillow passes it
to the next person standing in the line. Once the pillow reaches the end of the line, the
direction changes, and people continue passing the pillow in the opposite direction.
For example, once the pillow reaches the nth person they pass it to the n - 1th person, then
to the n - 2th person and so on.
Given the two positive integers n and time, return the index of the person holding the pillow
after time seconds.
Constraints
2 <= n <= 1000
1 <= time <= 1000
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n = 4, time = 5 n = 3, time = 2
Output 2 3
12. Factorial Divisibility
Problem statement: Given an integer n, find the largest power of a prime number p that
divides n!.
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n = 7, p = 3 n = 10, p = 3
Output 2 4
13. Climbing Stairs
Problem Statement: You are climbing a staircase. It takes n steps to reach the top. Each time
you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Constraints
1 <= n <= 45
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n=2 n=3
Output 2 3
14. Count possible ways to construct buildings
Problem Statement: There is a road passing through a city with N plots on both sides of the
road. Plots are arranged in a straight line on either side of the road. Determine the total
number of ways to construct buildings in these plots, ensuring that no two buildings are
adjacent to each other. Specifically, buildings on opposite sides of the road cannot be
adjacent.
Using * to represent a plot and || for the road, the arrangement for N = 3 can be visualized
as follows: * * * || * * *.
Note: As the answer can be very large, print it mod 109+7.Constraints
1 <= n <= 45
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input n=2 n=3
Output 2 3
15. Count possible ways to construct buildings
Problem Statement: There is a road passing through a city with N plots on both sides of the
road. Plots are arranged in a straight line on either side of the road. Determine the total
number of ways to construct buildings in these plots, ensuring that no two buildings are
adjacent to each other. Specifically, buildings on opposite sides of the road cannot be
adjacent.
Using * to represent a plot and || for the road, the arrangement for N = 3 can be visualized
as follows: * * * || * * *.
Note: As the answer can be very large, print it mod 109+7.
Constraints
1 <= n <= 45
Sample test Cases
Test Cases Test Case 1: Test Case 2:
Input N=1 N=3
Output 4 25