Algorithm Compilation Document
1. Compute the Average of Numbers
Algorithm:
1. Initialize a variable sum to store the total sum of numbers.
2. Initialize a variable count to store the number of elements.
3. Iterate through the list of numbers, adding each to sum.
4. Compute the average by dividing sum by count.
5. Return the average.
Pseudocode:
BEGIN
SET sum ← 0
SET count ← 0
FOR each number in list:
sum ← sum + number
count ← count + 1
END FOR
SET average ← sum / count
PRINT average
END
Python Code:
def compute_average(numbers):
if not numbers: # Prevent division by zero
return None
return sum(numbers) / len(numbers)
# Example usage:
numbers = [10, 20, 30, 40, 50]
print("Average:", compute_average(numbers))
2. First 10 Integers and Their Squares
Algorithm:
Loop through integers from 1 to 10.
Compute the square of each integer.
Store and display the results.
Pseudocode:
BEGIN
FOR num ← 1 TO 10 DO
square ← num * num
PRINT num, square
END FOR
END
Python Code:
for num in range(1, 11):
print(f"{num}: {num**2}")
3. Display 'Good Morning' in French and English
Pseudocode:
BEGIN
PRINT "Good morning"
PRINT "Bonjour"
END
Python Code:
messages = {"English": "Good morning", "French": "Bonjour"}
for lang, greeting in messages.items():
print(f"{lang}: {greeting}")
4. Charge Phone When Battery Below 20%
Pseudocode:
BEGIN
READ battery_level
IF battery_level < 20 THEN
PRINT "Battery low! Connect to a power source."
ELSE
PRINT "Battery level is sufficient."
END IF
END
Python Code:
battery_level = int(input("Enter battery percentage: "))
if battery_level < 20:
print("Battery low! Connect to a power source.")
else:
print("Battery level is sufficient.")
5. Count Male and Female Students
Pseudocode:
BEGIN
SET male_count ← 0
SET female_count ← 0
FOR each student IN class:
IF student IS male THEN
male_count ← male_count + 1
ELSE
female_count ← female_count + 1
END IF
END FOR
PRINT male_count, female_count
END
Python Code:
students = ["male", "female", "male", "female", "female"]
male_count = students.count("male")
female_count = students.count("female")
print(f"Male Students: {male_count}, Female Students: {female_count}")
6. Convocation Night Algorithm & Flowchart
The algorithm involves:
1. Preparing the event schedule.
2. Arranging decorations and lighting.
3. Ensuring food and drinks are available.
4. Welcoming guests and beginning the ceremony.
7. Solve Quadratic Equation
Python Code:
import math
def solve_quadratic(a, b, c):
D = b**2 - 4*a*c
if D < 0:
return "No real roots"
root1 = (-b + math.sqrt(D)) / (2*a)
root2 = (-b - math.sqrt(D)) / (2*a)
return root1, root2
print(solve_quadratic(1, -3, 2)) # Example: x² - 3x + 2
8. Compute Fibonacci Series
Python Code:
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(10) # Prints first 10 Fibonacci numbers
9. Area and Circumference of a Circle
Python Code:
import math
radius = float(input("Enter radius: "))
area = math.pi * radius**2
circumference = 2 * math.pi * radius
print(f"Area: {area}, Circumference: {circumference}")
10. Tower of Hanoi
Python Code:
def tower_of_hanoi(n, source, auxiliary, target):
if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n-1, source, target, auxiliary)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n-1, auxiliary, source, target)
tower_of_hanoi(3, 'A', 'B', 'C') # Solve for 3 disks
11. Summary of Abstraction
Abstraction simplifies complex systems by hiding unnecessary details:
Data Abstraction: Focuses on relevant attributes of data.
Procedural Abstraction: Uses functions to simplify complex tasks.
Control Abstraction: Manages flow structures like loops and conditionals.
12. Tower of Hanoi for n = 4
Python Code:
def tower_of_hanoi(n, source, auxiliary, target):
if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n-1, source, target, auxiliary)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n-1, auxiliary, source, target)
tower_of_hanoi(4, 'A', 'B', 'C')
13. Difference Between Heuristic and Algorithm
Aspect Heuristic Algorithm
A rule-of-thumb or an A well-defined, step-by-step
Definition approximation to solve procedure that guarantees an
problems efficiently. exact solution.
May not always give the optimal Guarantees a correct and
Accuracy
or correct solution. optimal solution.
Faster; often used when exact May be slower, especially for
Speed
solutions are impractical. complex problems.
AI, machine learning, decision- Sorting, searching,
Use Cases
making, games. mathematical computations.
Mr Tunnee Assignment
# 1) Variables for favorite color, name, and age
favorite_color = "Blue"
name = "John Doe"
age = 25
print(f"My favorite color is {favorite_color}, my name is {name}, and I am {age} years old.")
# 2) Compute the average of three numbers
num1, num2, num3 = 10, 20, 30
average = (num1 + num2 + num3) / 3
print(f"The average of {num1}, {num2}, and {num3} is {average}")
# 3) Program for sum, product, floor division, and mean
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum_result = num1 + num2
product = num1 * num2
floor_division = num1 // num2
mean = (num1 + num2) / 2
print(f"Sum: {sum_result}, Product: {product}, Floor Division: {floor_division}, Mean: {mean}")
# 4) Accept yes/no input in any case format
user_input = input("Enter Yes or No: ").strip().lower()
if user_input in ["yes", "no"]:
print(f"You entered: {user_input.capitalize()}")
else:
print("Invalid input. Please enter Yes or No.")
# 5) Categorize age group
age = int(input("Enter your age: "))
s
if 0 <= age <= 12:
print("Category: Child")
elif 13 <= age <= 18:
print("Category: Teenager")
elif age > 19:
print("Category: Adult")
else:
print("Invalid age entered.")