Lists 5 - First challenges
Download exercises zip
We now propose some exercises without solution, do you accept the challenge?
Challenge - super DUPER sorted
Given a string
s
as a series of exactly 3 words separated by various blanks, write some code which sorts each word separately and puts ins
a string with all the words joined and sorted.
Example - given:
s = ''super \t \n DUPER \n sorted''
after your code, it should result:
>>> s
'eprsuDEPRUdeorst'
DO NOT use if statements nor cycles
DO NOT write string constants in your code (so no
'\t'
…)
[1]:
s = 'super \t \n DUPER \n sorted' # 'eprsuDEPRUdeorst'
#s = 'cba BCAD dcab' # 'abcABCDabcd'
# write here
Challenge - What a nasty problem
Suppose you have a list of strings to_mod
with an asterisk '*'
, which you want to MODIFY by inserting another list to_ins
at asterisk position.
Example - given:
to_ins = ['I','mean','truly','darn']
to_mod = ['What','a','nasty','*','nasty','problem']
After your code, it must result:
>>> to_mod
['What', 'a', 'nasty', 'I', 'mean', 'truly', 'darn', 'nasty', 'problem']
DO NOT change the assignment of
to_mod
, so noto_mod =
statement is allowed !!! If you are thinking about converting everything to a string and back to a list, even if not optimal it could still be a solution provided you don’t use ato_mod =
statementDO NOT use loops nor
if
statementsHINT: think about methods that MODIFY a list, for example you could either:
reset the list with the
clear()
method and re-extend it with what you need (of course you will need to save prior resetting …)(harder) use slices ressignment
[2]:
to_ins = ['I','mean','truly','darn']
to_mod = ['What','a','nasty','*','nasty','problem']
#to_ins, to_mod = ['looks', 'like', 'a'], ['This','*','punishment']
# write here
Challenge - Toys in the Attic
There are Toys in the Attic! Let’s take them back!
Unfortunately, they are mixed with other stuff so we need to reorganize the mess. The attic is very tiny and dark, so you put your stuff in a long row. At the beginning of the attic
there is a little note you put with the last inventory you did. It reports the number of items of a particular category that you will find after the note. After all those objects, you will find another note with the number of objects for another category of objects which follows and so on.
We can represent the attic
as a list of mixed object types, numbers and strings. The list contains exactly three categories, in this order: toys, painting, and sports. You are given three separate empty lists toys
, painting
, sports
and your goal is to separate the objects into these 3 different lists
After your code, you should obtain:
toys: ['doll', 'lego', 'minicar']
painting: ['frame', 'brushes']
sports: ['bike', 'pump', 'racket', 'ball']
DO NOT replace the lists, so no
toys =
statements ! You can only MODIFY them.DO NOT use loops nor
if
statements
[4]:
# 0 1 2 3 4 5 6 7 8 9 10 11
attic=[3,'doll','lego','minicar',2,'frame','brushes', 4,'bike','pump','racket','ball']
# 0 1 2 3 4 5 6 7
#attic = [2,'cards','monopoly',1,'colors',2,'snowboard','ski']
# these are given, you have to somehow MODIFY these lists
toys = []
painting = []
sports = []
# write here
[ ]: