CPSC150A
Scientific Computing

Activity 23

String Accumulator

Password Generation

Passwords are possibly the most important way that individuals can ensure their own safety on the Internet. However, it is also typically the easiest thing for a hacker to figure out, or to get their hands on. This is simply because most Internet users choose incredibly weak passwords. The easiest way to get around this issue to to use a randomly generated password.

Details

Write the Python function generate_password(size: int) -> str that returns a string of random lower-case letters. It should be possible for the generated password to contain any letter. The parameter size size is a positive integer and is the number of characters the returned password string should contain.

Example

The code:

print(generate_password(5))
print(generate_password(10))

Might print:

crkkb
bgchwxqybn
  • The function should repeatedly generate a random character and use string concatenate to accumulate the characters into a single string.

  • There is no built-in way to generate random characters. There is a way to generate random numbers with the random module. Use random numbers and the index operator, [], to copy random characters out of the string "abcdefghijklmnopqrstuvwxyz".

Challenge

Even random strings of lowercase characters are pretty easy to break. A better program would also include upper-case letters and digits. Alter your function so that it is garenteed to include at least 1 lower-case letter, 1 upper-case letter and 1 digit. Note, the location of the required characters should be still be completely random.