0% found this document useful (0 votes)
24 views27 pages

Python Challenging Programming Exercises

Uploaded by

91learning360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
24 views27 pages

Python Challenging Programming Exercises

Uploaded by

91learning360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

Python challenging programming exercises

1. Level description
Level Description
Level 1Beginner means someone who has just gone through an introductory Python course. He
can solve some problems with 1 or 2 Python classes or functions.
Level 2Intermediate means someone who has just learned Python, but already has a relatively
strong programming background from before. He should be able to solve problems which may
involve 3 or 3 Python classes or functions..
Level 3Advanced. He should use Python to solve more complex problem using more rich
libraries functions and data structures

Level 1- 18/03/2024

Question:1
Write a program which will find all such numbers which are divisible by 7 but are not a multiple
of 5,
between 2000 and 3200 (both included).
The numbers obtained should be printed in a comma-separated sequence on a single line.

Hints:
Consider use range(#begin, #end) method

Question 2
Level 1

Question:
Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single line.
Suppose the following input is supplied to the program:
8
Then, the output should be:
40320

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 3
Level 1
Question:
With a given integral number n, write a program to generate a dictionary that contains (i, i*i)
such that is an integral number between 1 and n (both included). and then the program should
print the dictionary.
Suppose the following input is supplied to the program:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
Consider use dict()

Question 4
Level 1

Question:
Write a program which accepts a sequence of comma-separated numbers from console and
generate a list and a tuple which contains every number.
Suppose the following input is supplied to the program:
34,67,55,33,12,98
Then, the output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
tuple() method can convert list to tuple

Question 5
Level 1

Question:
Define a class which has at least two methods:
getString: to get a string from console input
printString: to print the string in upper case.
Also please include simple test function to test the class methods.

Hints:
Use __init__ method to construct some parameters
Question 6
level 1

Question:
Write a method which can calculate square value of number

Hints:
Using the ** operator

Question 7
Level 1

Question:
Python has many built-in functions, and if you do not know how to use it, you can read
document online or find some books. But Python has a built-in document function for every
built-in functions.
Please write a program to print some Python built-in functions documents, such as abs(),
int(), raw_input()
And add document for your own function

Hints:
The built-in document method is __doc__

Question 8
Level 1

Question:
Define a class, which have a class parameter and have a same instance parameter.

Hints:
Define a instance parameter, need add it in __init__ method
You can init a object with construct parameter or set the value later

Question:9
Define a function which can compute the sum of two numbers.

Hints:
Define a function with two numbers as arguments. You can compute the sum in the function and
return the value.

Question:10
Define a function that can convert a integer into a string and print it in console.

Hints:

Use str() to convert a number to string.

Question:11
Define a function that can convert a integer into a string and print it in console.

Hints:

Use str() to convert a number to string.

L E V E L 2- TO BE SOLVED ON 19/03/2024

Question 6
Level 2

Question:
Write a program that calculates and prints the value according to the given formula:
Q = Square root of [(2 * C * D)/H]
Following are the fixed values of C and H:
C is 50. H is 30.
D is the variable whose values should be input to your program in a comma-separated
sequence.
Example
Let us assume the following comma separated input sequence is given to the program:
100,150,180
The output of the program should be:
18,22,24

Hints:
If the output received is in decimal form, it should be rounded off to its nearest value (for
example, if the output received is 26.0, it should be printed as 26)
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 7
Level 2

Question:
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The
element value in the i-th row and j-th column of the array should be i*j.
Note: i=0,1.., X-1; j=0,1,¡­Y-1.
Example
Suppose the following inputs are given to the program:
3,5
Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]

Hints:
Note: In case of input data being supplied to the question, it should be assumed to be a console
input in a comma-separated form.

Question 8
Level 2

Question:
Write a program that accepts a comma separated sequence of words as input and prints the
words in a comma-separated sequence after sorting them alphabetically.
Suppose the following input is supplied to the program:
without,hello,bag,world
Then, the output should be:
bag,hello,without,world

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
Question 9
Level 2

