Showing posts with label strings. Show all posts
Showing posts with label strings. Show all posts

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

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?

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

Exercise 6: Strings as 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.)

Discussion

Concepts for this week:

  • List indexing
  • Strings are lists

List indexing

In Python (and most programming in general), you start counting lists from the number 0. The first element in a list is "number 0", the second is "number 1", etc.

As a result, when you want to get single elements out of a list, you can ask a list for that number element:

>>> a = [5, 10, 15, 20, 25]
>>> a[3]
20
>>> a[0]
5

There is also a convenient way to get sublists between two indices:

>>> a = [5, 10, 15, 20, 25, 30, 35, 40]
>>> a[1:4]
[10, 15, 20]
>>> a[6:]
[35, 40]
>>> a[:-1]
[5, 10, 15, 20, 25, 30, 35]

The first number is the "start index" and the last number is the "end index."

You can also include a third number in the indexing, to count how often you should read from the list:

>>> a = [5, 10, 15, 20, 25, 30, 35, 40]
>>> a[1:5:2]
[10, 20]
>>> a[0:3:-1]
[15, 10, 5]

To read the whole list, just use the variable name (in the above examples, a), or you can also use [:] at the end of the variable name (in the above examples, a[:]).

Strings are lists

Because strings are lists, you can do to strings everything that you do to lists. You can iterate through them:

string = "example"
for c in string: 
    print "one letter: " + c

Will give the result:

one letter: e
one letter: x
one letter: a
one letter: m
one letter: p
one letter: l
one letter: e

You can take sublists:

>>> string = "example"
>>> s = string[0:5]
>>> print s
exam

Now s has the string "exam" in it.

Moral of the story: a string is a list.

Happy coding!

Forgot how to submit exercises?

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.

Wednesday, January 29, 2014

Exercise 1: input and strings

Calibrating the exercises to the audience is going to be a challenging task, so I ask you to bear with me if the exercises are too easy or too hard. Every week there will be a poll you can click on to discuss whether the exercise is too easy or too hard and hopefully in a few weeks, I'll get the level right. Let's get to it! I will start with the exercise and include a discussion later, in case you want the extra challenge.

Exercise(s)

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.
Extras:
  1. Add on to the previous program by asking the user for another number and printing out that many copies of the previous message. (Hint: order of operations exists in Python)
  2. Print out that many copies of the previous message on separate lines. (Hint: the string "\n" is the same as pressing the ENTER button)

Discussion

Concepts for this week:
  • Getting user input
  • Manipulating strings (a few ways)

User input in Python


To get user input in Python (3), the command you use is input(). Store the result in a variable, and use it to your heart's content. Remember that the result you get from the user will be a string, even if they enter a number.
For example,
name = input("Give me your name: ")
print("Your name is " + name)
What this will print in the terminal (or the shell, whatever you are running Python in) will be:
>>> Give me your name: Michele
Your name is Michele
What happens at the end of input() is that it waits for the user to type something and press ENTER. Only after the user presses ENTER does the program continue.

Manipulating strings (a few ways)


What you get from the input() function is a string. What can you do with it?
  1. Make the string into a number. Let's say you are 100% positive that the user entered a number. You can turn the string into an integer with the function int(). (In a later exercise or two or three there will be questions about what to do when the user does NOT enter a number and you try to do this; for now don't worry about that problem). Here is what this looks like:
    age = input("Enter your age: ")
    age = int(age)
    (or, if you want to be more compact with your code)
    age = int(input("Enter your age: "))
    In both cases, age will hold a variable that is an integer, and now you can do math with it.
    (Note, you can also turn integers into strings exactly in the opposite way, using the str() function)
  2. Doing math with strings. What do I mean by that? I mean, if I want to combine (concatenate is the computer science word for this) strings, all I need to do is add them:
    print("Were" + "wolf")
    print("Door" + "man")
    print("4" + "chan")
    print(str(4) + "chan")
    The same works for multiplication, but division and subtraction do not work like this.

Forgot how to submit an exercise?