Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, December 17, 2016

Programming Projects for Beginners - Bubble Sort

This exercise will introduce you to sorting and specifically Bubble Sort.  There are several methods for sorting and bubble sort is one of them.

Note: If you stumbled upon this post and are wondering what this is about, start here.

Bubble Sort makes multiple passes through the list, while comparing adjacent elements.  It compares adjacent elements and swaps them if they are out of order.

Here is how it works:
Input list = [23, 4, 55, 12, 76, 3,41]
First pass:
[4,23,55,12,76,3,41] - Swap
[4,23,55,12,76,3,41] - No swap
[4,23,12,55,76,3,41] - Swap
[4,23,12,55,76,3,41] - No swap
[4,23,12,55,3,76,41] - Swap
[4,23,12,55,3,41,76] - Swap
Note that at the end of the first pass, the largest number is in its final place.  It has 'bubbled' up to its final location.

For the second pass, we can ignore the last element and sort the rest.  At the end of the second pass, the penultimate item will be in its final position.  This way, we keep shrinking the list until there is nothing else to sort.

Here is the usecase:
- Program displays a list of randomly ordered integers
- Program sorts the list using bubble sort
- Program displays the sorted list

Programming constructs used:
- Comparison
- Looping
- Conditional statements

Here is what the output would look like:

Note that I am printing the list at the end of each pass, but you don't need to do that.

You will also note that the sorting algorithm continues with the passes even though there is nothing to sort after the 6th pass.  This is because the algorithm is blindly running through the list once for every item in the list.

Bonus:
Improve the algorithm in such a way that it stops soon after the list is completely sorted.  The output should look like this:


Happy coding!

Programming Projects for Beginners - Hangman

This is the classic hangman game where the user guesses the chosen word in a given number of tries.

Note: If you stumbled upon this post and are wondering what this is about, start here.

Here is the usecase:
- Program asks user to guess a letter that makes up the chosen word
- User enters a letter
- Program checks if that letter is part of the word
- If the letter is not part of the word, the program starts drawing the hangman
- If the letter is part of the word, the program displays that part of the word and asks for the next letter
- If user keeps making guessing incorrect letters, the program continues to draw the hangman
- After several bad guesses, once the hangman drawing is complete, the user loses
- If the user guesses the word, the user wins

Programming constructs used:
- User input
- Comparison
- Looping
- Conditional statements
- Error handling
- Exception handling

Here is what a sample output would look like:

Another sample execution:

Hint:
For the hangman, create a list of the ASCII pictures with increasing degree of completion, and display them accordingly.

Bonus:
Read the list of words from a local file on disk.
Account for words with repeated letters.
Account for user typing an already found letter.

Happy coding!

Programming Projects for Beginners - Email Validator

We have all filled out forms where your email is requested.  In most advanced forms, as soon as you type your email, in case you made a mistake, the form would indicate that something is wrong.  This program does exactly that.  It checks the input text to ensure it is a valid email address.

Note: If you stumbled upon this post and are wondering what this is about, start here.

Here is the usecase:
- Program asks user for email address
- User enters email address
- Program checks validity and tells user if something is incorrect and asks user for the email address once more

Assume valid email addresses look like this:
abc@cde.com
where,
'abc' and 'cde' are alphanumeric text of any length (no special characters or spaces though)
'com' is either a two char or three char alpha string

Programming constructs used:
- User input
- String comparison
- Looping
- Conditional statements
- Error handling
- Exception handling

Here is what a sample output would look like:

Happy coding!

Programming Projects for Beginners - Guess the Number

This is a game where the computer selects a random number (say, between 0 and 10, to keep it simple), and the user guesses the number.

Note: If you stumbled upon this post and are wondering what this is about, start here.

Here is the usecase:
- Program picks a random number
- Program asks user to guess it
- User enters a guess
- Program tells user if (a) it is the correct guess (b) too high [say, if the guessed number is 5+- spaces away](c) too low (d) bit high or (e) bit low
- User guesses again until the answer is found

Programming constructs used:
- User input
- Libraries
- Comparison
- Looping
- Conditional statements
- Error handling

Here is what a sample output would look like:

As you notice above, I ran the program several times to give you a flavor of the output.

Bonus:
Handle invalid input as seen in the example above.
Count the number of tries the user takes and make an appropriate comment about it (as seen in the example).

Happy coding!

Programming Projects for Beginners - Texting Monitor

This was inspired by Shark Tank episode where Trisha Prabhu pitched her app 'ReThink' to stop cyber bullying.  As soon as I saw it, I thought a beginner could write a simple program to mimic what she is doing.

Note: If you stumbled upon this post and are wondering what this is about, start here.

Here is the usecase:
- Program asks user for text
- User types text
- Program analyzes text and if it is offensive, asks user if she really wants to post it
- Program does not send text if user agrees it is offensive
- Program sends text if user still wants to send it, but warns user of consequences

Programming constructs used:
- User input
- String operations
- Looping
- Conditional statements
- Error handling

You can keep a list of 'offensive' words and check if text contains any of them in order to decide if the text is offensive or not.

Here is what a sample output would look like:

In the screenshot above, I have run the program several times to show the different outputs.

Bonus:
Handle upper and lower case input (Eg: Y, y, yes, Yes,YES) as seen in the example.
Handle invalid input as seen in the example.

Happy coding!

Thursday, December 15, 2016

Programming Projects for Beginners - Python

I had signed up to teach programming as part of the Hour of Code movement to middle/high school kids and was pondering on what would be good and interesting programming exercises for beginners.  That got me thinking about simple programs that beginner students can write and feel proud about learning programming constructs and see them used in constructive ways.

After some thinking, here are five simple programming exercises that I came up with.  Each of these can be completed within an hour.

Note that I have solved these in python and will be including the screenshot of the output to give you an idea of how the program behaves and handles errors.

Python is a simple high level interpreted programming language with a rich set of libraries which makes it easy for beginners to learn. You can learn python here as well as run it in an online interpreter.  I often use repl for an online python interpreter. For some exercises, you will need to install python locally and run your programs.  We will go into the details later.

1. Texting monitor: This was inspired by Shark Tank episode where Trisha Prabhu pitched her app 'ReThink' to stop cyber bullying.  As soon as I saw it, I thought a beginner could write a simple program to mimic what she is doing.

2. Guess the number: This is a game where the computer selects a random number (say, between 1 and 100), and the user guesses the number.

3. Email validator: We have all filled out forms where your email is requested.  In most advanced forms, as soon as you type your email, in case you made a mistake, the form would indicate that something is wrong.  This program does exactly that.  It checks the input text to ensure it is a valid email address.

4. Hangman game: This is the classic hangman game where the user guesses the chosen word in a given number of tries.

5. Bubble sort: This exercise will introduce you to sorting a list of items.  Sorting is the process of placing the elements in a collection in a specific order.  In our case, we will sort a list of random integers into a list that contains the numbers in an ascending order.

I will post more details of each of these exercises in the coming days.  Look for those posts.

Happy coding!