Question:
Write a program that accepts sequence of lines as input and prints the lines after making all
characters in the sentence capitalized.
Suppose the following input is supplied to the program:
Hello world
Practice makes perfect
Then, the output should be:
HELLO WORLD
PRACTICE MAKES PERFECT

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 10
Level 2

Question:
Write a program that accepts a sequence of whitespace separated words as input and prints the
words after removing all duplicate words and sorting them alphanumerically.
Suppose the following input is supplied to the program:
hello world and practice makes perfect and hello world again
Then, the output should be:
again and hello makes perfect practice world

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
We use set container to remove duplicated data automatically and then use sorted() to sort the
data.

Question 11
Level 2

Question:
Write a program which accepts a sequence of comma separated 4 digit binary numbers as its
input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5
are to be printed in a comma separated sequence.
Example:
0100,0011,1010,1001
Then the output should be:
1010
Notes: Assume the data is input by console.

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 12
Level 2

Question:
Write a program, which will find all such numbers between 1000 and 3000 (both included) such
that each digit of the number is an even number.
The numbers obtained should be printed in a comma-separated sequence on a single line.

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 13
Level 2

Question:
Write a program that accepts a sentence and calculate the number of letters and digits.
Suppose the following input is supplied to the program:
hello world! 123
Then, the output should be:
LETTERS 10
DIGITS 3

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 14
Level 2

Question:
Write a program that accepts a sentence and calculate the number of upper case letters and
lower case letters.
Suppose the following input is supplied to the program:
Hello world!
Then, the output should be:
UPPER CASE 1
LOWER CASE 9

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 15
Level 2

Question:
Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
Suppose the following input is supplied to the program:
9
Then, the output should be:
11106

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

Question 16
Level 2

Question:
Use a list comprehension to square each odd number in a list. The list is input by a sequence of
comma-separated numbers.
Suppose the following input is supplied to the program:
1,2,3,4,5,6,7,8,9
Then, the output should be:
1,3,5,7,9
Question 17
Level 2

Question:
Write a program that computes the net amount of a bank account based a transaction log from
console input. The transaction log format is shown as following:
D 100
W 200

D means deposit while W means withdrawal.


Suppose the following input is supplied to the program:
D 300
D 300
W 200
D 100
Then, the output should be:
500

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

2.10

Question:18
Define a function that can receive two integral numbers in string form and compute their sum
and then print it in console.

Hints:

Use int() to convert a string to integer.

#----------------------------------------#
2.10

Question:19
Define a function that can accept two strings as input and concatenate them and then print it in
console.

Hints:

Use + to concatenate the strings

#----------------------------------------#
2.10

Question:20
Define a function that can accept two strings as input and print the string with maximum length
in console. If two strings have the same length, then the function should print al l strings line by
line.

Hints:

Use len() function to get the length of a string

#----------------------------------------#
2.10

Question:21
Define a function that can accept an integer number as input and print the "It is an even
number" if the number is even, otherwise print "It is an odd number".

Hints:

Use % operator to check if a number is even or odd.

#----------------------------------------#
2.10
Question:22
Define a function which can print a dictionary where the keys are numbers between 1 and 3
(both included) and the values are square of keys.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.


Use ** operator to get power of a number.

#----------------------------------------#
2.10

Question:23
Define a function which can print a dictionary where the keys are numbers between 1 and 20
(both included) and the values are square of keys.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.


Use ** operator to get power of a number.
Use range() for loops.

#----------------------------------------#
2.10

Question:24
Define a function which can generate a dictionary where the keys are numbers between 1 and
20 (both included) and the values are square of keys. The function should just print the values
only.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.


Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
#----------------------------------------#
2.10

Question:25
Define a function which can generate a dictionary where the keys are numbers between 1 and
20 (both included) and the values are square of keys. The function should just print the keys
only.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.


Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.

#----------------------------------------#
2.10

