CSC 211 – Computer Programming II
Date: 04-06-2024
Instruction: Answer any 4 questions
Duration: 2 Hours
Course Title: Computer Programming II
Question 1: Phone Sales Data Analysis
a. Show total sales per phone type in 2024 (assume average)
b. Determine phone brand with highest 4-year total
c. Total number of phones sold across all brands
Python Solution:
sales = {
"Samsung": [30, 23, 34, 28],
"IPhone": [20, 26, 31, 30],
"Tecno": [22, 28, 26, 29],
"Nokia": [18, 15, 20, 23],
"Huawei": [19, 17, 22, 25]
}
# a. Predict sales for 2024 (average)
print("Predicted 2024 Sales (Average of 2020–2023):")
for brand, values in sales.items():
avg_sales = sum(values) / len(values)
print(f"{brand}: {round(avg_sales)} units")
# b. Phone brand with highest total sales
max_brand = max(sales, key=lambda b: sum(sales[b]))
max_total = sum(sales[max_brand])
print(f"\nPhone with highest 4-year total: {max_brand} ({max_total} units)")
# c. Total phones sold
total_all = sum([sum(v) for v in sales.values()])
print(f"\nTotal phones sold from 2020–2023: {total_all} units")
Question 2: Candidate Score Processor
Python Solution:
candidates = {
"Taiwo": [56, 43, 57, 60],
"Kenny": [76, 56, 67, 60],
"Usman": [47, 36, 75, 59],
"Nkechi": [37, 49, 64, 58]
}
print("Average Scores for Each Candidate:")
for name, scores in candidates.items():
average = sum(scores) / len(scores)
print(f"{name}: {average:.2f}")
# Identify best-performing student
best = max(candidates, key=lambda name: sum(candidates[name]))
print(f"\nTop performer: {best} with total {sum(candidates[best])}")
Question 3: Simple Calculator Class
Python Solution:
class Calculator:
def calculate(self, a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a / b if b != 0 else "Error: Division by zero"
else:
return a + b # Default to sum if unknown operator
# Test Cases
calc = Calculator()
print("10 + 5 =", calc.calculate(10, 5, '+'))
print("10 - 5 =", calc.calculate(10, 5, '-'))
print("10 * 5 =", calc.calculate(10, 5, '*'))
print("10 / 5 =", calc.calculate(10, 5, '/'))
print("10 $ 5 =", calc.calculate(10, 5, '$'))
Question 4: Combination Formula – nCr Class
Python Solution:
class Combination:
def factorial(self, n):
result = 1
for i in range(2, n + 1):
result *= i
return result
def nCr(self, n, r):
if r > n:
return 0
return self.factorial(n) // (self.factorial(n - r) * self.factorial(r))
# Example Usage
combo = Combination()
print("5C2 =", combo.nCr(5, 2))
print("6C3 =", combo.nCr(6, 3))
print("10C0 =", combo.nCr(10, 0))