CPS Lab1
CPS Lab1
Lab Sheet 1
Introduction to Python
How to Output
comments
#This is a comment
print("Hello, World!")
Assignment Operator
x = 5
y = "Amrita"
print(x)
print(y)
x = y = z = "Orange"
print(x)
print(y)
print(z)
Python Operators
x = 10
y = 5
# Addition Operator
z=x+y
print(z)
print('x + y = ', x + y)
x = 10
y = 5
# Subtraction Operator
z=x-y
print(z)
print('x - y = ', x - y)
x = 10
y = 5
# Multiplication Operator
z=x*y
print(z)
print('x * y = ', x * y)
In Python, there are two types of division operators: / : Divides the number on its left
by the number on its right and returns a floating point value. // : Divides the number
on its left by the number on its right, rounds down the answer, and returns a whole
number.
x = 10
y = 5
# Division Operator
print('x / y = ', x / y) # True Division
print('x // y =', x//y) # Class Division
Modulus Operator
x = 103
y = 5
# Modulus
print('x % y = ', x % y)
x = 100
y = 5
# Modulus
print('x % y = ', x % y)
x = 15
y = 6
# Exponential Operator
print('x ** y = ', x ** y)
import math
x = 4.5467
print (math.ceil(x))
x = 4.1467
print (math.ceil(x))
x = 4
print (math.ceil(x))
It is the opposite of the ceil method and it returns the floor value of the number, i.e it is
rounded down to the previous integer value irrespective of whether the number after the
decimal is less than 5 or greater than it. If the number passed to the floor method is an
integer, it is returned unchanged. If a negative number is passed to the floor method, it
rounds down the number, i.e it returns the next lesser integer number present on the number
line.
import math
my_int = 4.5467
print (math.floor(my_int))
my_int = 4.9467
print (math.floor(my_int))
my_int = 4
print (math.floor(my_int))
my_int = -4.5467
print (math.floor(my_int))
abs() function
The abs() function returns the absolute value of the given number.
# random integer
integer = -20
print('Absolute value of -20 is:', abs(integer))
Simple conditional
a = 33
b = 200
if b > a:
print("b is greater than a")
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
https://www.codechef.com/problems/POPULATION
There were initially X million people in a town, out of which Y million people left the town and Z
million people immigrated to this town.
The first line of input will contain a single integer T, denoting the number of test cases.
The first and only line of each test case consists of three integers X, Y and Z.
312
222
418
10 1 10
Output
11
19
Explanation:
Test case
1: The initial population of the town was 3 million, out of which 1 million people left and 2 million
people entered the town. So, final population = 3 −1+2=4 =3−1+2=4 million.
Test case
2: The initial population of the town was 2 million, out of which 2 million left and 2 million
immigrated. The final population is thus 2+2−2=2 million.
2. Problem 2 POLYBAGS
https://www.codechef.com/problems/POLYBAGS
Chef bought N items from a shop. Although it is hard to carry all these items in hand, so Chef has to
buy some polybags to store these items.
1 polybag can contain at most 10 10 items. What is the minimum number of polybags needed by
Chef?
20 2
24 3
99 10
Explanation:
Test case-1: Chef will require 2 polybags. Chef can fit 10 items in the first and second polybag each.
Test case-2: Chef will require 3 polybags. Chef can fit 10 items in the first and second polybag each
and fit the remaining 4 items in the third polybag.
3. Problem 3. FRUITCHAAT
https://www.codechef.com/problems/FRUITCHAAT
Chef has closed his restaurant and decided to run a fruit stand instead. His signature dish is a fruit
chaat consisting of 2 bananas and
1 apple. He currently has X bananas and Y apples. How many chaats can he make with the fruits he
currently has?
72 50
38 93
51 4
36
19
4
Explanation:
Test Case 1 Chef can make 36 chaats using 72 bananas and 36 apples.
Test Case 2 Chef can make 19 chaats using 38 bananas and 19 apples.
Test Case 3 Chef can make 4 chaats using 8 bananas and 4 apples.
4. Problem 4 MINCOINS
https://www.codechef.com/problems/MINCOINS
Chef has infinite coins in denominations of rupees 5 and rupees 10. Find the minimum number of
coins Chef needs, to pay exactly X rupees. If it is impossible to pay X rupees in denominations of
rupees 5 and 10 only, print − 1 .
Input Format
Output Format
For each test case, print a single integer - the minimum number of coins Chef needs, to pay exactly X
rupees. If it is impossible to pay X rupees in denominations of rupees 5 and 10 only, print − 1.
50
15
Output
-1
Explanation:
Test Case 1
Chef would require at least 5 coins to pay 50 rupees. All these coins would be of rupees 10.
Test Case 2 Chef would require at least 2 coins to pay 15 rupees. Out of these, 1 coin would be of
rupees 10 and 1 coin would be of rupees 5
Test Case 3
1. Login to codechef.. Create a handle .Write solutions in jupyter notebook for the below
problems
https://www.codechef.com/problems/REACHFAST
https://www.codechef.com/problems/SONGS