NPTEL Joy Of Computing Using Python Week 5 Programming Assignment 2025

In this article, We discuss Solutions for NPTEL Joy Of Computing Using Python Week 5 Programming Assignment 2025. If you didn't complete the assignment for Week 4 Programming, the link is provided below. Also, join the telegram for updated answers every week.


Join us on Telegram 👉 CLICK HERE 

NPTEL Joy Of Computing Using Python Week 5 Programming Assignment 2025

NPTEL Joy Of Computing Using Python


Q1.
The Mystery of Word Frequencies

You are tasked with analyzing a mysterious message that was intercepted. The message seems to contain a series of repeated words, and the challenge is to count how many times each word appears. To solve this, you must write a Python program that takes a string of text and counts the frequency of each word.

Make sure your program is case-insensitive, as the secret message is a mix of upper and lower case letters.
After counting the occurrences of each word, display the results.

Input Format: A line of text
Output format: For all the unique words in the sentence, word: followed by frequency.

Example :

Input :
The quick brown fox jumped over the lazy dog. The quick fox jumped over the dog."
Output:
the: 3
quick: 2
brown: 1
fox: 2
jumped: 2
over: 2
lazy: 1
dog: 2

Program Code: 

sentence = input().lower().split()  
ans = {}
for word in sentence:
    ans[word] = ans.get(word, 0) + 1  
for word, count in ans.items():
    print(f"{word}: {count}") {codeBox}

Q2. 
Merging Dictionaries

You are required to write a Python function merge_dicts that takes two dictionaries as input and returns a new dictionary containing all the entries from both input dictionaries. The function should not modify the input dictionaries.

The dictionaries will have strings as keys and numbers as values. In the case where the same key appears in both dictionaries, the corresponding value in the output dictionary should be the sum of the values from both dictionaries.

You need to ensure the following:

If a key exists in only one of the dictionaries, its value should remain the same in the result.

If a key exists in both dictionaries, sum the values associated with that key.

The output dictionary should include all the keys from both input dictionaries, with updated values as described above.

Input Format:
Two dictionaries containing string keys and numerical values in two separate lines.

Output Format:
A new dictionary containing all the entries from both the dictionaries, with values summed for common keys.

Example :
Input:
{'Schuyler': 10, 'Brendan': 15}
{'Aaron': 5, 'Mansi': 20, 'Schuyler': -10}

Output:
{'Schuyler': 0, 'Aaron': 5, 'Mansi': 20, 'Brendan': 15}

Program Code: 

def merge_dicts(d1, d2):
    merged_dict = d1.copy()  
    
    for key, value in d2.items():
        if key in merged_dict:
            merged_dict[key] += value  
        else:
            merged_dict[key] = value  
    
    return merged_dict {codeBox}


Q3. 

Sorting the Royal Manuscripts

You are an apprentice in the Royal Library of the Vijayanagara Empire, home to centuries-old ancient manuscripts filled with knowledge on astronomy, Ayurveda, and warfare. However, a strong monsoon wind has scattered the manuscripts in random order!

The Rajguru (Royal Scholar) assigns you a crucial task: sort the manuscripts using the ancient "Bubble Sort" technique. But there’s a rule—you must record the order of manuscripts after every pass, as the Rajguru wishes to witness the sorting magic unfold gradually.

Your sorting spell, called "Grantha Sudharaka" (Sacred Manuscript Arranger), can swap two manuscripts at a time, ensuring the heaviest ones settle at the bottom after each pass. Your goal is to restore order to the library as quickly as possible. If there are no swaps, you can stop the process of arranging.

Can you help bring back order to the Royal Library of Vijayanagara?

Input Format :
List of integers separated by spaces.

Output Format :
Every pass output in a single line.

Example:

Input
700 200 500 300

Output
200 500 300 700
200 300 500 700
200 300 500 700

Program Code: 

def grantha_sudharaka(arr):
    n = len(arr)
    for i in range(n-1):
        swapped = False
        for j in range(n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]  
                swapped = True
        print(" ".join(map(str, arr)))  
        if not swapped:  
            break


arr = list(map(int, input().split()))
grantha_sudharaka(arr) {codeBox}

Conclusion 

For Faster Updates and Immediate answer links, join with us on Telegram

Join us on Telegram 👉 CLICK HERE




One Comment Please !

Post a Comment (0)
Previous Post Next Post