w3resource
Python Exercises

Python Basic(Part-II) - Exercises, Practice, Solution

Python basic (Part-II) [63 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python function that takes a sequence of numbers and determines if all the numbers are different from each other. Go to the editor
Click me to see the sample solution

2. Write a Python program to create all possible strings by using 'a', 'e', 'i', 'o', 'u'. Use the characters exactly once. Go to the editor
Click me to see the sample solution

3. Write a Python program to remove and print every third number from a list of numbers until the list becomes empty.
Click me to see the sample solution

4. Write a Python program to find unique triplets whose three elements gives the sum of zero from an array of n integers. Go to the editor
Click me to see the sample solution

5. Write a Python program to create the combinations of 3 digit combo. Go to the editor
Click me to see the sample solution

6. Write a Python program to print a long text, convert the string to a list and print all the words and their frequencies. Go to the editor
Click me to see the sample solution

7. Write a Python program to count the number of each character of a given text of a text file. Go to the editor
Click me to see the sample solution

8. Write a Python program to get the top stories from Google news. Go to the editor
Click me to see the sample solution

9. Write a Python program to get a list of locally installed Python modules. Go to the editor
Click me to see the sample solution

10. Write a Python program to display some information about the OS where the script is running. Go to the editor
Click me to see the sample solution

11. Write a Python program to check the sum of three elements (each from an array) from three arrays is equal to a target value. Print all those three-element combinations. Go to the editor
Sample data:
/*
X = [10, 20, 20, 20]
Y = [10, 20, 30, 40]
Z = [10, 30, 40, 20]
target = 70
*/
Click me to see the sample solution

12. Write a Python program to create all possible permutations from a given collection of distinct numbers.Go to the editor
Click me to see the sample solution

13. Write a Python program to get all possible two digit letter combinations from a digit (1 to 9) string. Go to the editor
string_maps = {
"1": "abc",
"2": "def",
"3": "ghi",
"4": "jkl",
"5": "mno",
"6": "pqrs",
"7": "tuv",
"8": "wxy",
"9": "z"
}
Click me to see the sample solution

14. Write a Python program to add two positive integers without using the '+' operator. Go to the editor
Note: Use bit wise operations to add two numbers.
Click me to see the sample solution

15. Write a Python program to check the priority of the four operators (+, -, *, /). Go to the editor
Click me to see the sample solution

16. Write a Python program to get the third side of right angled triangle from two given sides. Go to the editor
Click me to see the sample solution

17. Write a Python program to get all strobogrammatic numbers that are of length n. Go to the editor
A strobogrammatic number is a number whose numeral is rotationally symmetric, so that it appears the same when rotated 180 degrees. In other words, the numeral looks the same right-side up and upside down (e.g., 69, 96, 1001).
For example,
Given n = 2, return ["11", "69", "88", "96"].
Given n = 3, return ['818', '111', '916', '619', '808', '101', '906', '609', '888', '181', '986', '689']

Click me to see the sample solution

18. Write a Python program to find the median among three given numbers. Go to the editor
Click me to see the sample solution

19. Write a Python program to find the value of n where n degrees of number 2 are written sequentially in a line without spaces. Go to the editor
Click me to see the sample solution

20. Write a Python program to find the number of zeros at the end of a factorial of a given positive number. Go to the editor
Range of the number(n): (1 = n = 2*109).
Click me to see the sample solution

21. Write a Python program to find the number of notes (Sample of notes: 10, 20, 50, 100, 200 and 500 ) against an given amount. Go to the editor
Range - Number of notes(n) : n (1 = n = 1000000).
Click me to see the sample solution

22. Write a Python program to create a sequence where the first four members of the sequence are equal to one, and each successive term of the sequence is equal to the sum of the four previous ones. Find the Nth member of the sequence. Go to the editor
Click me to see the sample solution

23. Write a Python program that accept a positive number and subtract from this number the sum of its digits and so on. Continues this operation until the number is positive. Go to the editor
Click me to see the sample solution

24. Write a Python program to find the number of divisors of a given integer is even or odd. Go to the editor
Click me to see the sample solution

25. Write a Python program to find the digits which are absent in a given mobile number. Go to the editor
Click me to see the sample solution

26. Write a Python program to compute the summation of the absolute difference of all distinct pairs in an given array (non-decreasing order). Go to the editor
Sample array: [1, 2, 3]
Then all the distinct pairs will be:
1 2
1 3
2 3
Click me to see the sample solution

27. Write a Python program to find the type of the progression (arithmetic progression/geometric progression) and the next successive member of a given three successive members of a sequence. Go to the editor
According to Wikipedia, an arithmetic progression (AP) is a sequence of numbers such that the difference of any two successive members of the sequence is a constant. For instance, the sequence 3, 5, 7, 9, 11, 13, . . . is an arithmetic progression with common difference 2. For this problem, we will limit ourselves to arithmetic progression whose common difference is a non-zero integer.
On the other hand, a geometric progression (GP) is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, . . . is a geometric progression with common ratio 3. For this problem, we will limit ourselves to geometric progression whose common ratio is a non-zero integer.
Click me to see the sample solution

28. Write a Python program to print the length of the series and the series from the given 3rd term, 3rd last term and the sum of a series. Go to the editor
Input data:
3rd term - 3
3rd last term - 118 55
Sum of the series - 91
Click me to see the sample solution

29. Write a Python program to find common divisors between two numbers in a given pair. Go to the editor
Click me to see the sample solution

30. Write a Python program to reverse the digits of a given number and add it to the original, If the sum is not a palindrome repeat this procedure. Go to the editor
Note: A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar.
Click me to see the sample solution

31. Write a Python program to count the number of carry operations for each of a set of addition problems. Go to the editor
According to Wikipedia " In elementary arithmetic, a carry is a digit that is transferred from one column of digits to another column of more significant digits. It is part of the standard algorithm to add numbers together by starting with the rightmost digits and working to the left. For example, when 6 and 7 are added to make 13, the "3" is written to the same column and the "1" is carried to the left".
Click me to see the sample solution

32. Write a python program to find heights of the top three building in descending order from eight given buildings. Go to the editor
Input:
0 = height of building (integer) = 10,000
Input the heights of eight buildings:
25
35
15
16
30
45
37
39
Heights of the top three buildings:
45
39
37
Click me to see the sample solution

33. Write a Python program to compute the digit number of sum of two given integers. Go to the editor
Input:
Each test case consists of two non-negative integers x and y which are separated by a space in a line.
0 = x, y = 1,000,000
Input two integers(a b):
5 7
Sum of two integers a and b.:
2
Click me to see the sample solution

34. Write a Python program to check whether three given lengths (integers) of three sides form a right triangle. Print "Yes" if the given sides form a right triangle otherwise print "No". Go to the editor
Input:
Integers separated by a single space.
1 = length of the side = 1,000
Input three integers(sides of a triangle)
8 6 7
No
Click me to see the sample solution

35. Write a Python program which solve the equation: Go to the editor
ax+by=c
dx+ey=f
Print the values of x, y where a, b, c, d, e and f are given.
Input:
a,b,c,d,e,f separated by a single space.
(-1,000 = a,b,c,d,e,f = 1,000)
Input the value of a, b, c, d, e, f:
5 8 6 7 9 4
Values of x and y:
-2.000 2.000
Click me to see the sample solution

36. Write a Python program to compute the amount of the debt in n months. The borrowing amount is $100,000 and the loan adds 5% interest of the debt and rounds it to the nearest 1,000 above month by month. Go to the editor
Input:
An integer n (0 = n = 100)
Input number of months:
7 Amount of debt: $144000
Click me to see the sample solution

37. Write a Python program which reads an integer n and find the number of combinations of a,b,c and d (0 = a,b,c,d = 9) where (a + b + c + d) will be equal to n. Go to the editor
Input:
n (1 = n = 50)
Input the number(n):
15
Number of combinations: 592
Click me to see the sample solution

38. Write a Python program to print the number of prime numbers which are less than or equal to an given integer. Go to the editor
Input:
n (1 = n = 999,999)
Input the number(n):
35
Number of prime numbers which are less than or equal to n.: 11
Click me to see the sample solution

39. Write a program to compute the radius and the central coordinate (x, y) of a circle which is constructed by three given points on the plane surface. Go to the editor
Input:
x1, y1, x2, y2, x3, y3 separated by a single space.
Input three coordinate of the circle:
9 3 6 8 3 6
Radius of the said circle:
3.358
Central coordinate (x, y) of the circle:
6.071 4.643
Click me to see the sample solution

40. Write a Python program to check if a point (x,y) is in a triangle or not. There is a triangle formed by three points. Go to the editor
Input:
x1,y1,x2,y2,x3,y3,xp,yp separated by a single space.
Input three coordinate of the circle:
9 3 6 8 3 6
Radius of the said circle:
3.358
Central coordinate (x, y) of the circle:
6.071 4.643
Click me to see the sample solution

41. Write a Python program to compute and print sum of two given integers (more than or equal to zero). If given integers or the sum have more than 80 digits, print "overflow". Go to the editor
Input first integer:
25
Input second integer:
22
Sum of the two integers: 47
Click me to see the sample solution

42. Write a Python program that accepts six numbers as input and sorts them in descending order. Go to the editor
Input:
Input consists of six numbers n1, n2, n3, n4, n5, n6 (-100000 = n1, n2, n3, n4, n5, n6 = 100000). The six numbers are separated by a space.
Input six integers:
15 30 25 14 35 40
After sorting the said integers:
40 35 30 25 15 14
Click me to see the sample solution

43. Write a Python program to test whether two lines PQ and RS are parallel. The four points are P(x1, y1), Q(x2, y2), R(x3, y3), S(x4, y4). Go to the editor
Input:
x1,y1,x2,y2,x3,y3,xp,yp separated by a single space
Input x1,y1,x2,y2,x3,y3,xp,yp:
2 5 6 4 8 3 9 7
PQ and RS are not parallel
Click me to see the sample solution

44. Write a Python program to find the maximum sum of a contiguous subsequence from a given sequence of numbers a1, a2, a3, ... an. A subsequence of one element is also a continuous subsequence. Go to the editor
Input:
You can assume that 1 = n = 5000 and -100000 = ai = 100000.
Input numbers are separated by a space.
Input 0 to exit.
Input number of sequence of numbers you want to input (0 to exit):
3
Input numbers:
2
4
6
Maximum sum of the said contiguous subsequence:
12 Input number of sequence of numbers you want to input (0 to exit):
0
Click me to see the sample solution

45. There are two circles C1 with radius r1, central coordinate (x1, y1) and C2 with radius r2 and central coordinate (x2, y2). Go to the editor

Write a Python program to test the followings -

  • "C2 is in C1" if C2 is in C1
  • "C1 is in C2" if C1 is in C2
  • "Circumference of C1 and C2 intersect" if circumference of C1 and C2 intersect, and
  • "C1 and C2 do not overlap" if C1 and C2 do not overlap.
Input:
Input numbers (real numbers) are separated by a space.
Input x1, y1, r1, x2, y2, r2:
5 6 4 8 7 9
C1 is in C2
Click me to see the sample solution

46. Write a Python program to that reads a date (from 2016/1/1 to 2016/12/31) and prints the day of the date. Jan. 1, 2016, is Friday. Note that 2016 is a leap year. Go to the editor
Input:
Two integers m and d separated by a single space in a line, m ,d represent the month and the day.
Input month and date (separated by a single space):
5 15
Name of the date: Sunday
Click me to see the sample solution

47. Write a Python program which reads a text (only alphabetical characters and spaces.) and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters. Go to the editor

Note: A word is a sequence of letters which is separated by the spaces.

Input:
A text is given in a line with following condition:
a. The number of letters in the text is less than or equal to 1000.
b. The number of letters in a word is less than or equal to 32.
c. There is only one word which is arise most frequently in given text.
d. There is only one word which has the maximum number of letters in given text.
Input text: Thank you for your comment and your participation.
Output: your participation.
Click me to see the sample solution

48. Write a Python program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination. Go to the editor
Input:
Two integers as number of combinations and their sum by a single space in a line. Input 0 0 to exit.
Input number of combinations and sum, input 0 0 to exit:
5 6
2 4
0 0
2
Click me to see the sample solution

49. Write a Python program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus. Go to the editor
According to Wikipedia-
parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-intersecting) quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure.
rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as an equiangular quadrilateral, since equiangular means that all of its angles are equal (360°/4 = 90°). It can also be defined as a parallelogram containing a right angle.
rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a simple (non-self-intersecting) quadrilateral whose four sides all have the same length. Another name is equilateral quadrilateral, since equilateral means that all of its sides are equal in length. The rhombus is often called a diamond, after the diamonds suit in playing cards which resembles the projection of an octahedral diamond, or a lozenge, though the former sometimes refers specifically to a rhombus with a 60° angle, and the latter sometimes refers specifically to a rhombus with a 45° angle.
Input:
Two adjoined sides and the diagonal.
1 = ai, bi, ci = 1000, ai + bi > ci
Input two adjoined sides and the diagonal of a parallelogram (comma separated):
3,4,5
This is a rectangle.
Click me to see the sample solution

