Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

Friday, April 11, 2014

Exercise 10: List overlap comprehensions

Exercise

This week's exercise is going to be revisiting an old exercise (see Exercise 5), except require the solution in a different way.
Take two lists, say for example these two:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes. Write this in one line of Python. (Hint: Remember list comprehensions from Exercise 7).

Extra:
  • Randomly generate two lists to test this

Discussion

Concepts for this week:
  • List comprehensions
  • Random numbers, continued

List comprehensions

We already discussed list comprehensions in Exercise 7, but they can be made much more complicated.
For example:
x = [1, 2, 3]
y = [5, 10, 15]
allproducts = [a*b for a in x for b in y]
At the end of this piece of code, allproducts will contain the list [5, 10, 15, 10, 20, 30, 15, 30, 45]. So you can put multiple for loops inside the comprehension. But you can also add more complicated conditionals:
x = [1, 2, 3]
y = [5, 10, 15]
customlist = [a*b for a in x for b in y if a*b%2 != 0]
Now customlist contains [5, 15, 15, 45] because only the odd products are added to the list.
In general, the list comprehension takes the form:
[EXPRESSION FOR_LOOPS CONDITIONALS]
as shown in the examples above.


Random numbers, continued

Try to use the Python random documentation to figure out how to generate a random list. As a hint look below:
a = random.sample(range(100), 5)
This line of code will leave a containing a list of 5 random numbers from 0 to 99.


Happy coding!

Forgot how to submit exercises?

Wednesday, April 2, 2014

SOLUTION Exercise 8: rock, paper, scissors

I was slightly disappointed by how few people attempted the exercise this week - try it, it's a fun one, and look at the sample solutions if you are stuck. I was extremely happy with the solutions I did see! Feel free to reach out to me personally if you want more personalized help.

Exercise

Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
Remember the rules:
  • Rock beats scissors
  • Scissors beats paper
  • Paper beats rock

Sample solution

Thursday, March 27, 2014

Exercise 8: While loops (rock, paper, scissors)

Exercise

Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
Remember the rules:
  • Rock beats scissors
  • Scissors beats paper
  • Paper beats rock

Discussion

Concepts for this week:
  • While loops
  • Break statements

While loops

We have already discussed for loops, or loops that look sequentially (one by one) at elements in a list. There is a second type of loop that works in a slightly different way called a while loop.
The idea is simple: while a certain condition is True, keep doing something. For example:
a = 5
while (a > 0):
    print(a)
    a -= 1
The output of this code segment is:
5
4
3
2
1
A particularly useful way to use while loops is checking user input for correctness. For example:
quit = input('Type "enter" to quit:' )
while quit != "enter":
    quit = input('Type "enter" to quit:' )
The uses for this are infinite, and can (and should!) be combined with conditionals to yield the most efficient results.

Break statements

A break statement stops the execution of a loop before the original condition is met. While the use of a break statement will often start an argument about good coding practices, sometimes it is useful.
For example:
while True: 
    usr_command = input("Enter your command: ")
    if usr_command == "quit":
        break
    else: 
        print("You typed " + usr_command)
In this case, the break statement is used to break off the "infinite while loop" that we have constructed with the while True statement.

Happy coding!
Forgot how to submit exercises?

Wednesday, March 12, 2014

SOLUTION Exercise 5: list overlap

Exercise

Take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.

Sample solution

Wednesday, March 5, 2014

Exercise 5: list overlap

Exercise

Take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.

Extras:

  1. Randomly generate two lists to test this
  2. Write this in one line of Python (don't worry if you can't figure this out at this point - we'll get to it soon)

List properties

In other words, "things you can do with lists."

One of the interesting things you can do with lists in Python is figure out whether something is inside the list or not. For example:

>>> a = [5, 10, 15, 20]
>>> 10 in a
True
>>> 3 in a
False

You can of course use this in loops, conditionals, and any other programming constructs.

list_of_students = ["Michele", "Sara", "Cassie"]

name = input("Type name to check: ")
if name in list_of_students:
    print("This student is enrolled.")
Forgot how to submit exercises?

SOLUTION Exercise 4: divisors

Exercise

Create a program that asks the user for a number and then prints out a list of all the divisors of that number. (If you don't know what a divisor is, it is a number that divides evenly into another number. For example, 13 is a divisor of 26 because 26 / 13 has no remainder.)

Sample solution

There were a number of tricky things with this exercise. First, the type conversions between input strings and integers for the range() function need to be carefully output. Second, the arguments to range() had to be carefully constructed to cover all the possible numbers. Third, the condition in the if statement had to be correct as well.