Programming Assignment - 11
Programming Assignment - Week 11
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 a date in MM/DD/YYYY format as input and prints the date in DD-MM-YY format. The program should retain only the last two digits of the year, replace the forward slash / with a dash -, and swap the order of the month and date.
Input Format:
- The input consists of a single line.
- The line contains a date string in
MM/DD/YYYYformat.
Output Format:
- A single string in
DD-MM-YYformat. - Print the date with the day and month swapped, the year truncated to the last two digits, and all separators replaced by dashes
-.
Example:
Input:12/25/2024 Output: 25-12-24
Code:
date_input = input()
month, day, year = date_input.split("/")
year = year[-2:]
formatted_date = f"{day}-{month}-{year}"
print(formatted_date, end="")
Programming Assignment 2
Question:
Write a program that accepts a sequence of numbers separated by colons as input, and prints the common factors of all the numbers in ascending order.
Input Format:
- The input consists of a single line.
- The line contains a sequence of integers separated by colons.
Output Format:
- Print the common factors of all the numbers, separated by spaces, in ascending order.
Example:
Input: 12:18:24 Output: 1 2 3 6
Code:
def common_factors(numbers):
min_num = min(numbers)
factors = []
for i in range(1, min_num + 1):
if all(num % i == 0 for num in numbers):
factors.append(i)
return factors
input_str = input()
numbers = list(map(int, input_str.split(":")))
factors = common_factors(numbers)
print(" ".join(map(str, factors)),end="")
Programming Assignment 3
Question:
You are given a specific mapping between random lowercase alphabet letters and digits. Your task is to decode a date provided in the format DD-MM-YYYY, where each digit is represented by a corresponding letter, and convert it back to its standard decimal format.
The mapping is as follows:
a→ 0k→ 1x→ 2y→ 3s→ 4m→ 5b→ 6d→ 7p→ 8z→ 9
Input Format:
- A single line containing a date in the format
DD-MM-YYYY, where each digit is encoded using the given letter-to-number mapping.
Output Format:
- A single line containing the decoded date in standard
DD-MM-YYYYformat.
Example:
Input: mk-ya-kzma Output: 15-03-1750
Code:
def decode_date(encoded_date):
mapping = {
'a': '0',
'k': '1',
'x': '2',
'y': '3',
's': '4',
'm': '5',
'b': '6',
'd': '7',
'p': '8',
'z': '9'
}
decoded_date = ''.join(mapping[char] if char in mapping else char for char in encoded_date)
return decoded_date
encoded_date = input()
print(decode_date(encoded_date),end="")
Comments
Post a Comment