Programming Problems

22 minute read

In the following programs, try to solve each one of them using OOP methods and prefer taking user input instead of using hardcoded values. Try to use the entire concepts that you have learnt from the book. Obviously, we are not going to provide the solutions because we want to come up with the solution by yourself, that’s the best programming practice.

  1. Write a python program to add two numbers using functions.

  2. Write a python program to find the factorial of a number.

  3. Write a python program to calculate simple interest.

  4. Write a python program to calculate compound interest.

  5. Given a number x, determine whether the given number is Armstrong number or not. A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if.

    \[abcd…. = a^n + b^n + c^n + d^n + ………\]

  6. Write a python program to find the area of a circle.

  7. Write a python script to print all prime numbers in an interval.

  8. Write a program to print ASCII value of a character.

  9. Write a program for the sum of squares of the first n natural number.

  10. Write a program for the sum of cubes of first n natural numbers.

  11. Write a python program to interchange first and last elements in a list.

  12. Write a python program to swap two elements in a list.

  13. Write a python program to find N largest elements from a list.

  14. Write a python program to print even numbers in a list.

  15. Write a python program to print odd numbers in a list.

  16. Write a python program to add two matrices.

  17. Write a python program to multiply two matrices.

  18. Write a python program to find the transpose of a matrix.

  19. Write a python program to get the Kth column of the matrix.

  20. Write a python program to check if a string is palindromic or not.

  21. Write a python program to reverse the words in a given string.

  22. Write a python program to convert from snake case to camel and pascal case.

  23. Write a python program to print event length words in the string.

  24. Write a python program to accept the string which contains all vowels.

  25. Write a python program to remove all duplicates from the string.

  26. Write a Python program to read file word by word.

  27. Write a Python program to read character by character from a file.

  28. Write a Python program to get number of characters, words, spaces and lines in a file.

  29. Write a Python program to Count the Number of occurrences of a key-value pair in a text file.

  30. Write a Python Program to obtain the line number in which given word is present.

  31. Write a Python program to count number of lines in a text file in Python.

  32. Write a Python Program to remove lines starting with any prefix.

  33. Write a Python Program to Eliminate repeated lines from a file.

  34. Write a Python Program to read List of Dictionaries from file.

  35. Write a python program to Append content of one text file to another.

  36. Write a Python program to copy odd lines of one file to other.

  37. Write a Python Program to merge two files into a third file.

  38. Write a Python program to Reverse a single line of a text file.

  39. Write a Python program to reverse the content of a file and store it in another file.

  40. Write a Python program to create a set.

  41. Write a Python program to iterate over sets.

  42. Write a Python program to add member(s) in a set.

  43. Write a Python program to remove item(s) from set

  44. Write a Python program to remove an item from a set if it is present in the set.

  45. Write a Python program to create an intersection of sets.

  46. Write a Python program to create a union of sets.

  47. Write a Python program to create set difference.

  48. Write a Python program to create a symmetric difference.

  49. Write a Python program to check if a set is a subset of another set.

  50. Write a Python program to create a shallow copy of sets. (Note : Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object.)

  51. Write a Python program to clear a set.

  52. Write a Python program to use of frozensets (Note: Frozensets behave just like sets except they are immutable.)

  53. Write a Python program to find maximum and the minimum value in a set.

  54. Write a Python program to find the length of a set.

  55. Write a Python program to check if a given value is present in a set or not.

  56. Write a Python program to check if two given sets have no elements in common.

  57. Write a Python program to check if a given set is superset of itself and superset of another given set.

  58. Write a Python program to find the elements in a given set that are not in another set.

  59. Write a Python program to check a given set has no elements in common with other given set

  60. Write a Python program to remove the intersection of a 2nd set from the 1st set.

  61. Write a Python function that takes a sequence of numbers and determines whether all the numbers are different from each other.

  62. Write a Python program to create all possible strings by using ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. Use the characters exactly once.

  63. Write a Python program to remove and print every third number from a list of numbers until the list becomes empty.

  64. Write a Python program to find unique triplets whose three elements gives the sum of zero from an array of n integers.

  65. Write a Python program to create the combinations of 3 digit combo.

  66. Write a Python program to print a long text, convert the string to a list and print all the words and their frequencies.

  67. Write a Python program to count the number of each character of a given text of a text file

  68. Write a Python program to get a list of locally installed Python modules.

  69. Write a Python program to display some information about the OS where the script is running.

  70. 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.

    Sample data:
        
    X = [10, 20, 20, 20]
    Y = [10, 20, 30, 40]
    Z = [10, 30, 40, 20]
    target = 70
    
  71. Write a Python program to create all possible permutations from a given collection of distinct numbers.

  72. Write a Python program to get all possible two digit letter combinations from a digit (1 to 9) string.

    string_maps = 
    {
    "1": "abc",
    "2": "def",
    "3": "ghi",
    "4": "jkl",
    "5": "mno",
    "6": "pqrs",
    "7": "tuv",
    "8": "wxy",
    "9": "z"
    }
    
  73. Write a Python program to add two positive integers without using the ‘+’ operator (Note: Use bit wise operations to add two numbers.)

  74. Write a Python program to check the priority of the four operators (+, -, *, /).

  75. Write a Python program to get the third side of right angled triangle from two given sides.

  76. Write a Python program to get all strobogrammatic numbers that are of length n.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']
    
  77. Write a Python program to find the median among three given numbers.

  78. Write a Python program to find the value of n where n degrees of number 2 are written sequentially in a line without spaces.

  79. Write a Python program to find the number of zeros at the end of a factorial of a given positive number. Range of the number(n): (\(1 <= n <= 2*109\)).

  80. 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.

  81. 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.

  82. Write a Python program to find the number of divisors of a given integer is even or odd.

  83. Write a Python program to find the digits which are absent in a given mobile number.

  84. Write a Python program to compute the summation of the absolute difference of all distinct pairs in an given array (non-decreasing order).

    Sample array: [1, 2, 3]
    Then all the distinct pairs will be:
    1 2
    1 3
    2 3
    
  85. 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. 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.

  86. 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.

    Sample Data:
    Input third term of the series: 3
    Input 3rd last term: 3
    Sum of the series: 15
    Length of the series: 5
    Series:
    1 2 3 4 5
    
  87. Write a Python program to find common divisors between two numbers in a given pair.

  88. Write a Python program to count the number of carry operations for each of a set of addition problems. 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”.

  89. Write a python program to find heights of the top three building in descending order from eight given buildings.

    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
    
  90. Write a Python program which solve the equation

    \[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
    
  91. 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.

    Input:
    An integer n (0 <= n <= 100)
    Input number of months: 7
    Amount of debt: $144000
    
  92. 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.

    Input:
    n (1 <= n <= 50)
    Input the number(n): 15
    Number of combinations: 592
    
  93. 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.

    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
    
  94. Write a Python program to check whether a point (x,y) is in a triangle or not. There is a triangle formed by three points.

    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
    
  95. 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”.

    input first integer:
    25
    Input second integer:
    22
    Sum of the two integers: 47
    
  96. Write a Python program that accepts six numbers as input and sorts them in descending order.

    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
    
  97. 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).

    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
    
  98. 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.

    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
    
  99. There are two circles C1 with radius r1, central coordinate (x1, y1) and C2 with radius r2 and central coordinate (x2, y2).

    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
    
  100. 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.

    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.
    
  101. 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.

    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
    
  102. 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.

    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.
    
  103. Write a Python program to replace a string “Python” with “Java” and “Java” with “Python” in a given string.

    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
    
  104. 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.

    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
    
  105. Write a Python program to compute the sum of first n given prime numbers.

  106. 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.

    Input:
    (1 <= n <= 10,000)
    Input number of straight lines (o to exit):
    5
    Number of regions:
    16
    
  107. 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.

    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!.
    
  108. 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.

    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
    
  109. 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.

    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
    
  110. 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. 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
    
  111. 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

    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.
    
  112. 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.

    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
    
  113. 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.

    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
    
  114. Given a list of numbers and a number k, write a Python program to check whether the sum of any two numbers from the list is equal to k or not.

    For example, given [1, 5, 11, 5] and k = 16, return true since 11 + 5 is 16.

    Sample Input:
    ([12, 5, 0, 5], 10)
    ([20, 20, 4, 5], 40)
    ([1, -1], 0)
    ([1, 1, 0], 0)
    Sample Output:
    True
    True
    True
    False
    
  115. Write a Python program to find the longest common prefix string amongst a given array of strings. Return false If there is no common prefix.

    For Example, longest common prefix of “abcdefgh” and “abcefgh” is “abc”.

    Sample Input:
    ["abcdefgh","abcefgh"]
    ["w3r","w3resource"]
    ["Python","PHP", "Perl"]
    ["Python","PHP", "Java"]
    Sample Output:
    abc
    w3r
    P
    
  116. Write a Python program to reverse only the vowels of a given string.

    Sample Input:
    ("w3resource")
    ("Python")
    ("Perl")
    ("USA")
    Sample Output:
    w3resuorce
    Python
    Perl
    ASU
    
  117. Write a Python program to calculate the maximum profit from selling and buying values of stock. An array of numbers represent the stock prices in chronological order.

    For example, given [8, 10, 7, 5, 7, 15], the function will return 10, since the buying value of the stock is 5 dollars and sell value is 15 dollars.

    Sample Input:
    ([8, 10, 7, 5, 7, 15])
    ([1, 2, 8, 1])
    ([])
    Sample Output:
    10
    7
    0
    
  118. Write a Python program to remove all instances of a given value from a given array of integers and find the length of the new array.

    Sample Input:
    ([1, 2, 3, 4, 5, 6, 7, 5], 5)
    ([10,10,10,10,10], 10)
    ([10,10,10,10,10], 20)
    ([], 1)
    Sample Output:
    6
    0
    5
    0
    
  119. Write a Python program to find the starting and ending position of a given value in a given array of integers, sorted in ascending order.

    If the target is not found in the array, return [0, 0].

    Input: [5, 7, 7, 8, 8, 8] target value = 8
    Output: [0, 5]
    Input: [1, 3, 6, 9, 13, 14] target value = 4
    Output: [0, 0]
    
  120. Write a Python program that accept two strings and test if the letters in the second string are present in the first string.

    Sample Input:
    ["python", "ypth"]
    ["python", "ypths"]
    ["python", "ypthon"]
    ["123456", "01234"]
    ["123456", "1234"]
    Sample Output:
    True
    False
    True
    False
    True
    

