You are on page 1of 6

Python

September 27, 2017

Official website : python.org

Python has become very popular because of its simple style of programming which appeals to most
of the programmers. It is extensively used by researchers, data scientists, network engineers for
writing web crawlers , automation scripts etc.

Python has many libraries. Few examples include numpy, selenium, beautiful soup, pygame and the
most recent tensor flow which has gained immense popularity.

The aim of this module is to introduce you to python and make you a python ninja.

!!! Happy Coding !!!

Team SSL

Day 1 - Welcome to school


Familiarize yourself with the basic concepts of list and its associated functions, dictionary, sets, tuples, strings
and its manipulation, loops , decision making and functions. You can also go through numpy library if you
have time.

Practice session
Take a list with duplicate elements.
Sort it.

Remove duplicate elements by converting it to a set.


Find the maximum element.
Count the no of elements.

1
Day 2 - Learning the first moves
These questions would help you build concepts on hashing. In python there is something called dictionary
which makes the job of hashing easier.
1. Given an array and a no x find whether there exists a pair with sum as x . Expected time complexity
O(n) . Expected space complexity O(n) .
First line contains the no of test cases n . For each test case , first line contains x. The second line
contains the array. For each test case print any pair if it exists. Otherwise print No such pair exists.
Sample Input
2
4
1 2 3 4
5
3 4 5 6 10
Sample Output
1 3
No such pair exists
2. Check whether two sets are disjoint. First line contains the no of test cases n . For each test case 1st
line contains first set, the second line contains the second. Print YES if the set is disjoint, else print
NO. Time complexity O(min(m,n)) where m and n are length of the two sets. Space complexity O(1)

Sample Input
1
1 2 3 4
5 6 7 8
Sample Output
YES
3. Tell if there is a subarray with 0 sum. First line contains the no of test cases n . Each test case contains
a single line denoting the array. You have to tell YES if such array exists, otherwise NO.

Sample Input
1
1 2 -3 4
Sample Output
YES
4. Display words that appear exactly two times in an array of words.First line conatins the no of test
cases. Time Complexity O(n) where n is the size of the list.

Sample Input
1
aa bb cc aa bb bb
Sample Output
aa

2
Day 3 Sharpening your tools
Its time to learn some DP concepts.
5. Coin change problem.
Given a value N, find the number of ways to make change for N cents, if we have infinite supply of
each of S = S1, S2, .. , Sm valued coins. The order of coins doesnt matter.

Sample Input
First line contains no of test cases. For each test case first line contains the array denoting the coins
available and the second line contains the value N

2
1 2 3
4
2 5 3 6
10

Sample Output

4
5

6. 0-1 Knapsack problem. Given weights and values of n items, put these items in a knapsack of capacity
W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1]
and wt[0..n-1] which represent values and weights associated with n items respectively. Also given an
integer W which represents knapsack capacity, find out the maximum value subset of val[] such that
sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick
the complete item, or dont pick it (0-1 property).

Sample Input
First line contains no of test cases. For each test case first line contains the array denoting the values,
second line denotes the weights available and the third line denotes the maximum knapsack capacity.

1
60 100 120
10 20 30
50

Sample Output

220

Practice Session
Maximum profit by buying and sharing a share atmost twice. In a daily share trading, a buyer buys
shares in the morning and sells it on same day. If the trader is allowed to make at most 2 transactions
in a day, where as second transaction can only start after first one is complete (Sell-buy-sell-buy).
Given stock prices throughout day, find out maximum profit that a share trader could have made. e.g.

Input: price[] = {10, 22, 5, 75, 65, 80}


Output: 87
Trader earns 87 as sum of 12 and 75
Buy at price 10, sell at 22, buy at 5 and sell at 80

3
Day 4 Adding the Object Oriented Weapon
Practice Session
Design a class student with the following data members-

Name
Roll NO
Age
Marks

Write functions to sort students according to name , age and marks.

Write a function to print the details of a student.


Create a list of students and apply the above functions.

7. Implement the Huffman code Compression technique. Mark the left child as 0 and the right as 1.

Sample Input
First line contains the no of characters.
Each subsequent line contains the character and its frequency

6
a 5
b 9
c 12
d 13
e 16
f 45

Sample Output

It can be in any order. But the overall weight should be same.

f 0
c 100
d 101
a 1100
b 1101
e 111

4
FAQs
Before you become a Python Ninja there will be questions in your mind.

1. What type of questions will come in exam.


In exams questions will be based on Day 1 and Day 2.
2. Do we need to submit questions given in practice session.

No . Only questions that are enumerated need to be submitted.


3. Will the questions be hard.
Depends on your practice.

5
Day 5 Rise of the Python Ninja
Be prepared. Snake is coming . . .

You might also like