Question:26
Define a function which can generate and print a list where the values are square of numbers
between 1 and 20 (both included).

Hints:

Use ** operator to get power of a number.


Use range() for loops.
Use list.append() to add values into a list.

#----------------------------------------#
2.10

Question:27
Define a function which can generate a list where the values are square of numbers between 1
and 20 (both included). Then the function needs to print the first 5 elements in the list.

Hints:
Use ** operator to get power of a number.
Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list

#----------------------------------------#
2.10

Question:28
Define a function which can generate a list where the values are square of numbers between 1
and 20 (both included). Then the function needs to print the last 5 elements in the list.

Hints:

Use ** operator to get power of a number.


Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list

#----------------------------------------#
2.10

Question:29
Define a function which can generate a list where the values are square of numbers between 1
and 20 (both included). Then the function needs to print all values except the first 5 elements in
the list.

Hints:

Use ** operator to get power of a number.


Use range() for loops.
Use list.append() to add values into a list.
Use [n1:n2] to slice a list
#----------------------------------------#
2.10

Question:30
Define a function which can generate and print a tuple where the value are square of numbers
between 1 and 20 (both included).

Hints:

Use ** operator to get power of a number.


Use range() for loops.
Use list.append() to add values into a list.
Use tuple() to get a tuple from a list.

#----------------------------------------#
2.10

Question:31
With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line
and the last half values in one line.

Hints:

Use [n1:n2] notation to get a slice from a tuple.

#----------------------------------------#
2.10

Question:32
Write a program to generate and print another tuple whose values are even numbers in the
given tuple (1,2,3,4,5,6,7,8,9,10).

Hints:

Use "for" to iterate the tuple


Use tuple() to generate a tuple from a list.
#----------------------------------------#
2.14

Question:33
Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or
"Yes", otherwise print "No".

Hints:

Use if statement to judge condition.

L E V E L 3 - TO BE SOLVED ON 20/03/2024

Question 34
Level 3

Question:
A website requires the users to input username and password to register. Write a program to
check the validity of password input by users.
Following are the criteria for checking the password:
1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
1. At least 1 letter between [A-Z]
3. At least 1 character from [$#@]
4. Minimum length of transaction password: 6
5. Maximum length of transaction password: 12
Your program should accept a sequence of comma separated passwords and will check them
according to the above criteria. Passwords that match the criteria are to be printed, each
separated by a comma.
Example
If the following passwords are given as input to the program:
ABd1234@1,a F1#,2w3E*,2We3345
Then, the output of the program should be:
ABd1234@1

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
Question 35
Level 3

Question:
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 36
Level 3

Question:
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 37
Level 3
Question:
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 38
Level 3

Question:
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.

#----------------------------------------#
3.4

Question:29
Write a program which can filter even numbers in a list by using filter function. The list is:
[1,2,3,4,5,6,7,8,9,10].

Hints:

Use filter() to filter some elements in a list.


Use lambda to define anonymous functions.

#----------------------------------------#
3.4

Question:40
Write a program which can map() to make a list whose elements are square of elements in
[1,2,3,4,5,6,7,8,9,10].

Hints:

Use map() to generate a list.


Use lambda to define anonymous functions.

#----------------------------------------#
3.5

Question:41
Write a program which can map() and filter() to make a list whose elements are square of even
number in [1,2,3,4,5,6,7,8,9,10].
Hints:

Use map() to generate a list.


Use filter() to filter elements of a list.
Use lambda to define anonymous functions.

#----------------------------------------#
3.5

Question:42
Write a program which can filter() to make a list whose elements are even number between 1
and 20 (both included).

Hints:

Use filter() to filter elements of a list.


Use lambda to define anonymous functions.

#----------------------------------------#
3.5

Question:43
Write a program which can map() to make a list whose elements are square of numbers
between 1 and 20 (both included).

Hints:

Use map() to generate a list.


Use lambda to define anonymous functions.

#----------------------------------------#
7.2

Question:44
Define a class named American which has a static method called printNationality.

Hints:
Use @staticmethod decorator to define class static method.

#----------------------------------------#

7.2

Question:45
Define a class named American and its subclass NewYorker.

Hints:

Use class Subclass(ParentClass) to define a subclass.

#----------------------------------------#

7.2

Question:46
Define a class named Circle which can be constructed by a radius. The Circle class has a
method which can compute the area.

Hints:

Use def methodName(self) to define a method.

#----------------------------------------#

7.2
Ouestion:47
Define a class named Rectangle which can be constructed by a length and width. The
Rectangle class has a method which can compute the area.

Hints:
Use def methodName(self) to define a method.

#----------------------------------------#

7.2
Question:48
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:

To override a method in super class, we can define a method with the same name in the super
class.

Question:49

Assuming that we have some email addresses in the "username@companyname.com" format,


please write program to print the user name of a given email address. Both user names and
company names are composed of letters only.

Example:
If the following email address is given as input to the program:

john@google.com

Then, the output of the program should be:

john

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:

Use \w to match letters.

#----------------------------------------#
Question:50
Assuming that we have some email addresses in the "username@companyname.com" format,
please write program to print the company name of a given email address. Both user names
and company names are composed of letters only.

Example:
If the following email address is given as input to the program:

john@google.com

Then, the output of the program should be:

google

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:

Use \w to match letters.

#----------------------------------------#
Question:50

Write a program which accepts a sequence of words separated by whitespace as input to print
the words composed of digits only.

Example:
If the following words is given as input to the program:

2 cats and 3 dogs.

Then, the output of the program should be:

['2', '3']

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:

Use re.findall() to find all substring using regex.


#----------------------------------------#
Question:51

Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).

