INFO II Practice 3 With Solutions
INFO II Practice 3 With Solutions
2 return sum(lst)
3
4 L = [1,2,3,4]
5 print(sum_list_elements(L))
10
1 def multiply_list(L):
2 s = 1
3 for i in L:
4 s *= i
5 return s
6
7
8 myList = [1,2,3,4]
9 mult1= multiply_list(myList)
10 print(mult1)
24
24
1 def count_strings_with_same_first_last_char(strings):
2 count = 0
3 for s in strings:
4 if len(s) >= 2 and s[0] == s[-1]:
5 count += 1
6 return count
7
1 def remove_duplicates(lst):
2 deduplicated_list = []
3 for item in lst:
4 if item not in deduplicated_list:
5 deduplicated_list.append(item)
6 return deduplicated_list
7
8 # Example usage:
9 original_list = [1, 2, 2, 3, 4, 4, 5]
10 result_list = remove_duplicates(original_list)
11 print(result_list)
[1, 2, 3, 4, 5]
1
2 def printValues():
3 l = list()
4 for i in range(1,21):
5 l.append(i**2)
6 print(l[:5])
7 print(l[-5:])
8
9 printValues()
10
11 squared_values = [x**2 for x in range(1, 31)][5:]
12 print(squared_values)
13
1 def print_nested_list(nested_list):
2 for sublist in nested_list:
3 print(sublist)
4
5 # Example usage:
6 nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print_nested_list(nested_list)
7 print_nested_list(nested_list)
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
1 def check_presence(roll_number):
2 # Assuming some logic to determine if the student is present or absent
3 # Here, checking if the roll number is even for simplicity
4 # otherwise we can check if the number is in a list
5 return "Present" if roll_number % 2 == 0 else "Absent"
6
7 # Example usage:
8 student_roll = 101
9 attendance_status = check_presence(student_roll)
10 print(f"Student with roll number {student_roll} is {attendance_status}")
11
1 def count_vowels_and_consonants(word):
2 vowels = "aeiou"
3 vowel_count = sum(1 for char in word if char.lower() in vowels)
4 consonant_count = len(word) - vowel_count
5 return vowel_count, consonant_count
6
7 # Example usage:
8 input_word = "Hello"
9 vowels_count, consonants_count = count_vowels_and_consonants(input_word)
10 print(f"Vowels: {vowels_count}, Consonants: {consonants_count}")
11
1 def convert_to_uppercase(word):
2 return word.upper()
3
4 # Example usage:
5 lowercase_word = "python"
6 uppercase_word = convert_to_uppercase(lowercase_word)
7 print(f"Uppercase: {uppercase_word}")
8
convert2fahren
0 -> 32.0
32 -> 89.6
100 -> 212.0
frame_string string
frame_string
Spanish Inquisition
Ni
*************************
** Spanish Inquisition **
*************************
********
** Ni **
********
frame_string
None
None
return
1 def frame_string(input_string):
2 """Prints the input string with a frame around it."""
3 length = len(input_string)
4 border = '*' * (length + 4)
5 content = f'** {input_string} **'
6
7 print(border)
8 print(content)
9 print(border)
10
11 frame_string("Spanish Inquisition")
12 print() # blank line
13 frame_string("Ni")
14
15 result1 = frame_string("Spanish Inquisition")
16 result2 = frame_string("Ni")
17
18 print(result1)
19 print(result2)
20
1 def insurance_tariff(age, license_years, accidents, loyalty_years):
2 if age < 25:
3 if license_years < 2:
4 if accidents == 0:
5 return "Red"
6 else:
7 return "Refused"
8 else:
9 if accidents == 0:
10 return "Orange"
11 elif accidents == 1:
12 return "Red"
13 else:
14 return "Refused"
15 else:
16 if license_years < 2:
17 if accidents == 0:
18 return "Orange"
19 elif accidents == 1:
20 return "Red"
21 else:
22 return "Refused"
23 else:
24 if accidents == 0:
25 if loyalty_years >= 1:
26 return "Green"
27 else:
28 return "Blue"
29 elif accidents == 1:
30 return "Orange"
31 elif accidents == 2:
31 elif accidents == 2:
32 return "Red"
33 else:
34 return "Refused"
35
36 age = int(input("Enter driver's age: "))
37 license_years = int(input("Enter years of holding the license: "))
38 accidents = int(input("Enter the number of accidents caused: "))
39 loyalty_years = int(input("Enter loyalty years with the company: "))
40
41 result = insurance_tariff(age, license_years, accidents, loyalty_years)
42 print(f"The insurance tariff for the driver is: {result}")
43
1 import random
2
3 # 2. function that does a turn of the game
4
5 def game_turn(current_position, dice1, dice2):
6 total_dice = dice1 + dice2
7 new_position = current_position + total_dice
8
9 # Check for special squares
10 if new_position == 52: # Jail
11 return 0
12 elif new_position == 63: # Arrival
13 return new_position
14 elif new_position > 63: # Retreat if passing square 63
15 new_position = 126 - new_position
16
17 # Check for goose squares
18 goose_squares = [9, 18, 27, 36, 45, 54]
19 if new_position in goose_squares:
20 new_position += total_dice
21
22 # Check for specific dice rolls
22 # Check for specific dice rolls
23 if (dice1, dice2) in [(3, 6), (6, 3)]:
24 new_position = 26
25 elif (dice1, dice2) in [(4, 5), (5, 4)]:
26 new_position = 53
27
28 return new_position
29
30 # 3. test the function with user-defined game state
31 current_position = 0
32
33 # You can either input dice values manually or generate random values
34 dice1 = int(input("Enter the value of the first die: "))
35 dice2 = int(input("Enter the value of the second die: "))
36 # Uncomment the following lines to generate random dice values
37 # dice1 = random.randint(1, 6)
38 # dice2 = random.randint(1, 6)
39
40 new_position = game_turn(current_position, dice1, dice2)
41
42 print(f"Current position: {current_position}, Dice values: {dice1}, {dice2}, New position:
43