0% found this document useful (0 votes)
3 views15 pages

Computer Programming (Python)

The document contains a list of 189 practical programming questions and tasks primarily focused on Python, covering various topics such as functions, data structures, file handling, and mathematical computations. Each question requires the implementation of specific functionalities, such as calculating sums, finding subarrays, and manipulating strings and lists. The tasks are designed to enhance programming skills and understanding of Python concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Computer Programming (Python)

The document contains a list of 189 practical programming questions and tasks primarily focused on Python, covering various topics such as functions, data structures, file handling, and mathematical computations. Each question requires the implementation of specific functionalities, such as calculating sums, finding subarrays, and manipulating strings and lists. The tasks are designed to enhance programming skills and understanding of Python concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSC202/CSC208 Practical 2 Questons

Instruction:
- Please ensure you do the question assigned to you. We are going to check when
grading you in the lab and during manual grading
- Write the question assigned to your Matric. No. in your Practical manual

1. Write a Python function to find the maximum sum of a circular subarray.

2. Write a Python function to find the length of the longest subarray with at most

two distinct elements.

3. Write a Python function to find the number of subarrays with an odd sum.

4. Write a Python function to find the longest subarray with a sum equal to zero.

5. Write a Python function to find the length of the longest subarray with at most

one odd number.

6. Write a Python function to find the length of the longest subarray with a given

product.

7. Write a Python function to find the number of subarrays with an even sum.

8. Write a Python function to find the longest subarray with a sum divisible by a

given value.

9. Write a Python function to find the length of the longest subarray with a given

XOR.

10. Write a Python function to find the number of subarrays with a given XOR.

11. Write a Python function to find the longest subarray with a sum of squares equal

to a given value.
12. Write a Python function to find the length of the longest subarray with a sum of

cubes equal to a given value.

13. Write a Python function to find the number of subarrays with a sum of fourth

powers equal to a given value.

14. Write a Python function to find the length of the longest subarray with a sum of

fifth powers equal to a given value.

15. Write a Python function to find the number of subarrays with a sum of factorials

equal to a given value.

16. Write a Python function to find the length of the longest subarray with a sum of

Fibonacci numbers equal to a given value.

17. Write a Python function to find the number of subarrays with a sum of Lucas

numbers equal to a given value.

18. Write a Python function to find the length of the longest subarray with a sum of

prime numbers equal to a given value.

19. Write a Python function to find the number of subarrays with a sum of composite

numbers equal to a given value.

20. Write a Python function to find the length of the longest subarray with a sum of

square-free numbers equal to a given value.

21. Write a Python function to find the number of subarrays with a sum of squareful

numbers equal to a given value.


22. Write a Python function to find the length of the longest subarray with a sum of

triangular numbers equal to a given value.

23. Write a Python function to find the number of subarrays with a sum of tetrahedral

numbers equal to a given value.

24. Write a Python function to find the length of the longest subarray with a sum of

pentagonal numbers equal to a given value.

25. Write a Python function to find the number of subarrays with a sum of hexagonal

numbers equal to a given value.

26. Write a Python function to find the length of the longest subarray with a sum of

heptagonal numbers equal to a given value.

27. Write a Python function to find the number of subarrays with a sum of octagonal

numbers equal to a given value.

28. Write a Python function that returns the cumulative sum of a list.

29. Ask the user for their full name and age. Print a greeting like: "Hello John Doe,

you are 25 years old."

30. Take two numbers as input. Print the result of all arithmetic operations (+, -, *, /,

%, **).

31. Write a program that takes a float input, converts it to integer, and explains the

loss of precision.
32. Swap two variables using only arithmetic operations (no third variable, no tuple

swap).

33. Take an integer and print whether it’s odd, even, or a multiple of both 2 and 3.

34. Take a price input (float) and apply a 15% discount. Print final price.

35. Accept height (in cm) and weight (in kg) as input. Calculate and print the BMI.

36. Get three float inputs for test scores. Calculate and display the average to 2

decimal places.

37. Ask for the user’s birth year and calculate their age assuming the current year is

2025.

38. Ask for a number and print its last digit.

39. Take an integer input and print the sum of its digits.

40. Take a 3-digit number and reverse it using mathematical operations.

41. Ask for two numbers. If the first is divisible by the second, print the result. If not,

print the remainder.

42. Compute the area of a triangle given its base and height.

43. Ask the user for a temperature in Fahrenheit. Convert to Celsius, and check if it's

below freezing.

44. Ask for hours worked and hourly wage. Calculate total pay, with 1.5× for hours

over 40.

45. Ask for the length of three sides. Determine if they can form a triangle.
46. Ask for a number and check if it is a perfect square (without using sqrt).

47. Write a program that checks if a number is divisible by both 4 and 6 but not by 5.

48. Ask for a four-digit number and determine if the sum of its first two digits equals

the sum of the last two.

49. Ask the user for their age. Categorize them as child, teenager, adult, or senior.

50. Ask for a number and check if it's prime (basic method).

51. Ask for a score (0–100) and print grade: A, B, C, D, F.

52. Input three numbers. Print them in ascending order.

53. Write a calculator program that keeps running until the user types exit.

54. Ask for a year and determine whether it's a leap year and also divisible by 100 or

400.

55. Input a character and identify if it's a digit, lowercase letter, uppercase letter, or

symbol.

56. Take two dates (day and month only). Determine which one comes earlier in the

calendar year.

57. Ask for a password twice. Validate if they match and meet length ≥ 8 characters.

58. Ask for a number. If it’s divisible by 2, print half; if not, print double.

59. Print the first 10 multiples of a given number using a loop.

60. Sum only the odd numbers between 1 and 100.

61. Calculate factorial using a while loop (no built-in function).

62. Generate the first n Fibonacci numbers and store in a list.


63. Reverse an integer using a loop (no string methods).

64. Take numbers as input until the user enters 0. Then print the sum and average.

65. Count how many numbers between 1 and 1000 are divisible by both 3 and 7.

66. Print numbers between 1 and 50, skipping multiples of 5.

67. Generate the multiplication table from 1 to 10 using nested loops.

68. Take a number and determine how many digits it has using a loop.

69. Simulate a simple login system with 3 attempts.

70. Create a number guessing game (1–100) with hints: "Too high"/"Too low".

71. Ask for a number. Print its binary representation manually (no bin()).

72. Print all prime numbers between 1 and 100 using nested loops.

73. Count the number of vowels, consonants, digits, and spaces in a string.

74. Take a sentence and print each word on a new line.

75. Reverse a string without using slicing.

76. Check if a string is a palindrome (ignore case and spaces).

77. Remove all punctuation from a string.

78. Count how many times a character appears in a string.

79. Write a program that replaces all vowels in a string with *.

80. Write a program that Capitalizes the first letter of each word manually (no. title

()).

81. Write a program that counts how many words are in a sentence.

82. Write a program that checks if one string is a rotation of another.


83. Write a program that finds the longest word in a sentence.

84. Write a program that encode a string using a basic Caesar cipher (+3 shift).

85. Write a program that checks if a string contains only alphabetic characters.

86. Write a function that removes duplicate characters from a string.

87. Write a program that inputs a sentence and returns a dictionary of word counts.

88. Write a program that creates a list of 10 random integers between 1 and 50.

89. Write a program that removes all even numbers from a list.

90. Ask the user to input 5 numbers. Store them in a list and print the median.

91. Write a program that counts how many elements in a list are greater than the

average.

92. Write a program that merges two lists and removes duplicates.

93. Write a program that finds the second-largest number in a list.

94. Write a program that rotates a list to the right by 1.

95. Write a program that sorts a list without using sort() or sorted().

96. Write a program that splits a list into even and odd numbers.

97. Write a program to find the difference between max and min in a list.

98. Remove all elements from a list except the first and last.

99. Create a list of squares for all even numbers between 1 and 20.

100. Find common elements between two lists.

101. Accept a list of integers and return only those that are palindromes.

102. Take a list and return a new list with only unique elements in original order.
103. Convert a list into a tuple. Try modifying an element. What happens?

104. Given a tuple of numbers, count how many are greater than 10.

105. Create a set from user input and remove duplicates.

106. Take two sets of user input. Find elements unique to the first.

107. Create a dictionary with student names and their grades.

108. Update the grade of a student in a dictionary.

109. Given a dictionary, remove the key with the lowest value.

110. Sort a dictionary by values.

111. Write a program to count word frequency in a paragraph using a dictionary.

112. Find keys in one dictionary that are not in another.

113. Write a function that accepts any number of arguments and returns their sum.

114. Write a function that returns True if a string is a valid identifier.

115. Write a function that accepts a list and returns a list with elements doubled.

116. Write a function that accepts a sentence and returns a dictionary with character

counts.

117. Write a function that checks whether a year is leap year (include all rules).

118. Write a program that writes 10 random numbers to a file, one per line.

119. Read a file and print all lines that contain the word “error”.

120. Count how many lines, words, and characters are in a file.

121. Merge two text files into a third one.

122. Replace a word in a file and save the result in a new file.
123. Generate a random password of 8 characters (letters, digits).

124. Print a calendar for a given month and year using the calendar module.

125. Create a simple contact book: allow user to add, search, delete contacts.

126. Build a mini quiz app that asks 3 questions and gives a score.

127. Write a program that simulates rolling two dice 100 times and count how often

each total appears.

128. Write a Python function that returns the cumulative sum of a list.

129. Store a list of prime numbers and print all primes below 50.

130. Create a tuple of the first 5 Fibonacci numbers and print them.

131. Store a dictionary mapping number names to their squares and print the

dictionary.

132. Append a new number to a list of integers and print the updated list.

133. Create a list of angles in degrees and convert all to radians using a loop.

134. Store a tuple of roots of a quadratic equation and print their sum and

product.

135. Create a dictionary with numbers as keys and their factorials as values.

136. Remove duplicates from a list of integers and print the unique elements.

137. Sort a list of integers in descending order and print the result.

138. Create a dictionary of math constants (pi, e, phi) and print their values.
139. Store a list of tuples representing 2D points and find the point closest to

the origin.

140. Create a list of the first 10 even numbers using list comprehension.

141. Store numbers and their squares in a dictionary and print keys with

squares > 50.

142. Given a list of numbers, count how many are prime.

143. Store coordinates of vertices of a polygon as tuples in a list and print

them.

144. Print the first 20 numbers in the Fibonacci sequence using a loop.

145. Sum all integers from 1 to n, where n is input by the user.

146. Print all multiples of 7 between 1 and 100.

147. Loop through a list of numbers and print only the even ones.

148. Calculate the factorial of a number using a for loop.

149. Loop through numbers 1 to 100 and print "Fizz" if divisible by 3, "Buzz"

if divisible by 5, and "FizzBuzz" if both.

150. Print all prime numbers between 1 and 100 using nested loops.

151. Loop through a list of angles in degrees and print their sine values.

152. Calculate the sum of squares of numbers from 1 to n.


153. Print the multiplication table of a number input by the user.

154. Use a while loop to compute the greatest common divisor (GCD) of two

numbers.

155. Loop through a list of tuples representing points and print the distance

of each from the origin.

156. Print all palindromic numbers between 1 and 500.

157. Loop through a dictionary of numbers and their squares, printing only

those with squares divisible by 4.

158. Use a loop to approximate the value of π using the Leibniz formula.

159. Read a file containing a list of numbers and calculate their average.

160. Write a file listing the first 50 prime numbers.

161. Load a CSV file containing x and y coordinates and compute the

distance between each consecutive point.

162. Read a file of polynomial coefficients and evaluate the polynomial at a

given point.

163. Append a new data point to a text file containing function values.

164. Write a function to save the results of a factorial calculation to a file.


165. Read a file containing pairs of numbers and print those pairs whose

product is even.

166. Load data from a file and find the maximum and minimum values.

167. Write a program that logs the output of sin(x) for x in 0 to 360 degrees

to a file.

168. Read a file containing angles and convert all to radians, saving results in

a new file.

169. Read a CSV of numbers and count how many are perfect squares.

170. Write a file that contains a multiplication table for numbers 1 to 10.

171. Read a file with polynomial roots and print their sum and product.

172. Load a dataset of vector magnitudes and compute the average

magnitude.

173. Save a dictionary of mathematical constants to a JSON file.

174. Write a function to calculate the nth Fibonacci number recursively.

175. Create a function that returns the factorial of a given number.

176. Write a function that checks if a number is prime.

177. Define a function that calculates the area of a circle given its radius.
178. Create a function that returns the roots of a quadratic equation given

coefficients.

179. Write a function to convert degrees to radians.

180. Define a function that calculates the distance between two points in 2D

space.

181. Create a function to calculate the sum of a list of numbers.

182. Write a function that checks if a number is a palindrome.

183. Define a function to compute the GCD of two numbers using Euclid’s

algorithm.

184. Write a function that returns the list of divisors of a number.

185. Create a function that normalizes a list of numbers to a scale between 0

and 1.

186. Write a function that calculates the nth triangular number.

187. Define a function that generates a list of prime numbers up to n.

188. Write a function that calculates the nth Catalan number using recursion

189. Create a Vector2D class with methods to add, subtract, and compute

magnitude.
190. Define a Matrix class with attributes for rows and columns and a method

to transpose.

191. Implement a Polynomial class with methods to evaluate at a given point

and find degree.

192. Create a ComplexNumber class with methods for addition, subtraction,

and modulus.

193. Write a Circle class with methods to compute area and circumference.

194. Implement a Triangle class that checks if three side lengths form a valid

triangle and computes its area.

195. Create a Point class representing a 2D point with methods to calculate

distance to another point.

196. Define a Sequence class that stores a list of numbers and can compute

mean, median, and mode.

197. Implement a Matrix class with a method to multiply by another matrix.

198. Write a FactorialCalculator class with a method that returns factorial of

a number.

199. Create a PrimeChecker class that stores a number and checks if it is

prime.
200. Define a Circle class that can check if a point lies inside the circle.

You might also like