Computational Problem Solving

advertisement
Computational Problem Solving
Lab Sheet 1
Familiarisation to Jupyter notebook
Either use Colab https://colab.research.google.com/
Or open the local Jupyter Notebook installed in your machine
Introduction to Python
Try the below code snippets in your jupyter Notebook.
How to Output
print("Amrita Vishwa Vidyapeetham!")
comments
#This is a comment
print("Hello, World!")
Assignment Operator
x = 5
y = "Amrita"
print(x)
print(y)
Multiple variable assignment
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
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)
The math library and ceil method
Ceil is a function in math library which takes parameter as the argument, whose ceil value is
to be returned, i.e the number is rounded up to its next integer value. This is done
irrespective of whether the number after the decimal is less than 5 or greater than 5. If the
number is an integer, it is returned unchanged. If the number is a negative number, it moves
the number up on the number line to the next integer value and returns that as output.
import math
x = 4.5467
print (math.ceil(x))
x = 4.1467
print (math.ceil(x))
x = 4
print (math.ceil(x))
x = -4.5467
# It behaves in the same way when used with negative
numbers also
print (math.ceil(x))
The floor method
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))
#random floating number
floating = -30.33
print('Absolute value of -30.33 is:', abs(floating))
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")
PROBLEM SOLVING EXERCISE FROM CODECHEF
1. Problem1 : Final Population
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.
Determine the final population of town in millions.
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.
Input Test Cases
312
222
418
10 1 10
Output
4
2
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 items. What is the minimum number of polybags needed by Chef?
Input Test Cases
Output
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?
Test Case inputs
72 50
38 93
51 4
Test Case Outputs
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
Each test case contains of a single integer X.
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.
Input Test Cases
50
15
8
Output
5
2
-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
Chef cannot pay exactly 8 rupees in denominations of rupees 5 and 10 only.
Homework
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
2. Try submitting all the above problems directly in codechef
Install Jupyter notebook in your laptop
https://jupyter.org/install
https://noteable.io/jupyter-notebook/install-jupyter-notebook/
Download