VITyarthi Python Essentials Module 7 Challenging Task Solution

Challenging Task-Peer Assessment 1

Asha has a huge collection of country currencies. She decided to count the total number of distinct country currencies in her collection. She asked for your help. You pick the currency one by one from a stack of country currencies.

Find the total number of distinct country currencies.

Input Format:

The first line contains an integer N , the total number of country currencies.
The next N lines contains the name of the country where the currency is from

Sample Input and Output
Enter Number of Currencies 3

Enter the name of the country India

1

Enter the name of the country USA

2

Enter the name of the country Japan

3

Solution

				
					print("Enter Number of Currencies:")
n = [1,2,3,1,2,3,1,2,3,1,1]
print("Enter the name of the country 1 for India / 2 for USA / 3 for Japan")
country =int(input("Enter the option:"))
if country == 1:
    count = n.count(1)
    print("No of Indian Currencies:",count)
elif country == 2:
    count = n.count(2)
    print("No of USA Currencies:",count)
elif country == 3:
    count = n.count(3)
    print("No of Japan Currencies:",count)
else:
    print("No option, enter the correct option")

				
			

Challenging Task-Peer Assessment 2

A startup company named ‘MY_Chat’ having four employees names as Sunny, Ravi, Vijay and Messi having the respected salaries are 25000,23000, 26000, and 30000.

After six months, Sunny resigned from his job. Update data based on employee name.

Input format: Sunny

 Output format: all employee data after update

Solution

				
					names = ["Sunny", "Ravi", "Vijay", "Messi"]
salary = [25000, 23000, 26000, 30000]
for i, item in enumerate(names):
    print(item, "salary is:", salary[i])
r_name = input("Enter the name of employee who resigned: ")
if r_name in names:
    index = names.index(r_name)
    names.pop(index)
    salary.pop(index)
else:
    print("Employee not found in database.")
print("Updated Database is:")
for i, item in enumerate(names):
    print(item, "salary is:", salary[i])
				
			

Challenging Task-Peer Assessment 3

Write a Python Program to Check if a Given Key Exists in a Dictionary or Not. 

Solution

				
					# Function to check if a key exists in a dictionary
def check_key(dictionary, key):
    if key in dictionary:
        return True
    else:
        return False

# Test the function
my_dict = {'a': 1, 'b': 2, 'c': 3}
key = 'a'

if check_key(my_dict, key):
    print("The key '" + key + "' exists in the dictionary.")
else:
    print("The key '" + key + "' does not exist in the dictionary.")

				
			

Challenging Task-Peer Assessment 4

A quiz was conducted in a programming tutorial class for 10 marks. Calculate Mean and Standard Deviation of the marks scored by minimum seven students in the class. Use the appropriate functions provided by Python.

Input format: Enter 7 integers with spaces 

Output format: Mean is: Standard Deviation is:

Solution

				
					import math

# prompt user for input
marks = input("Enter 7 integers separated by spaces: ").split()

# convert input to list of integers
marks = [int(m) for m in marks]

# check that there are at least 7 marks
if len(marks) < 7:
    print("Error: Please enter at least 7 integers.")
    exit()

# calculate mean
mean = sum(marks) / len(marks)

# calculate standard deviation
variance = sum((x - mean) ** 2 for x in marks) / len(marks)
std_dev = math.sqrt(variance)

# print results
print(f"Mean is: {mean:.2f}")
print(f"Standard Deviation is: {std_dev:.2f}")
				
			

Challenging Task-Peer Assessment 5

School attendance register needs to be prepared with several columns. Amongst ‘Name’ column size need to be decided based on the longest word in the list of students’ names.

The program takes a list of words and returns the word with the longest length. If there are two or more words of the same length then the first one is considered.

Input format : One integer (Number of Words) followed by list words separated by newline or enter

Output format : The word with the longest length is:

Solution

				
					def find_longest_word(words):
    longest_word = ""
    max_length = 0

    for word in words:
        if len(word) > max_length:
            longest_word = word
            max_length = len(word)

    return longest_word

# Prompt user for input
num_words = int(input("Enter the number of words: "))
word_list = []

# Get the words from the user
for _ in range(num_words):
    word = input()
    word_list.append(word)

# Find the word with the longest length
longest_word = find_longest_word(word_list)

# Print the result
print("The word with the longest length is:", longest_word)

				
			

Challenging Task-Peer Assessment 6

Python Program to remove the given key from a dictionary.

Solution

				
					d = {'a':1,'b':2,'c':3,'d':4}
print("Initial dictionary")
print(d)
key=input("Enter the key to delete(a-d):")
if key in d: 
    del d[key]
else:
    print("Key not found!")
    exit(0)
print("Updated dictionary")
print(d)

				
			

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment