Strings 5 - First challenges

Download exercises zip

Browse file online

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

Challenge - a strange zoo

✪ You are given a phrase and you know there are are a couple strange char at the boundaries of the phrase. Write some code to fix the phrase so it doesn’t has the extremities delimited by the first occurrences of char.

  • DO NOT use loops

For example - given:

phrase = "There is za strange zoo nearby, with many animalsz you wouldn't believe."

after your code, it must result:

>>> phrase
'a strange zoo nearby, with many animals'
[1]:


char, phrase = "z", "There is za strange zoo nearby, with many animalsz you wouldn't believe."  # 'a strange zoo nearby, with many animals'
#char, phrase = "Z","Zthere is a Zorg in the ZooZ outside the neighborhood"   # 'there is a Zorg in the Zoo'

# write here


Challenge - nuclear fusion

✪ Given a phrase of words separated by spaces and a word, write some code to produce a string where all occurrences beginning with that word are substitued with sub

  • phrase never begins with the word to substitute

  • DO NOT use loops

[2]:


phrase = "it's clear nuclear fusion is the future - clearly, there is a lot of interest around it"

word, sub = "clear", "unclear"  # "it's unclear nuclear fusion is the future - unclearly, there is a lot of interest around it"

#word, sub = "is", "can be"     # "it's clear nuclear fusion can be the future - clearly, there can be a lot of interest around it"

# write here


Challenge - gold

✪✪ You are given a string s which begins with a sequence of the same repeated chacter, then it has a treasure, and then continues with another sequence of another repeated character. Both initial and ending sequences have unknown length.

  • assume the string has at least three characters

  • assume the characters of starting sequence are always different from end sequence

  • DO NOT use loops

  • DO NOT write constant characters in your code (so no , $, ..)

[11]:

s = "************gold----"  # gold
#s = "///////gems!!!!!!!!"  # gems
#s = "-------€_______"      # €
#s = "p$q"                  # $


# write here


[ ]: