Programming assignment - Week 3
Programming assignment - Week 3
Solutions
Note: There could be optimised ways to solve the problem, but the goal here is to solve it effectively. The most intuitive solution has been provided for clarity.
Programming assignment - 1
Create a Python program that finds the second largest number in a list of positive integers(includes zero). The program should prompt the user to input a list of numbers, then compute and print the second largest number in that list.
Input Format:
The input consists of a single list of numbers, separated by spaces.
Hint: Use .split() function to convert input to list.
Output Format:
The output consists of the second largest number in the input list.
Example:
Input:
3 1 4 1 5 9 2 6 5 3 5
Output:
6
Code:
def find_second_largest(numbers):
unique_numbers = list(set(numbers))
unique_numbers.sort(reverse=True)
if len(unique_numbers) < 2:
return "There is no second largest number."
return unique_numbers[1]
input_string = input()
numbers_list = list(map(int, input_string.split()))
second_largest = find_second_largest(numbers_list)
print(second_largest, end="")
Programming assignment - 2
Create a Python program that removes all duplicate positive integer numbers(includes zero) from a list and prints the unique numbers in the order they first appeared.
The program should prompt the user to input a list of numbers, then process the list to remove duplicates and print the resulting list of unique numbers.
Input Format:
The input consists of a single list of numbers, separated by spaces.
Output Format:
The output consists of the unique numbers, separated by spaces, from the input list, in the order they first appeared.
Example:
Input:
3 1 4 1 5 9 2 6 5 3 5
Output:
3 1 4 5 9 2 6
Code:
def remove_duplicates(numbers):
seen = set()
unique_numbers = []
for number in numbers:
if number not in seen:
seen.add(number)
unique_numbers.append(number)
return unique_numbers
input_string = input()
numbers_list = list(map(int, input_string.split()))
unique_numbers = remove_duplicates(numbers_list)
print(" ".join(map(str, unique_numbers)), end="")
Programming assignment - 3
Create a Python program that takes a list of integers, reverses the list, adds the values at odd indices from both the original and reversed lists, and creates a new list with the result. The new list should be printed in the end.
Input Format:
The input consists of a single list of integers, separated by spaces.
Output Format:
The output consists of the new list of values, separated by spaces, obtained by adding values at odd indices from both the original and reversed lists.
Example:
Input:
1 2 3 4 5
Output:
1 6 3 6 5
Code:
def process_list(input_list):
reversed_list = input_list[::-1]
new_list = []
for i in range(len(input_list)):
if i % 2 == 1:
new_value = input_list[i] + reversed_list[i]
new_list.append(new_value)
else:
new_list.append(input_list[i])
return new_list
input_str = input()
input_list = list(map(int, input_str.split()))
result = process_list(input_list)
print(" ".join(map(str, result)), end="")
Comments
Post a Comment