Computer Science

Lab 4a — Strings

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your `cs110` directory in a folder called `lab4a`.

Tick Tick Boom

10 minutes

In the file `tick_boom.py`, you will find a set of short puzzles. The `main()` function calls a set of functions that process strings. For example:

``f1('a!_pA35S222y')``

Every function, `f1()``f4()` is a string bomb. It should return a string that contains one `'tick'` for every character in the string, but then end with `'BOOM'` when a character is reached that triggers the bomb.

Unfortunately, all of the bombs are duds right now, because all functions return one `'tick'` for every character and never explode. Your job is to add the logic that activates each string bomb.

For example, you can see that `f1('a!_pA35S222y')` should return `'tickticktickticktickBOOM'`:

``````    text = 'a!_pA35S222y'
value = f1(text)
if value != 'tickticktickticktickBOOM':
print(f"Error on f1 with {text}")
return``````

However, this same function, when called with `f1('gh 9af380Pb')` should return `'tickticktickBOOM'`:

`````` text = 'gh 9af380Pb'
value = f1(text)
if value != 'tickticktickBOOM':
print(f"Error on f1 with {text}")
return``````

What logic can you add to trigger this bomb correctly? Work one bomb at a time.

Discussion with TA:

• What is the trigger for each string bomb?
• How did you write code to trigger the bombs?

Make a string

10 minutes

Write a fuction called `make_a_string()` that takes a list of strings and puts all of these strings into one string. It does this by capitalizing all the strings in the list and concatenating them with a dash in between. For example when called like this:

``````    the_list = ['yer','a','wizard','harry']
print(make_a_string(the_list))``````

then `make_a_string()` should return `'YER-A-WIZARD-HARRY'`.

You can find starter code in `make_string.py`.

Discussion with TA:

• How did you make sure the dashes get put in the right place?

Hint, see the `collect words()` example in the guide on strings.

20 minutes

Write a function that checks whether a password is a “good” password. A password is good only if it:

• has at least 8 characters
• has at least one lowercase character
• has at least one uppercase character
• has at least one number

For example this

``print(check_password('password'))``

prints `False` and this

``print(check_password('a87fRPesT'))``

prints `True`.

There is starter code in `password_checker.py` and a set of examples that should alternate `False` and `True`.

To check whether a string has at least one lowercase letter, you can:

• set a variable, `has_lowercase` to `False`
• loop through the string checking every character
• if you find a character that is lowercase, set `has_lowercase` to `True`

You should be able to write your password checker so that it only loops through the characters of the password once.

Discussion with TA:

• What is the algorithm to solve this problem?
• Share a solution and walk through it line-by-line

Note, you will see that this password checker doesn’t particularly ensure that passwords are strong, meaning difficult for an attacker to break. The latest advice from security experts is for websites to:

• require only a minimum length (e.g. at least 8 characters)
• check whether your password is in a list of known breaches from sites that have been hacked (and then have you change it if it is found)
• check whether they can easily guess your password (and have you pick a stronger one if you have a weak password)