NPTEL Joy Of Computing Using Python Week 3 Programming Assignment 2024

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

NPTEL Joy Of Computing Using Python


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

Program Code: 

# Function to find the second largest number in a list
def 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 spaces
input_numbers = input()

# Convert the input string to a list of integers
numbers_list = list(map(int, input_numbers.split()))

# Find and print the second largest number
second_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

Output:
3 1 4 5 9 2 6

Program Code: 
# Function to remove duplicates and preserve the order
def 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 spaces
input_numbers = input()

# Convert the input string to a list of integers
numbers_list = list(map(int, input_numbers.split()))

# Process the list to remove duplicates while preserving the order
unique_numbers = remove_duplicates(numbers_list)

# Print the unique numbers separated by spaces
print(" ".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 indices
def 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 spaces
input_numbers = input()

# Convert the input string to a list of integers
numbers_list = list(map(int, input_numbers.split()))

# Reverse the list
reversed_list = numbers_list[::-1]

# Create the new list by adding values at odd indices
new_list = add_odd_indices(numbers_list, reversed_list)

# Print the new list separated by spaces
print(" ".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  

One Comment Please !

Post a Comment (0)
Previous Post Next Post