Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

PROGRAM 1: Write a program to print the following Pattern :

A A A

B B B

C C C

ERRONEOUS CODE:
startPoint = A #Quote Missing
code =ord(startPoint)
for outer in range(1,4):
for inner in range(1,4) #Colon missing #Indentation
startPoint = chr(code)
print(startPoint , end="\t")
print("\n")
code+=1

CORRECT CODE:
startPoint = 'A'
code =ord(startPoint)
for outer in range(1,4):
for inner in range(1,4):
startPoint = chr(code)
print(startPoint , end="\t")
print("\n")
code+=1

CLUES:
1. Indicates the start of a function block (5)- colon
2. Essential whitespace for code blocks(11)- indentation
3. Punctuation used to enclose strings(6)- quotes
PROGRAM 2: Write a program to print the product of all the elements of the tuple

ERRONEOUS CODE:
t1 = (4, 3, 2, 2)
product = 1; #Semicolon present
for x in t1:
product *= x #Indentation error
print ("Original Tuple: " , t1)
print("The Multiplication of all elements in tuple:"product) # Comma missing

CORRECT CODE:
t1 = (4, 3, 2, 2)
product = 1
for x in t1:
product *= x
print ("Original Tuple: " , t1)
print("The Multiplication of all elements in tuple:",product)

Puzzle Clues:
1. The set of rules defining the structure of a language (6) - syntax
2. Essential whitespace for code blocks(11)- indentation
PROGRAM 3: WAP to reverse a string

ERRONEOUS CODE:
def string_reverse(str1):
rstr1 = ''
index = length(str1) #len()
for index > 0: #while
rstr1 += str1[ index - 1 ]
index = index - 1
return rstr1 #variable name error
str1 = "BITFEST 2024 - Computer Association"
print(string_reverse(str1))

CORRECT CODE:
def string_reverse(str1):
rstr1 = ''
index = len(str1)
while index > 0:
rstr1 += str1[ index - 1 ]
index = index - 1
return rstr1
str1 = "BITFEST 2024 - Computer Association"
print(string_reverse(str1))

CLUES:
1. Continuously loop while a condition holds true(5)- while
2. Function to find the length of a string(3)- len
3. A named memory location to store data(8)- variable
PROGRAM 4: Sum of digits using recursion
ERRONEOUS CODE:
def sum_list(numbers):
if !numbers: #'not' logical operator
return 0
else:
return numbers[0] + sum_list(numbers[1:])

numlist = [6,4,0,2,9,2,1] #variable name error


print("Sum of list ",num_list," is ",sum_list(num_list))

CORRECT CODE:
def sum_list(numbers):
if not numbers:
return 0
else:
return numbers[0] + sum_list(numbers[1:])

num_list = [1, 2, 3, 4, 5]
print(f"Sum of list {num_list} is {sum_list(num_list)}")

CLUES:
1. A logical operator that inverts the truth value of an expression.(3)- not
2. A named memory location to store data(8)- variable
PROGRAM 5: Program to display line plots using matplotlib.

ERRONEOUS CODE:
a = [1, 2, 3, 4] # import missing
b = [2, 4, 6, 8]
plt.figure(figsize=10, 10) # parentheses () missing

plt.subplot(2, 2, 1)
plt.plot(a, b, 'r', marker='d', markersize=6, markeredgecolor='green')
plt.title('Plot 1')
plt.tight_layout()

plt.subplot(2, 2, 2)
plt.plot(a, b, 'k', linestyle='solid', marker='s', markersize=6, markeredgecolor='red')
plt.title('Plot 2')
plt.tight_layout()
# plt.show() missing

CORRECT CODE:
import matplotlib.pyplot as plt
a = [1, 2, 3, 4]
b = [2, 4, 6, 8]
plt.figure(figsize=(10, 10))

plt.subplot(2, 2, 1)
plt.plot(a, b, 'r', marker='d', markersize=6, markeredgecolor='green')
plt.title('Plot 1')
plt.tight_layout()

plt.subplot(2, 2, 2)
plt.plot(a, b, 'k', linestyle='solid', marker='s', markersize=6, markeredgecolor='red')
plt.title('Plot 2')
plt.tight_layout()

plt.show()

CLUES:
1. Keyword to make code in one module available in another (6) - import
2. Used to enclose function arguments (8)- parentheses
3. Function to display a matplotlib plot (4)- show
PROGRAM 6: Program to check whether a number is an armstrong number or not

ERRONEOUS CODE:
n = input("Enter a number: ") # int() missing
s=0
t=n

while t > 0:
digit = t % 10
s += digit * 3 #Replaced exponent ( ** ) with multiply ( * )
t //= 10

if n == s:
prnt(n,"is an Armstrong number") #Print keyword is incorrect
else:
prnt(n,"is not an Armstrong number")

CORRECT CODE:
n = int(input("Enter a number: "))
s=0
t=n

while t > 0:
digit = t % 10
s += digit ** 3
t //= 10

if n == s:
print(n,"is an Armstrong number")
else:
print(n,"is not an Armstrong number")

CLUES:
1. Function to convert a value to an integer (3) - int
2. Function used to print statements to the console(5)- print
3. Operator that is equivalent to Math. pow() (14)- exponentiation

You might also like