Example:
If the following n is given as input to the program:

Then, the output of the program should be:

3.55

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:
Use float() to convert an integer to a float

#----------------------------------------#
Question:52

Write a program to compute:

f(n)=f(n-1)+100 when n>0


and f(0)=1

with a given n input by console (n>0).

Example:
If the following n is given as input to the program:

Then, the output of the program should be:

500

In case of input data being supplied to the question, it should be assumed to be a console input.
Hints:
We can define recursive function in Python.

#----------------------------------------#

Question:53

The Fibonacci Sequence is computed based on the following formula:

f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1

Please write a program to compute the value of f(n) with a given n input by console.

Example:
If the following n is given as input to the program:

Then, the output of the program should be:

13

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:
We can define recursive function in Python.

#----------------------------------------#

#----------------------------------------#

Question:54

The Fibonacci Sequence is computed based on the following formula:


f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1

Please write a program using list comprehension to print the Fibonacci Sequence in comma
separated form with a given n input by console.

Example:
If the following n is given as input to the program:

Then, the output of the program should be:

0,1,1,2,3,5,8,13

Hints:
We can define recursive function in Python.
Use list comprehension to generate a list from an existing list.
Use string.join() to join a list of strings.

In case of input data being supplied to the question, it should be assumed to be a console input.

#----------------------------------------#

Question:55

Please write a program using generator to print the even numbers between 0 and n in comma
separated form while n is input by console.

Example:
If the following n is given as input to the program:

10

Then, the output of the program should be:

0,2,4,6,8,10

Hints:
Use yield to produce the next value in generator.
In case of input data being supplied to the question, it should be assumed to be a console input.

#----------------------------------------#

Question:56

Please write a program using generator to print the numbers which can be divisible by 5 and 7
between 0 and n in comma separated form while n is input by console.

Example:
If the following n is given as input to the program:

100

Then, the output of the program should be:

0,35,70

Hints:
Use yield to produce the next value in generator.

In case of input data being supplied to the question, it should be assumed to be a console input.

#----------------------------------------#

Question:57

Please write assert statements to verify that every number in the list [2,4,6,8] is even.

Hints:
Use "assert expression" to make assertion.

#----------------------------------------#

You might also like