50. Write a Python program to replace a string "Python" with "Java" and "Java" with "Python" in a given string. Go to the editor
Input:
English letters (including single byte alphanumeric characters, blanks, symbols) are given on one line. The length of the input character string is 1000 or less.
Input a text with two words 'Python' and 'Java'
Python is popular than Java
Java is popular than Python
Click me to see the sample solution

51. Write a Python program to find the difference between the largest integer and the smallest integer which are created by 8 numbers from 0 to 9. The number that can be rearranged shall start with 0 as in 00135668. Go to the editor
Input:
Input an integer created by 8 numbers from 0 to 9.:
2345
Difference between the largest and the smallest integer from the given integer:
3087
Click me to see the sample solution

52. Write a Python program to compute the sum of first n given prime numbers. Go to the editor
Input:
n ( n = 10000). Input 0 to exit the program.
Input a number (n=10000) to compute the sum:(0 to exit)
25
Sum of first 25 prime numbers:
1060
Click me to see the sample solution

53. Write a Python program that accept a even number (>=4, Goldbach number) from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations. Go to the editor
Goldbach number: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes.[4] Since four is the only even number greater than two that requires the even prime 2 in order to be written as the sum of two primes, another form of the statement of Goldbach's conjecture is that all even integers greater than 4 are Goldbach numbers.
The expression of a given even number as a sum of two primes is called a Goldbach partition of that number. The following are examples of Goldbach partitions for some even numbers:
6 = 3 + 3
8 = 3 + 5
10 = 3 + 7 = 5 + 5
12 = 7 + 5
...
100 = 3 + 97 = 11 + 89 = 17 + 83 = 29 + 71 = 41 + 59 = 47 + 53
Input an even number (0 to exit):
100
Number of combinations:
6
Click me to see the sample solution

