For loops 3 - iterating lists
Download exercises zip
Let’s see some exercise about lists.
Exercise - The contest
✪ A list of participant
has won a contest, and now we want to show on a display ther rank. Write some code which MODIFIES the list by writing the rank of the participant next to the name.
Example - given:
partecipants = ['Marta','Peppo','Elisa','Gioele','Rosa']
After your code it must result:
>>> partecipants
['Marta-1', 'Peppo-2', 'Elisa-3', 'Gioele-4', 'Rosa-5']
[2]:
partecipants = ['Marta','Peppo','Elisa','Gioele','Rosa']
#partecipants = ['Gioele','Carmela','Rosario']
# write here
Exercise - babbà
✪✪ Write some code which given a character search
to find and a phrase
, produces a list with all the words containing that character.
[3]:
search = 's' # ['This', 'is', 'donuts,', 'croissant']
#search = 'f' # ['full', 'of', 'coffee']
phrase = "This city is full of donuts, croissant and coffee"
# write here
Exercise - The Temple of Fortune
✪✪ While exporing a temple in the region of Uttar Pradesh, you found precious stones each one with a sacred number carved in it. You are tempted to take them all, but a threating message looms over the stones, telling only the fools takes the numbers without first consult the Oracle.
To one side, you find the statue of a Buddha with crossed legs, which keeps a tray with some holes in sequence on his lap. Some hole is filled with a bean, others aren’t.
Given a list stones
of numbers and one oracle
of booleans, write some code which MODIFIES the list bag
by putting inside only the numbers of stones
such that there is a True
in a corresponding position of oracle
.
assume both the lists have exactly the same dimensions
Example - given:
[4]:
stones = [9, 7, 6, 8, 7]
oracle = [True, False, True, True, False]
After your code it must result:
>>> print(bag)
[9, 6, 8]
[5]:
stones, oracle = [9,7,6,8,7], [True, False, True, True, False] # [9, 6, 8]
#stones, oracle = [3,5,2,3,4,2,4], [True, True, False, True, False, True, False] # [3,5,3,2]
bag = []
# write here
Exercise - the longest word
✪✪ Write some code which given a phrase
, prints the length of the longest word.
NOTE: we only want to know the length of the longest word, not the word itself!
Example - given:
phrase = "The hiker is climbing the brink of the mountain"
your code must print
8
which is the length of the most long word, in this case climbing
and mountain
in a tie.
[6]:
phrase = "The hiker is climbing the brink of the mountain" # 8
#phrase = "The fearsome pirate Le Chuck ruled ruthlessly the South seas" # 10
#phrase = "Practically obvious" # 11
# write here
Exercise - desert
✪✪✪ Write some code which given a string trip
produces a list with all the words which precede the commas.
Example - given:
[7]:
trip = "They crossed deserts, waded across rivers, clambered over the mountains, and finally arrived to the Temple"
your code must produce:
['deserts', 'rivers', 'mountains']
[8]:
trip = "They crossed deserts, waded across rivers, clambered over the mountains, and finally arrived to the Temple"
# ['deserts', 'rivers', 'mountains']
#trip = "They walked with across the strees,the crowded markets, the alleys,the porches, until they found the cathedral."
# ['strees', 'markets', 'alleys', 'porches']
#trip = "The trip ended."
# []
# write here
Exercise - splash
✪✪✪ Given a lst
of odd length filled with zeros except the number in the middle, write some code which MODIFIES the list to write numbers which decrease according to the distance from the middle.
the length of the list is always odd
assume the list is always long enough to host a zero at each side
a list of dimension 1 will only contain a zero
Example 1 - given:
lst = [0, 0, 0, 0, 4, 0, 0, 0, 0]
After your code, it must result:
>>> lst
[0, 1, 2, 3, 4, 3, 2, 1, 0]
Example 2 - given:
lst = [0, 0, 0, 3, 0, 0, 0]
after your code, it must result:
>>> lst
[0, 1, 2, 3, 2, 1, 0]
[9]:
lst = [0, 0, 0, 0, 4, 0, 0, 0, 0] # -> [0, 1, 2, 3, 4, 3, 2, 1, 0]
#lst = [0, 0, 0, 3, 0, 0, 0] # -> [0, 1, 2, 3, 2, 1, 0]
#lst = [0, 0, 2, 0, 0] # -> [0, 1, 2, 1, 0]
#lst = [0] # -> [0]
# write here
Continue
Go on with exercises about iterating tuples