Program 1
Aim:
To find the maximum of a list of numbers.
Algorithm:
• Start the program.
• Define a function max_of_list that takes a list as input.
• Initialize a variable max_num with the first element of the list.
• Loop through the list and compare each element with max_num.
• If an element is greater than max_num, update max_num.
• Return max_num as the maximum value in the list.
Code:
def max_of_list(numbers):
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
# Example usage
print(max_of_list([4, 7, 1, 12, 9]))
Sample Output:
12
Viva Questions:
• What is the purpose of this program?
• How does the loop help in finding the maximum number?
• Can this program handle an empty list? Why or why not?
• What would you modify to find the minimum value instead?
• How does this approach compare to using Python’s built-in max() function?
Result:
The program successfully finds the maximum number in a list.
Program 2
Aim:
To calculate the number of digits and letters in a string.
Algorithm:
• Start the program.
• Define a function to accept a string as input.
• Initialize counters for letters and digits.
• Loop through each character in the string.
• If the character is a letter, increment the letter counter; if it's a digit, increment the digit
counter.
• Print the counts for letters and digits.
Code:
def count_letters_digits(string):
letters = digits = 0
for char in string:
if char.isalpha():
letters += 1
elif char.isdigit():
digits += 1
print(f"Letters: {letters}, Digits: {digits}")
# Example usage
count_letters_digits("Hello123")
Sample Output:
Letters: 5, Digits: 3
Viva Questions:
• What function do we use to check if a character is a letter?
• How does the program differentiate between letters and digits?
• Can this code handle special characters?
• What will happen if the input string has no letters?
• How would you modify the program to count spaces as well?
Result:
The program successfully counts letters and digits in the string.