54. if you draw a straight line on a plane, the plane is divided into two regions. For example, if you pull two straight lines in parallel, you get three areas, and if you draw vertically one to the other you get 4 areas.
Write a Python program to create maximum number of regions obtained by drawing n given straight lines. Go to the editor
Input:
(1 = n = 10,000)
Input number of straight lines (o to exit):
5
Number of regions:
16
Click me to see the sample solution

55. There are four different points on a plane, P(xp,yp), Q(xq, yq), R(xr, yr) and S(xs, ys). Write a Python program to test AB and CD are orthogonal or not. Go to the editor
Input:
xp,yp, xq, yq, xr, yr, xs and ys are -100 to 100 respectively and each value can be up to 5 digits after the decimal point It is given as a real number including the number of. Output:
Output AB and CD are not orthogonal! or AB and CD are orthogonal!.
Click me to see the sample solution

56. Write a Python program to sum of all numerical values (positive integers) embedded in a sentence.
Write a Python program to create maximum number of regions obtained by drawing n given straight lines. Go to the editor
Input:
Sentences with positive integers are given over multiple lines. Each line is a character string containing one-byte alphanumeric characters, symbols, spaces, or an empty line. However the input is 80 characters or less per line and the sum is 10,000 or less.
Input some text and numeric values ( to exit):
Sum of the numeric values: 80
None
Input some text and numeric values ( to exit):
Sum of the numeric values: 17
None
Input some text and numeric values ( to exit):
Sum of the numeric values: 10
None
Click me to see the sample solution

