Showing posts with label solution. Show all posts
Showing posts with label solution. Show all posts

Thursday, July 10, 2014

SOLUTIONS Exercise 17: How to Decode a Web Page

As promised, the exercise for How To Decode a Web Page was a hard one, and I wrote up a very detailed explanation of the solutions. However, the formatting allowed to me on Blogger were not adequate enough for what I needed, given the complexity of the solution. Instead, I wrote it up on my own blog, where the formatting options are much more customizable. Enjoy: How To Decode a Webpage Detailed Solutions!

(The format on the new site is also a trial for migrating this entire blog to that platform. Any comments appreciated!)

Saturday, July 5, 2014

SOLUTION Exercise 17: Decode a Web Page

Exercise

Use the BeautifulSoup and requests Python packages to print out a list of all the article titles on the New York Times homepage.

Solutions?

I did not get any sample solutions for this Exercise. What I will do is write the sample program, and comment every line - dissect my own program for readers to see themselves. This will be forthcoming - this post is meanwhile a placeholder.

Thursday, June 19, 2014

SOLUTION 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 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.

Sample solution

There are so many possible solutions to this exercise, really depending on how far you want to challenge yourself. The field of security, especially as it relates to computing, is an ever-growing field with countless experts, theories, principles, and more.
The sample solution here is one possible way to answer the question: it generates a string of random characters. It is clean, simple, and elegant.

Wednesday, May 28, 2014

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

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

SOLUTION Exercise 12: List Ends

Exercise

Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a new list of only the first and last elements of the given list. For practice, write this code inside a function.

Sample solution

Thursday, April 24, 2014

SOLUTION Exercise 11: Check Primality and Functions

Exercise

Ask the user for a number and determine whether the number is prime or not. (For those who have forgotten, a prime number is a number that has no divisors.). You can (and should!) use your answer to Exercise 4 to help you.

Sample solution

There are many ways of solving this problem, so here are a sample solutions:
The first, one possibility for using functions:
And this one is a different breakdown of functions to solve the problem. The strings between three ''' marks are comments in the code that describe what each function does.

And here is a solution without using functions. It is also a correct solution that accomplishes the given task, just without the use of functions.

Wednesday, April 16, 2014

SOLUTION Exercise 10: List overlap comprehensions

Exercise

This week's exercise was an old exercise (see Exercise 5), requiring 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.

Extra:
  • Randomly generate two lists to test this

Sample solution

Great example of a solution including the extras!

Friday, April 11, 2014

SOLUTION Exercise 9: Randomness, a guessing game

I apologize for being a day late on the solution / exercise for this week, I hope you'll enjoy it anyway!

Exercise


Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)

Extras:



  • Keep the game going until the user types "exit"
  • Keep track of how many guesses the user has taken, and when the game ends, print this out.

  • Sample solution


    Great example of a solution including both extras!

    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

    SOLUTION Exercise 7: list comprehensions

    Exercise

    Let's say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it.

    Sample solutions

    For the meat of the solution, check this out:

    And for a "complete" solution, look at this:

    For a solution that uses the random library to generate test lists, check this out:

    Wednesday, March 19, 2014

    SOLUTION Exercise 6: String lists

    Exercise

    Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)

    Sample solution

    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

    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.

    Wednesday, February 26, 2014

    SOLUTION Exercise 3: lists

    Exercise

    Take a list, say for example this one:
    a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    and write a program that prints out all the elements of the list that are less than 5.
    Extras:
    1. Instead of printing the elements one by one, make a new list that has all the elements less than 5 from this list in it and print out this new list.
    2. Write this in one line of Python.
    3. Ask the user for a number and return a list that contains only elements from the original list a that are smaller than that number given by the user.

    Sample solution

    I will note that none of the solutions that were submitted were written in one line of Python. There will be more exercises later that show you how to do this!
    Here is a sample solution that solves the exercise, including extras 1 and 3.

    Saturday, February 15, 2014

    SOLUTION Exercise 2: conditionals

    Full exercise post

    Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2?

    Extras:

  • If the number is a multiple of 4, print out a different message.
  • Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num, tell that to the user. If not, print a different appropriate message.

  • Sample solution

    There are many ways of doing the exercise, so I am posting a few sample solutions. The very basics:

    And something that looks slightly more complex (but is just a more complicated conditional):


    Wednesday, February 5, 2014

    SOLUTION Exercise 1: inputs and strings

    Great job this week with finishing the first weekly problem! Because I cannot include every single submission I get, I will choose one or two that are example answers and include those within each post.

    Exercise (click to see the full post and explanation)

    Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.

    Sample solution

    Note how a sample correct solution makes liberal use of the int and str functions - changing from one type to another.