Problem set Python-programming_240119_141957
Problem set Python-programming_240119_141957
Md Kutubuddin Sardar
Senior Research Fellow,
Department of Pure Mathematics,
Calcutta University, Kolkata
Email id: kutub.pmath@gmail.com
March 5, 2022
1 Exercises
1. Write a Python program that can swap to integers.
2. Using exactly two variables, write a Python program that can swap two integers.
3. Given the lengths of the straight lines, write a Python program that can check whether
(a) the three straight lines form a triangle or not.
(b) the three straight lines form an equilateral triangle or not.
(c) the three straight lines form an isosceles triangle or not.
4. Given the co-ordinates for the three points A, B and C as (a1, b1), (a2, b2) and (a3, b3) ∈
R2 , respectively, write a Python program that can
(a) find area of the triangle ABC.
(b) check whether the triangle ABC is a right angle triangle or not.
5. Given n integers, write a Python program that can find exact average (in float) and
integer average of the n numbers.
1
1 2
6. For given n, write a Python program that can print
···
1 2 ··· n
1
7. Given n, write a Python program that can compute factorial of n.
10. Write a Python program that can find the gcd of two integers.
11. Write a Python program that can find lcm of two integers.
12. Given n write a Python program that can print first n terms of F ibonacci series. Also
compute the sum of the series.
13. Given n, write a Python program that can find the pascal triangle.
14. Write a Python program that can check whether a given integer is prime or not.
15. Given n, write a Python program that can compute the sum 1 + (1 + 2) + · · · + (1 +
2 + · · · + n). Also print 1 + (1 + 2) + · · · + (1 + 2 + · · · + n) = sum
18. Given n, write a Python program that can find all positive factors of n.
19. Given n, write a Python program that can find all prime factors of n.
20. Given n > 1, write a Python program that can represent n as pα1 1 · pα2 2 · · · pαk k , where
pi s are distinct primes and αi s are positive integers, for i = 1, 2, . . . , k.
21. Given a positive integer n, write a Python program that can find the set Z∗n = {x ∈
Zn : gcd(x, n) = 1}.
23. Given n integers, n < 100, using Python programming, write down the integers in
ascending order.
24. Given marks of n subjects for m students, write a Python program that can find
average mark of each student and find average mark of each subject.
25. Write a Python program that can compute the addition of two matrices, report if not
possible.
2
26. Write a Python program that can compute the multiplication of two matrices, report
if not possible.
27. Write a Python program that can compute the transpose of a given matrix.
28. Write a Python program that can check whether a given matrix is symmetric or not.
29. Write a Python program that can check whether a given matrix is skew-symmetric
or not.
30. Write a Python program that can demonstrate that every square matrix (over R) can
be written as sum of symmetric matrix and skew-symmetric matrix.
31. Write a Python program that can find the trace of a given matrix.
32. Write a Python program that can find the sum of cross-diagonal elements of a given
matrix.
33. Write a Python program that can find the inverse of a given matrix, if it exists.
34. Given an 3-digit integer, write a Python program that can find the reverse integer, i.e.,
if the integer is abc, then the program will output cba.
35. Write a Python program hat can transform a decimal positive integer into binary
representation.
36. Write a Python program that can transform a decimal positive integer into octal rep-
resentation.
37. Write a Python program that can transform a binary representation into decimal
representation.
38. Given a positive integer n, write a Python program that can print all subsets of the
set {1, 2, . . . , n}.
39. Given a positive integer n, write a Python program that can print all subsets of odd
order of the set {1, 2, . . . , n}.
40. Given a positive integer n, write a Python program that can print all subsets of even
order of the set {1, 2, . . . , n}.
3
42. Given an interval [a, b], write a Python program that can find all perfect numbers
within the interval [a, b]. If there exists no such perfect number, report that there
exists no perfect number in the interval [a, b].
43. Given n, write a Python program (using function) that can compute factorial of n.
44. Given an interval [a, b], write a Python program using function that can find all primes
within the interval [a, b]. If there exists no such prime, report that there exists no prime
in the interval [a, b].
45. Write a Python program (using function) that can find the value of n Pr .
46. Write a Python program (using function) that can find the value of n Cr .
47. Write a Python program (using function) that can find the gcd of two integers.
48. Write a Python program (using function) that can find lcm of two integers.
49. Write a Python program (using function and pointer) that can swap to integers.
50. Write a Python program (using function) that can check whether a given integer is
prime or not.
51. Write a Python program (using function) that can transform a decimal positive integer
into binary representation.
52. Write a Python program (using function) that can transform a decimal positive integer
into octal representation.
53. Write a Python program (using function) that can transform a binary representation
into decimal representation.
54. Write a Python program that can count the number of characters in a given string of
input.
55. Write a Python program that can count the number of consonants and vowels in a
given string of input.
57. Write a Python program that can check whether a given string of input is palindrome
or not.
58. Write a Python program that can change the lower case alphabet into upper case
alphabet.
4
59. Write a Python program that can change the upper case alphabet into lower case
alphabet.
60. Write a Python program that can change the all lower case alphabet into upper case
alphabet and all upper case alphabet into lower case alphabet in a given string of
alphabets.
61. Write a Python program that takes a sentence as input and outputs a sentence from
backward direction. For example, input: “I am a good boy”; output: “boy good a am
I”.
62. Given n integers, using dynamic memory allocation write a Python program that can
output the integers in ascending order.
63. Given marks of n subjects for m students, write a Python program (using dynamic
memory allocation) that can find average mark of each student and find average mark
of each subject.
64. Write a Python program (using dynamic memory allocation) that can compute the
addition of two matrices, report if not possible.
65. Write a Python program (using dynamic memory allocation) that can compute the
multiplication of two matrices, report if not possible.
66. Write a Python program (using dynamic memory allocation) that can compute the
transpose of a given matrix.
67. Write a Python program (using dynamic memory allocation) that can check whether
a given matrix is symmetric or not.
68. Write a Python program (using dynamic memory allocation) that can check whether
a given matrix is skew-symmetric or not.
69. Write a Python program (using dynamic memory allocation) that can demonstrate
that every square matrix (over R) can be written as sum of symmetric matrix and
skew-symmetric matrix.
70. Write a Python program (using dynamic memory allocation) that can find the trace
of a given matrix.
71. Write a Python program (using dynamic memory allocation) that can find the sum of
cross-diagonal elements of a given matrix.
5
72. Write a Python program (using dynamic memory allocation) that can find the inverse
of a given matrix, if it exists.
73. Given a positive integer n, write a Python program (using dynamic memory allocation)
that can construct a Latin square of order n.
74. Given an odd positive integer n, write a Python program (using dynamic memory
allocation) that can construct a Latin square of order n in which all the diagonal
elements are distinct.
75. Given an odd positive integer n, write a Python program (using dynamic memory
allocation) that can construct a Latin square of order n in which all the diagonal
elements are in ascending order.
76. Write a Python program (using function and dynamic memory allocation) in which
given n integers will be arranged in ascending order.
77. Write a Python program that can print in the terminal the program itself.
78. Write a Python program that can find the number of alphabets in a given file stored
in the same folder as the program itself.
79. Write a Python program that can read an input n (the order of a latin square) from
a given file stored in the same folder as the program itself and write the output (latin
square of order n) in the same file.
83. Given n, write a Python program (using function) that can compute factorial of n and
store the output in a file.
84. Write a Python program (using function) that can find the value of n Pr and store the
output in a file.
85. Write a Python program (using function) that can find the value of n Cr and store the
output in a file.
86. Write a Python program (using function) that can find the gcd of two integers and
store the output in a file.
6
87. Write a Python program (using function) that can find lcm of two integers and store
the output in a file.
88. Write a Python program (using function and pointer) that can swap to integers and
store the output in a file.
89. Write a Python program (using function) that can check whether a given integer is
prime or not and store the output in a file.
90. Given n, write a Python program (using gcd function) that can find ϕ(n) find σ(n)
and store the output in a file.
91. Given n integers, using dynamic memory allocation write a Python program that can
output the integers in ascending order and store the output in a file.
92. Given marks of n subjects for m students, write a Python program (using dynamic
memory allocation) that can find average mark of each student and find average mark
of each subject and store the output in a file.
93. Write a Python program (using dynamic memory allocation) that can compute the
addition of two matrices, report if not possible and store the output in a file.
94. Write a Python program (using dynamic memory allocation) that can compute the
multiplication of two matrices, report if not possible and store the output in a file.
95. Write a Python program (using dynamic memory allocation) that can compute the
transpose of a given matrix and store the output in a file.
96. Write a Python program (using dynamic memory allocation) that can check whether
a given matrix is symmetric or not and store the output in a file.
97. Write a Python program (using dynamic memory allocation) that can check whether
a given matrix is skew-symmetric or not and store the output in a file.
98. Write a Python program (using dynamic memory allocation) that can demonstrate
that every square matrix (over R) can be written as sum of symmetric matrix and
skew-symmetric matrix and store the output in a file.
99. Write a Python program (using dynamic memory allocation) that can find the trace
of a given matrix and store the output in a file.
100. Write a Python program (using dynamic memory allocation) that can find the sum of
cross-diagonal elements of a given matrix and store the output in a file.
7
101. Write a Python program (using dynamic memory allocation) that can find the inverse
of a given matrix, if it exists and store the output in a file.
102. Given a positive integer n, write a Python program (using dynamic memory allocation)
that can construct a Latin square of order n and store the output in a file.
103. Given an odd positive integer n, write a Python program (using dynamic memory
allocation) that can construct a Latin square of order n in which all the diagonal
elements are distinct and store the output in a file.
104. Given an odd positive integer n, write a Python program (using dynamic memory
allocation) that can construct a Latin square of order n in which all the diagonal
elements are in ascending order and store the output in a file.
105. Write a Python program that can transform a decimal positive integer into binary
representation and store the output in a file.
106. Write a Python program that can transform a decimal positive integer into octal rep-
resentation and store the output in a file.
107. Write a Python program that can transform a binary representation into decimal
representation and store the output in a file.
108. Write a Python program (using function and dynamic memory allocation) in which
given n integers will be arranged in ascending order and store the output in a file.
109. Write a Python program that can count the number of characters in a given string of
input and store the output in a file.
110. Write a Python program that can count the number of consonants and vowels in a
given string of input and store the output in a file.
111. Write a Python program that is capable of doing name abbreviation and store the
output in a file.
112. Write a Python program that can check whether a given string of input is palindrome
or not and store the output in a file.
113. Write a Python program that can change the lower case alphabet into upper case
alphabet and store the output in a file.
114. Write a Python program that can change the upper case alphabet into lower case
alphabet and store the output in a file.
8
115. Write a Python program that can change the all lower case alphabet into upper case
alphabet and all upper case alphabet into lower case alphabet in a given string of
alphabets and store the output in a file.
116. Write a Python program that can print in the terminal the program itself and also
store the output in a file.
117. Write a Python program that can find the number of alphabets in a given file stored
in the same folder as the program itself and store the output in a file.
118. Write a Python program using break and store the output in a file.
119. Write a Python program using continue and store the output in a file.
120. Write a Python program using switch-case and store the output in a file.
2 Exercises
1. Write a Python program to print the following string in a specific format (see the
output)