Programming Assignment - 8

Programming Assignment - Week 8


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 Python function that takes an integer n followed by n lines of space-separated integers, where each line represents a tuple. The function should return the sum of the first elements of all tuples.

Input Format:

  • The first line contains a single integer n, which represents the number of tuples.
  • The next n lines each contain two space-separated integers representing the values of the tuple.

Output Format:

  • A single integer representing the sum of the first elements of all tuples.

Example:

Input:
3
3 4
1 2
5 6

Output:
9

Code:

def sum_of_first_elements():
    # Read the number of tuples
    n = int(input())
    
    # Initialize the sum to zero
    sum_first_elements = 0
    
    # Iterate over the number of tuples
    for _ in range(n):
        # Read a tuple from input, split into two integers
        a, b = map(int, input().split())
        # Add the first element of the tuple to the sum
        sum_first_elements += a
        
    # Print the resulting sum
    print(sum_first_elements, end="")
    
sum_of_first_elements()
    

Programming Assignment 2

Question:

Write a Python function that checks if two given strings are anagrams of each other, considering only alphabetic characters and ignoring case. The function should handle spaces, punctuation, and return 1 if the strings are anagrams, and 0 if not.

Input Format:

  • The first line contains a string str1 which may include alphabetic characters, spaces, and punctuation.
  • The second line contains a string str2 which may also include alphabetic characters, spaces, and punctuation.

Output Format:

  • Print 1 if the two strings are anagrams of each other, otherwise print 0.

Example:

Input:
Astronomer
Moon starer

Output:
1

Code:

def are_anagrams(str1, str2):
    # Helper function to clean and sort a string
    def clean_and_sort(s):
        # Filter out non-alphabetic characters, convert to lowercase, and sort
        return sorted([char.lower() for char in s if char.isalpha()])

    # Clean and sort both strings
    sorted_str1 = clean_and_sort(str1)
    sorted_str2 = clean_and_sort(str2)

    # Check if the sorted versions of the strings are equal
    if sorted_str1 == sorted_str2:
        return 1
    else:
        return 0

# Take inputs
str1 = input()
str2 = input()

# Call the function and print the result
print(are_anagrams(str1, str2), end="")

Programming Assignment 3

Question:

Write a function that takes a list of integers and an integer k. The function should multiply each integer in the list by k and then return the maximum value from the resulting list.

Input Format:

  • The first line contains space-separated integers representing the list of numbers.
  • The second line contains a single integer k, the multiplier.

Output Format:

  • Print a single integer representing the maximum value after multiplying each number by k.

Example:

Input:
1 5 3 9
2

Output:
18

CODE:

def max_after_multiplication(numbers, k):
    # Multiply each number in the list by k
    multiplied_numbers = [num * k for num in numbers]
    
    # Return the maximum value from the multiplied list
    return max(multiplied_numbers)

# Example usage:
# Read input as a list of integers
numbers = list(map(int, input().split()))

# Read the multiplier k
k = int(input())

# Call the function and print the result
print(max_after_multiplication(numbers, k), end="")
    

Comments

Popular posts from this blog

The Joy of Computing using python -Week 1