Try Something New

Print the following patterns for the given number of rows N,

  • ****
    ***
    **
    *
    
  • ****
    ***     
      **
      *
    
  • A
    BB
    CCC
    
  • 1111
    000
    11
    0
    
  • E
    DE
    CDE
    BCDE
    ABCDE
    
  • 1 
    3 2 
    4 5 6 
    10 9 8 7 
    11 12 13 14 15
    
  • ABCCBA
    ABBA
      AA
    
  • 333
    233
    123
    
  • 0
    101
    21012
    
  • *
    **
    ***
    ****
    ***
    **
    *
    
  •     1
      123
      12345
    1234567
      12345
      123
        1
    
  • 1
        232
      34543
    4567654
    567898765
    
  • *
    ***
    *****
    ***
      *
    
  • ``` *
    • *


    • * * ```
  • 4444444
    4333334
    4322234
    4321234
    4322234
    4333334  
    4444444
    
  • ``` *** ** ** ** ** ** **
    • * ** ** * *** ** ** ******* ```
  • 1       1
    2     2
      3   3
      4 4
        5
      4 4
      3   3
    2     2
    1       1
    
  • 1
    23 
    4567
    89123456
    7891234567891234
    
Reference

All the programming problems & their solutions, mentioned above are available on internet. It was taken from different programming contests and sites to maintain the current trend and standards of programming problems.

We are here to help ! Be sure to check our website and don’t hesitate to ask any questions on our community platform. We provide personal mentoring and teaching too, in order to upgrade your skills. Vist www.edualgoacademy.com to get started.

Spotted a bug ? Great job, you found a bug. Please report it to us in our mail and we’ll fix it as soon as possible.