Welcome back, everyone! In this article, we'll walk you through the solutions for the Week 2 programming assignment of the NPTEL course "Joy of Computing Using Python." These answers are provided for reference, so feel free to use them as a guide. If you have any doubts or questions, don't hesitate to ask in the comments section below. Ready to dive in? Let's solve these problems together!
Join Telegram:Â Â CLICK HEREÂ
NPTEL Joy Of Computing Using Python Week 3 Programming Assignment 2024
Last Date: 15-08-2024Â
Q1. 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
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
Program Code:Â
# Function to find the second largest number in a listdef find_second_largest(numbers):  # Convert the list to a set to remove duplicates  unique_numbers = list(set(numbers))  Â  # Sort the unique numbers in descending order  unique_numbers.sort(reverse=True)  Â  # Return the second element, which is the second largest  return unique_numbers[1] if len(unique_numbers) > 1 else None# Main program# Prompt the user to input a list of numbers, separated by spacesinput_numbers = input()# Convert the input string to a list of integersnumbers_list = list(map(int, input_numbers.split()))# Find and print the second largest numbersecond_largest = find_second_largest(numbers_list)if second_largest is not None:  print(second_largest,end="")else:  print("EMPTY") {codeBox}
Q2. 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
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
Program Code:Â
# Function to remove duplicates and preserve the orderdef remove_duplicates(numbers):Â Â unique_numbers = []Â Â seen = set()Â Â ÂÂ Â for number in numbers:Â Â Â Â if number not in seen:Â Â Â Â Â Â unique_numbers.append(number)Â Â Â Â Â Â seen.add(number)Â Â ÂÂ Â return unique_numbers# Main program# Prompt the user to input a list of numbers, separated by spacesinput_numbers = input()# Convert the input string to a list of integersnumbers_list = list(map(int, input_numbers.split()))# Process the list to remove duplicates while preserving the orderunique_numbers = remove_duplicates(numbers_list)# Print the unique numbers separated by spacesprint(" ".join(map(str, unique_numbers)), end="") {codeBox}
Q3.Â
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
Program Code:Â
# Function to create a new list by adding values at odd indicesdef add_odd_indices(original, reversed_list):Â Â result = []Â Â for i in range(len(original)):Â Â Â Â if i % 2 == 1:Â Â Â Â Â Â result.append(original[i] + reversed_list[i])Â Â Â Â else:Â Â Â Â Â Â result.append(original[i])Â Â return result# Main program# Prompt the user to input a list of integers, separated by spacesinput_numbers = input()# Convert the input string to a list of integersnumbers_list = list(map(int, input_numbers.split()))# Reverse the listreversed_list = numbers_list[::-1]# Create the new list by adding values at odd indicesnew_list = add_odd_indices(numbers_list, reversed_list)# Print the new list separated by spacesprint(" ".join(map(str, new_list)), end = "") {codeBox}
Conclusion
If any changes in the answer, please visit the website on or before to the last date.Â
Join Telegram:Â Â CLICK HEREÂ Â