while loops 8 - Challenges

Download exercises zip

Browse file online

We now propose some exercises without solution, do you accept the challenge?

Challenge - Consecutive letters

✪✪ You are given a list of characters la. Write some code to discover the first letter which has a duplicate in the immediately next position.

  • USE a while loop

  • STOP as soon as you find a couple

  • DO NOT use break

  • DO NOT use search methods (so no .index, .find, nor strange string stuff like .replace ….)

Example 1 - given:

la = ['a','b','c','a','c','f','f','g','h','i','i','f','f','f','m']  # f 5

your code should output:

FOUND f at position 5

Example 2 - given:

la = ['c','b','a', 'b']

your code should output

DIDN'T FIND COUPLES!
[1]:

la = ['a','b','c','a','c','f','f','g','h','i','i','f','f','f','m']  # f 5
#la = ['a']                   # nothing
#la = ['a', 'a']              # a 0
#la = ['a','b', 'a', 'a']     # a 2
#la = ['b', 'b', 'a']         # b 0
#la = ['c','b','a', 'b']      # nothing
#la = ['a','c','b','b','b']   # b 2

# write here


Challenge - Failed casinos

✪✪✪ Casinos can make big money…. or fail big. You are asked to collect the giant characters from the falling billboards of some casinos which went bankrupt.

Your truck has only space to hold capacity characters. Visit casinos list from last to first order.

Write some code to:

  • MODIFY truck list until it is filled with capacity characters

  • MODIFY casinos removing ONLY the characters which were taken

Be careful:

  • USE a while loop

  • STOP as soon as you reach capacity

  • DO NOT use break

  • DO NOT use search methods (so no .index, .find, weird string stuff…)

Example - given:

truck    = []
capacity = 20
casinos  = ['The Claridge Hotel', 'Atlantic club', 'Camelot Hotel', 'Showboat','Le Jardin', 'The Sands']

it should print:

Found The Sands
  Collecting characters "The Sands"
Found Le Jardin
  Collecting characters "Le Jardin"
Found Showboat
  Collecting characters "Sh"
Collected 20 characters

truck variable is:
['T', 'h', 'e', ' ', 'S', 'a', 'n', 'd', 's', 'L', 'e', ' ', 'J', 'a', 'r', 'd', 'i', 'n', 'S', 'h']

casinos variable is:
['The Claridge Hotel', 'Atlantic club', 'Camelot Hotel', 'owboat']

NOTICE the 'S','h' at the end of truck and the missing Sh at the end of casinos

[6]:


truck = []
capacity = 20

casinos = ['The Claridge Hotel', 'Atlantic club', 'Camelot Hotel', 'Showboat','Le Jardin', 'The Sands']
#capacity, casinos = 5, ['Regency Hotel']   # truck: ['R', 'e', 'g', 'e', 'n'] casinos: ['cy Hotel']
#capacity, casinos = 2, ['a','b','c']           # truck:  ['c','b']  casinos: ['a']


# write here


[ ]: