NPTEL Joy Of Computing Using Python Week 10 Programming Assignment 2024

 Welcome back, everyone! In this article, we'll walk you through the solutions for the Week 10 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 10 Programming Assignment 2024

NPTEL Joy Of Computing Using Python


Last Date: 03-10-2024 


Q1. 

Write a program that accepts the string `para` and a positive integer `n` as input. The program should print `1` if there is at least one word in `para` that occurs exactly `n` times, and `0` otherwise.

Input Format:

- The input consists of two lines.

  - The first line contains the string `para`, which is a sequence of space-separated words.

  - The second line contains a positive integer `n`.

Output Format:

- A single integer, either `1` or `0`.

  - Print `1` if there is at least one word that occurs exactly `n` times.

  - Print `0` otherwise.

Example:

Input:

apple orange apple banana orange banana banana

2

Output:

1

Program Code: 

# Input the paragraph and the integer n
para = input().strip()
n = int(input())

# Split the paragraph into words
words = para.split()

# Create a dictionary to count the occurrences of each word
word_count = {}

for word in words:
    word_count[word] = word_count.get(word, 0) + 1

# Check if any word occurs exactly n times
found = 0
for count in word_count.values():
    if count == n:
        found = 1
        break

# Output the result
print(found,end="") {codeBox}

Q2. 

Write a program that accepts a string of space-separated float numbers. The program should print the number of long tail numbers, where a float number is said to have a long tail if the number of digits after the decimal point is greater than the number of digits before the decimal point.

Input Format:
The input consists of a single line containing space-separated float numbers.

Output Format:
A single integer representing the number of long tail numbers.

Example:
Input:
12.3214 123.56 3.14159 100.1 45.6789

Output:
3

Program Code: 

# Input the space-separated float numbers
numbers = input().strip().split()

# Initialize a counter for long tail numbers
long_tail_count = 0

# Iterate over each float number
for num in numbers:
    # Split the number at the decimal point
    before_decimal, after_decimal = num.split(".")
    
    # Check if digits after the decimal are greater than those before
    if len(after_decimal) > len(before_decimal):
        long_tail_count += 1

# Output the count of long tail numbers
print(long_tail_count, end="") {codeBox}

Q3.

Write a program that accepts a space-separated string of integers representing a sequence of even length. The program should determine whether the sequence is left-heavy, right-heavy, or balanced. It should print:

  • -1 if the sequence is left-heavy (left half sum > right half sum).
  • 1 if the sequence is right-heavy (right half sum > left half sum).
  • 0 if the sequence is balanced (both sums are equal).
Input Format:
The input consists of a single line containing space-separated integers.

Output Format:
A single integer: -1 for left-heavy, 1 for right-heavy, or 0 for balanced.

Example:
Input:
1 2 3 4 5 6
Output:
1

Program Code: 

# Input the space-separated integers
sequence = list(map(int, input().split()))

# Calculate the middle index to split the sequence
mid = len(sequence) // 2

# Calculate the sum of the left half and the right half
left_sum = sum(sequence[:mid])
right_sum = sum(sequence[mid:])

# Determine if the sequence is left-heavy, right-heavy, or balanced
if left_sum > right_sum:
    print(-1,end="")
elif right_sum > left_sum:
    print(1,end="")
else:
    print(0,end=""){codeBox}






One Comment Please !

Post a Comment (0)
Previous Post Next Post