Programming Assignment - 10

Programming Assignment - Week 10


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

Question:

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

Code:

from collections import Counter

para = input().strip()
n = int(input().strip())

words = para.split()

word_counts = Counter(words)

if n in word_counts.values():
    print(1, end="")
else:
    print(0, end="")

Programming Assignment 2

Question:

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

Code:

float_numbers = input().strip().split()

long_tail_count = 0

for number in float_numbers:
    if '.' in number:
        integer_part, decimal_part = number.split('.')        
        if len(decimal_part) > len(integer_part):
            long_tail_count += 1

print(long_tail_count, end="")

Programming Assignment 3

Question:

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

Code:

sequence = list(map(int, input().strip().split()))
mid = len(sequence) // 2

left_sum = sum(sequence[:mid])
right_sum = sum(sequence[mid:])

if left_sum > right_sum:
    print(-1,end="")
elif right_sum > left_sum:
    print(1,end="")
else:
    print(0,end="")

Comments

Popular posts from this blog

The Joy of Computing using python -Week 1