PyPro: Challenge 2#
This challenge is due on Saturday, October 5th, 2024, at 8pm.#
These exercises offer a diverse array of tasks that assess your comprehension of fundamental Python concepts such as loops, data types, string manipulations, list comprehensions, and higher-order functions.
Levels |
Level Description |
Estimated Completion Time per Exercise |
---|---|---|
L1 |
Beginner. The solutions can typically be found in standard textbooks. |
5 - 10 minutes |
L2 |
Intermediate. Requires problem-solving skills. Solutions are not directly found in textbooks. |
10 - 20 minutes |
L3 |
Advanced. You should be capable of using Python to solve more intricate problems involving libraries, data structures, and algorithms. |
20 - 30 minutes |
Question 1#
Level 1
Task:
Develop a program to find all numbers between 2000 and 3200 (inclusive) that are divisible by 7 but not multiples of 5. Display these numbers as a single line of output, separated by commas.
Question 2#
Level 1
Task:
Create a function that computes the factorial of a specified integer. Display the result in a single line of output. For example, if the input is:
8
Then, the output should be:
40320
Question 3#
Level 1
Task:
Given an integer n
, generate a dictionary with entries (i, i*i) for all integers from 1 to n
(inclusive). Print the resulting dictionary. For example, if the input is:
8
Then, the output should be:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
Question 4#
Level 1
Task:
Create a program that reads a series of comma-separated numbers from the console, then generates and prints both a list and a tuple containing those numbers. For example, if the input is:
34,67,55,33,12,98
The output should be:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')
Question 5#
Level 1
Task:
Create a class that has two methods:
getString
: To accept a string input from the console.printString
: To display the string in uppercase.
Include a simple test function to demonstrate the class methods.
Question 6#
Level 2
Task:
Write a program that calculates and prints values based on the formula:
[ Q = \sqrt{\frac{2 \times C \times D}{H}} ]
Here, C and H are fixed constants with values 50 and 30, respectively. D
is a variable whose values are input to the program as a comma-separated sequence. For example, if the input is:
100,150,180
Then, the output should be:
18,22,24
Question 7#
Level 2
Task:
Write a program that accepts two numbers, X and Y, as input and generates a 2-dimensional list. The value at the i-th row and j-th column should be the product of i
and j
. For example, if the input is:
3,5
The output should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
Question 8#
Level 2
Task:
Write a program that accepts a comma-separated list of words, sorts them alphabetically, and then prints them in a single line, separated by commas. For example, if the input is:
without,hello,bag,world
The output should be:
bag,hello,without,world
Question 9#
Level 2
Task:
Create a program that reads multiple lines of input and outputs each line with all characters converted to uppercase. For example, if the input is:
Hello world
Practice makes perfect
The output should be:
HELLO WORLD
PRACTICE MAKES PERFECT
Question 10#
Level 2
Task:
Write a program that reads a sequence of whitespace-separated words, removes all duplicate words, sorts them alphanumerically, and then prints the resulting words. For example, if the input is:
hello world and practice makes perfect and hello world again
The output should be:
again and hello makes perfect practice world
Question 11#
Level 2
Task:
Create a program that takes a sequence of 4-digit binary numbers, checks which ones are divisible by 5, and prints them as a comma-separated sequence. For example, if the input is:
0100,0011,1010,1001
The output should be:
1010
Note: Assume the input is provided through the console.
Question 12#
Level 2
Task:
Develop a program that finds all numbers between 1000 and 3000 (inclusive) where each digit is an even number. Print the results as a comma-separated sequence in a single line.
Question 13#
Level 2
Task:
Write a program that counts the number of letters and digits in a given sentence. For example, if the input is:
hello world! 123
The output should be:
LETTERS 10
DIGITS 3
Question 14#
Level 2
Task:
Create a program that counts and prints the number of uppercase and lowercase letters in a given sentence. For example, if the input is:
Hello world!
The output should be:
UPPER CASE 1
LOWER CASE 9
Question 15#
Level 2
Task:
Write a program that calculates the value of a + aa + aaa + aaaa
given a digit as the value of a
. For example, if the input is:
9
The output should be:
11106
Question 16#
Level 2
Task:
Use a list comprehension to generate a list of squares for each odd number in a given sequence of comma-separated values. For example, if the input is:
1,2,3,4,5,6,7,8,9
The output should be:
1,3,5,7,9
Question 17#
Level 2
Task:
Create a program that calculates the net amount in a bank account based on a transaction log input. The log format is:
D 100
(Deposit 100)
W 200
(Withdraw 200)
Suppose the following input is provided:
D 300
D 300
W 200
D 100
The output should be:
500
Question 18#
Level 3
Task:
Write a program that checks the validity of passwords based on the following criteria:
At least one lowercase letter [a-z]
At least one digit [0-9]
At least one uppercase letter [A-Z]
At least one character from [$#@]
Minimum length of 6 characters
Maximum length of 12 characters
Input is provided as a sequence of comma-separated passwords. Valid passwords should be printed, separated by commas. For example, if the input is:
ABd1234@1,a F1#,2w3E*,2We3345
The output should be:
ABd1234@1
Question 19#
Level 3
Task:
Create a program that sorts a list of (name, age, height) tuples based on the following priority:
Name (alphabetically)
Age (numerically)
Height (numerically)
For example, if the input is:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
The output should be:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
Question 20#
Level 3
Task:
Define a class that includes a generator to yield numbers divisible by 7 within a given range from 0 to n.
Question 21#
Level 3
Task:
A robot moves on a grid starting from the origin
point (0,0). It can move UP, DOWN, LEFT, or RIGHT by a specified number of steps. Write a program to calculate the distance from the current position to the origin after a series of movements. If the distance is a float, print the nearest integer. For example, if the input is:
UP 5
DOWN 3
LEFT 3
RIGHT 2
The output should be:
2
Question 22#
Level 3
Task:
Create a program to calculate the frequency of words from the input. Display the output with the keys sorted alphabetically. For example, if the input is:
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
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
Question 23#
Level 1
Task:
Create a function that calculates and returns the square of a given number.
Question 24#
Level 1
Task:
Python provides built-in documentation for each built-in function. Create a program that displays the documentation for abs()
, int()
, and raw_input()
. Also, add documentation for a user-defined function.
Question 25#
Level 1
Task:
Define a class with both a class attribute and an instance attribute of the same name. Show the difference between the two by printing the class attribute and the instance attribute separately.