Wednesday, May 28, 2014

Exercise 15: A Password Generator

Exercise

Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your run-time code in a main method.
Extra:
  • Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list.

Discussion

There are no new topics this week, but you will need to use Python's random module, described in this post.

Happy coding!

Explore away!
Forgot how to submit exercises?

SOLUTION Exercise 14: Reverse Word Order

Exercise

Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. For example, say I type the string:
My name is Michele
Then I would see the string:
Michele is name My
shown back to me.

Sample solution

Here is the quick, one-liner solution to the problem:

But most likely you didn't come up with that solution right away. You most likely went through a number of iterations like this:

But you also could have taken a hybrid approach:

Thursday, May 22, 2014

Exercise 14: Reverse Word Order

Exercise

Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. For example, say I type the string:
My name is Michele
Then I would see the string:
Michele is name My
shown back to me.

Discussion

Concepts for this week:
  • More string things

More string things

Python has a lot of interesting things you can do with strings. I will show a few here, but you can see many more methods that may or may not be useful at the official Python documentation about the string format.

Remember that strings are lists.

Splitting strings

You can "split" or tear apart strings based on a given set of characters. For example:
teststring = "this is a test"
result = teststring.split("t")
And at the end, result will contain the list:
['', 'his is a ', 'es', '']
Instead of "t", you can write any character you want. If you do not include any character, it means "split on all whitespace":
teststring = "  this      has a lot    of   spaces and    tabs"
result = testring.split()
Then result contains:
['this', 'has', 'a', 'lot', 'of', 'spaces', 'and', 'tabs']

Joining strings

You can also relatively easily "join" or "smush" strings together:
listofstrings = "['a', 'b', 'c']"
result = "**".join(listofstrings)
Then result will contain the string:
a**b**c
Take a look at the official Python documentation for more information.

Happy coding!

Explore away!
Forgot how to submit exercises?

SOLUTION Exercise 13: List Duplicates

Exercise

Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates.
Extras:
  • Write two different functions to do this - one using a loop and constructing a list, and another using sets.
  • Go back and do Exercise 5 using sets, and write the solution for that in a different function.

Sample solution

This solution has two different functions doing the solution in two ways - one does it with a loop, and one with sets!

Thursday, May 15, 2014

Exercise 13: List Duplicates

Exercise

Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates.

Extras:
  • Write two different functions to do this - one using a loop and constructing a list, and another using sets.
  • Go back and do Exercise 5 using sets, and write the solution for that in a different function.

Discussion

Concepts for this week:
  • Sets

Sets

In mathematics, a set is a collection of elements where no element is repeated. This becomes useful because if you know your data is stored in a set, you are guaranteed to have unique elements.

Features of sets

  • Sets are not ordered. This means that there is no "first element" or "last element." There are just "elements". You cannot ask a set for it's "next element".
  • There are no repeat elements in sets.
  • You can convert between sets and lists very easily.

In Python

In Python, you make and use a set with the set() keyword. For example:
names = set()
names.add("Michele")
names.add("Robin")
names.add("Michele")
print(names)
And the output will be;
set(['Michele', 'Robin'])
You can do to a set almost anything you can do to a list (except ask for things like "the third element"). See the Python documentation about sets to get a full list of things you can do to sets.
You can convert from a list to a set and a set to a list pretty easily:
names = ["Michele", "Robin", "Sara", "Michele"]
names = set(names)
names = list(names)
print(names)
And the result of this will be:
['Michele', 'Robin', 'Sara']
Explore away!

Happy coding!

Forgot how to submit exercises?