You are on page 1of 7

Computer Science Practical le

Q1. def calculate_score(shape, value):


dragons_score = 0
wizards_score = 0

if shape == 'diamond' or shape == 'club':


dragons_score += 1
elif shape == 'heart':
if isinstance(value, int):
wizards_score += 1
else:
dragons_score += 1
else:
wizards_score += 1

return dragons_score, wizards_score

def determine_winner(dragons_score, wizards_score):


if dragons_score > wizards_score:
return 'Dragons'
elif wizards_score > dragons_score:
return 'Wizards'
else:
return 'It\'s a tie'

# Main program
dragons_score = 0
wizards_score = 0

n = int(input('Enter the number of cards: '))

for i in range(n):
shape = input('Enter the shape of card {}: '.format(i+1))
value = input('Enter the value of card {}: '.format(i+1))

dragons, wizards = calculate_score(shape, value)


dragons_score += dragons
wizards_score += wizards

winner = determine_winner(dragons_score, wizards_score)


print('The winner is:', winner)

Rahul Patwarika
Computer Science Practical File

Q2. numbers = []
for i in range(5):
number = int(input('Enter an integer: '))
numbers.append(number)

maximum = max(numbers)
minimum = min(numbers)

print('Maximum value: ', maximum)


print('Minimum value: ', minimum)

Q3.

def trafficLight():
color = input("Enter the color of the traffic light (RED, YELLOW, GREEN): ")

result = LIGHT(color)

if result == 0:
print("STOP, your life is precious")
elif result == 1:
print("Please WAIT, until the light is green")
elif result == 2:
print("GO! Thank you for being patient")
else:
print("Error: Invalid input. Please enter RED, YELLOW, or GREEN")

def LIGHT(color):
if color == "RED":
return 0
elif color == "YELLOW":
return 1
elif color == "GREEN":
return 2
else:
return -1

trafficLight()
print("SPEED THRILLS BUT KILLS")

Rahul Patwarika
Computer Science Practical File
Q4.

def login(uid, pwd):


if uid == "ADMIN" and pwd == "St0rE@1":
print("Login Successful")
return True
else:
return False

attempts = 0

while attempts < 3:


user_id = input("Enter user ID: ")
password = input("Enter password: ")

if login(user_id, password):
break
else:
print("Invalid user ID or password. Please try again.")
attempts += 1

if attempts == 3:
print("Account blocked")

Q5.

def convert_to_roman(number):
roman_numerals = {
1000: "M",
900: "CM",
500: "D",
400: "CD",
100: "C",
90: "XC",
50: "L",
40: "XL",
10: "X",
9: "IX",
5: "V",
4: "IV",
1: "I"
}

roman_numeral = ""
Rahul Patwarika
Computer Science Practical File

for value, symbol in roman_numerals.items():


while number >= value:
roman_numeral += symbol
number -= value

return roman_numeral

number = int(input("Enter a number: "))


roman_number = convert_to_roman(number)
print(f"The equivalent Roman numeral is: {roman_number}")

Q6.

def rotate_word(word, n):


rotated_word = ""

for char in word:


if char.isalpha():
if char.isupper():
base = ord('A')
else:
base = ord('a')
rotated_code = (ord(char) - base + n) % 26 + base
rotated_char = chr(rotated_code)
rotated_word += rotated_char
else:
rotated_word += char

return rotated_word

# Prompt the user for input


word = input("Enter a word: ")
rotation = int(input("Enter the rotation value: "))

# Call the rotate_word function with user input


rotated_word = rotate_word(word, rotation)

# Print the rotated word


print("Rotated word:", rotated_word)

Rahul Patwarika
Computer Science Practical File

Q7.
def find_triple_double_letters(word):
for i in range(len(word) - 5):
if word[i] == word[i+1] and word[i+2] == word[i+3] and word[i+4] == word[i+5]:
return word[i:i+6]
return None

# Prompt the user for input


word = input("Enter a word: ")

# Find three consecutive double letters in the word


result = find_triple_double_letters(word)

# Print the result


if result:
print(f"The word '{word}' has three consecutive double letters: {result}")
else:
print(f"The word '{word}' does not have three consecutive double letters.")

Q8.

def is_anagram(string1, string2):


# Convert the strings to lowercase and remove whitespace
string1 = string1.lower().replace(" ", "")
string2 = string2.lower().replace(" ", "")

# Sort the characters in both strings


sorted_string1 = sorted(string1)
sorted_string2 = sorted(string2)

# Compare the sorted strings


if sorted_string1 == sorted_string2:
return True
else:
return False

# Prompt the user for input


word1 = input("Enter the first word: ")
word2 = input("Enter the second word: ")

# Check if the words are anagrams


if is_anagram(word1, word2):
print(f"'{word1}' and '{word2}' are anagrams.")
else:
Rahul Patwarika
print(f"'{word1}' and '{word2}' are not anagrams.")

Computer Science Practical File

Q9.

def create_frequency_dict(word_list):
frequency_dict = {}

# Count the frequency of each word


for word in word_list:
if word in frequency_dict:
frequency_dict[word] += 1
else:
frequency_dict[word] = 1

# Create the dictionary with frequency as key and words as value


result_dict = {}
for word, frequency in frequency_dict.items():
if frequency in result_dict:
result_dict[frequency].append(word)
else:
result_dict[frequency] = [word]

return result_dict

# Prompt the user for input


words_str = input("Enter a list of words, separated by spaces: ")

# Convert the input string to a list of words


words_list = words_str.split()

# Create the frequency dictionary


frequency_dict = create_frequency_dict(words_list)

# Print the frequency dictionary


print(frequency_dict)

Q10.

def find_value_frequency(dictionary):
frequency_dict = {}

# Count the frequency of each value


for value in dictionary.values():
Rahul Patwarika
Computer Science Practical File

if value in frequency_dict:
frequency_dict[value] += 1
else:
frequency_dict[value] = 1

return frequency_dict

# Prompt the user for input


input_dict_str = input("Enter a dictionary in the format {'key1': value1, 'key2': value2, ...}:
")

# Convert the input string to a dictionary


input_dict = eval(input_dict_str)

# Find the value frequency


frequency_dict = find_value_frequency(input_dict)

# Print the frequency dictionary


print(frequency_dict)

Rahul Patwarika

You might also like