57. There are 10 vertical and horizontal squares on a plane. Each square is painted blue and green. Blue represents the sea, and green represents the land. When two green squares are in contact with the top and bottom, or right and left, they are said to be ground. The area created by only one green square is called "island". For example, there are five islands in the figure below.
Write a Python program to read the mass data and find the number of islands. Go to the editor
Input:
Input 10 rows of 10 numbers representing green squares (island) as 1 and blue squares (sea) as zeros
1100000111
1000000111
0000000111
0010001000
0000011100
0000111110
0001111111
1000111110
1100011100
1110001000
Number of islands:
5
Click me to see the sample solution

58. When character are consecutive in a string , it is possible to shorten the character string by replacing the character with a certain rule. For example, in the case of the character string YYYYY, if it is expressed as # 5 Y, it is compressed by one character.
Write a Python program to restore the original string by entering the compressed string with this rule. However, the # character does not appear in the restored character string. Go to the editor
Note: The original sentences are uppercase letters, lowercase letters, numbers, symbols, less than 100 letters, and consecutive letters are not more than 9 letters.
Input:
The restored character string for each character on one line.
Original text: XY#6Z1#4023
XYZZZZZZ1000023
Original text: #39+1=1#30
999+1=1000
Click me to see the sample solution

59. A convex polygon is a simple polygon in which no line segment between two points on the boundary ever goes outside the polygon. Equivalently, it is a simple polygon whose interior is a convex set. In a convex polygon, all interior angles are less than or equal to 180 degrees, while in a strictly convex polygon all interior angles are strictly less than 180 degrees.
Write a Python program that compute the area of the polygon . The vertices have the names vertex 1, vertex 2, vertex 3, ... vertex n according to the order of edge connections Go to the editor
Note: The original sentences are uppercase letters, lowercase letters, numbers, symbols, less than 100 letters, and consecutive letters are not more than 9 letters.
Input:
Input is given in the following format.
x1 , y1
x2 , y2
:
xn , yn
xi , yi are real numbers representing the x and y coordinates of vertex i , respectively.
Input the coordinates (ctrl+d to exit):
1.0, 0.0
0.0, 0.0
1.0, 1.0
2.0, 0.0
-1.0, 1.0
Area of the polygon;
1.50000000.
Click me to see the sample solution

