Python Assignments
Python Assignments
Hint:
Use for loop to iterate all possible solutions.
*******************************************************
Question 2:
Please write a program which accepts a string from console and print
the characters that have even indexes.
Example:
If the following string is given as input to the program:
H1e2l3l4o5w6o7r8l9d
Helloworld
Hints:
Use list[::2] to iterate a list by step 2.
*******************************************************
Question 3:
Please write a program which accepts a string from console and print
it in reverse order.
Example:
If the following string is given as input to the program:
Hints:
Use list[::-1] to iterate a list in a reverse order.
*******************************************************
Question 4:
Please write a program which count and print the numbers of each
character in a string input by console.
Example:
If the following string is given as input to the program:
abcdefgabc
a,2
c,2
b,2
e,1
d,1
g,1
f,1
Hints:
Use dict to store key/value pairs.
Use dict.get() method to lookup a key with default value.
*******************************************************
Question 5:
Define a class named Shape and its subclass Square. The Square class
has an init function which takes a length as argument. Both classes
have a area function which can print the area of the shape where
Shape's area is 0 by default.
Hints:
*******************************************************
Question 6:
*******************************************************
Question 7:
Example:
If the following email address is given as input to the program:
john@google.com
john
Hints:
*******************************************************
Question 8:
Example:
If the following words is given as input to the program:
['2', '3']
*******************************************************
Question 9:
Example:
If the following n is given as input to the program:
3.55
Hints:
Use float() to convert an integer to a float
*******************************************************
Question 10:
Example:
If the following n is given as input to the program:
500
Hints:
We can define recursive function in Python.
*******************************************************
Question 11:
Hints:
In case of input data being supplied to the question, it should be
assumed to be a console input.
******************************************************
Question 12:
You are required to write a program to sort the (name, age, height)
tuples by ascending order where name is string, age and height are
numbers. The tuples are input by console. The sort criteria is:
1: Sort based on name;
2: Then sort based on age;
3: Then sort by score.
The priority is that name > age > score.
If the following tuples are given as input to the program:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Then, the output of the program should be:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'),
('Json', '21', '85'), ('Tom', '19', '80')]
Hints:
In case of input data being supplied to the question, it should be
assumed to be a console input.
We use itemgetter to enable multiple sort keys.
******************************************************
Question 13:
Define a class with a generator which can iterate the numbers, which
are divisible by 7, between a given range 0 and n.
Hints:
Consider use yield
******************************************************
Question 14:
A robot moves in a plane starting from the original point (0,0). The
robot can move toward UP, DOWN, LEFT and RIGHT with a given steps.
The trace of robot movement is shown as the following:
UP 5
DOWN 3
LEFT 3
RIGHT 2
The numbers after the direction are steps. Please write a program to
compute the distance from current position after a sequence of
movement and original point. If the distance is a float, then just
print the nearest integer.
Example:
If the following tuples are given as input to the program:
UP 5
DOWN 3
LEFT 3
RIGHT 2
Then, the output of the program should be:
2
Hints:
In case of input data being supplied to the question, it should be
assumed to be a console input.
******************************************************
Question 15:
Write a program to compute the frequency of the words from the
input. The output should output after sorting the key
alphanumerically.
Suppose the following input is supplied to the program:
New to Python or choosing between Python 2 and Python 3? Read Python
2 or Python 3.
Then, the output should be:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1
Hints
In case of input data being supplied to the question, it should be
assumed to be a console input.
******************************************************