60. Internet search engine giant, such as Google accepts web pages around the world and classify them, creating a huge database. The search engines also analyze the search keywords entered by the user and create inquiries for database search. In both cases, complicated processing is carried out in order to realize efficient retrieval, but basics are all cutting out words from sentences.
Write a Python program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters. Go to the editor
Input:
English sentences consisting of delimiters and alphanumeric characters are given on one line.
Input a sentence (1024 characters. max.)
The quick brown fox
3 to 6 characters length of words:
The quick brown fox
Click me to see the sample solution

61. Arrange integers (0 to 99) as narrow hilltop, as illustrated in Figure 1. Reading such data representing huge, when starting from the top and proceeding according to the next rule to the bottom. Write a Python program that compute the maximum value of the sum of the passing integers. Go to the editor
Input:
A series of integers separated by commas are given in diamonds. No spaces are included in each line. The input example corresponds to Figure 1. The number of lines of data is less than 100 lines.
Output:
The maximum value of the sum of integers passing according to the rule on one line.
Input the numbers (ctrl+d to exit):
8
4, 9
9, 2, 1
3, 8, 5, 5
5, 6, 3, 7, 6
3, 8, 5, 5
9, 2, 1
4, 9
8
Maximum value of the sum of integers passing according to the rule on one line.
64
Click me to see the sample solution

62. Write a Python program to find the number of combinations that satisfy p + q + r + s = n where n is a given number <= 4000 and p, q, r, s in the range of 0 to 1000. Go to the editor
Input a positive integer: (ctrl+d to exit)
252
Number of combinations of a,b,c,d: 2731135
Click me to see the sample solution

63. Write a Python program which adds up columns and rows of given table as shown in the specified figure. Go to the editor
Input number of rows/columns (0 to exit)
4
Input cell value:
25 69 51 26
68 35 29 54
54 57 45 63
61 68 47 59
Result:
25 69 51 26 171
68 35 29 54 186
54 57 45 63 219
61 68 47 59 235
208 229 172 202 811
Input number of rows/columns (0 to exit)
Click me to see the sample solution

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



New Content: Composer: Dependency manager for PHP, R Programming