Tuesday, May 12, 2015

Python Exercise Solution & More

So, I wanted to post my solution to Finger Exercise 2.4 on pg. 20. This problem was a little bit harder than the first, but I was able to use some knowledge from Codecademy's online Python course, as well as some info accumulated from Stack Exchange.

Finger exercise: Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.

My Solution:

""" Write a program that asks the user to input 10 integers, and then prints the
largest odd number that was entered. If no odd number was entered, it should
print a message to that effect. """

# Ask user for input
raw_user_input = raw_input("Please enter ten integers with spaces between them: ")

# Create a list of integers called 'answers' (split based on spaces)
answers = raw_user_input.split(' ')

# create an array for the odd numbers
odd_numbers = []

# Iterate through each item in 'answers' array
for i in answers:
    # convert type str to type int
    i = int(i)

    # find out if i is an odd number using modulo
    if i % 2 != 0:
        # append odd numbers to their array
        odd_numbers.append(i)

if len(odd_numbers) == 0:
    print "There are no odd numbers in this list."
else:
    # print max odd number
    print "The largest odd number in this list is", max(odd_numbers)

The hardest part of this problem for me was figuring out how to use the "for" statement. I looked back in Codecademy's course, and I thought that this made the most sense.
I also added the knowledge about arrays, or lists, in Python, putting all of my odd numbers into a list before selecting the max.
I found the .split(' ') method on Stack Exchange, and read some documentation on it in order to allow the user to enter 10 separate numbers at once.
And I also found the max() method on Stack Exchange, read up on it, and decided it was the best option for finding the maximum odd number.

Monday, May 11, 2015

Beginning My Journey Into Coding

I began this blog so that I can document my journey into learning how to code. I have used Codecademy and various other sites along my journey thus far, but I haven't been able to really use that information to fully develop anything yet.

I decided that I would devote myself fully to learning Python, since it seems to be a good introductory language to learn. I have a hard copy of "Introduction to Computation and Programming using Python" by John V. Guttag, and I plan to work through this book in order to fully learn how to program in Python.

You are welcome to join me on my journey as I document my way through being a code newbie.

I have completed the first two chapters of this book. Here are the second exercise and my solution thus far:

2.2 Finger Exercise, Pg. 16

Write a program that examines three variables --- x, y and z --- and prints the largest odd number among them. If none of them are odd, it should print a message to that effect.

My Solution:

""" Write a program that examines three variables --- x, y, and z --- and
prints the largest odd number among them. If none of them are odd, it should
print a message to that effect. """

def largest_odd_number(x, y, z):
    print x, y, z
    if x % 2 != 0 and y % 2 != 0 and z % 2 != 0:
        if x > y and x > z:
            print x
        elif y > x and y > z:
            print y
        elif z > x and z > y:
            print z
        elif x == y and y == z:
            print x
    elif x % 2 != 0 and y % 2 != 0 and z % 2 == 0:
        if x > y:
            print x
        else:
            print y
    elif x % 2 == 0 and y % 2 != 0 and z % 2 != 0:
        if y > z:
            print y
        else:
            print z
    elif x % 2 != 0 and y % 2 == 0 and z % 2 != 0:
        if x > z:
            print x
        else:
            print z
    elif x % 2 != 0 and y % 2 == 0 and z % 2 == 0:
        print x
    elif x % 2 == 0 and y % 2 != 0 and z % 2 == 0:
        print y
    elif x % 2 == 0 and y % 2 == 0 and z % 2 != 0:
        print z
    else:
        print "none of these are odd numbers"

Notes:
I know that  my solution to this problem is probably not the most efficient, but it is what I produced as a newbie to programming. It seems to work fine, but it seems kinda long and clunky. Alas, I hope to get better with time. :)
I should say that I downloaded Python 2.7.9, as stated in the book, and I am using the standard IDLE program to write and debug my code. It seems fairly easy to use thus far.
I did use some info that I learned from Codecademy's Python track in this solution, how to create a function. I wanted to use a function in this problem because it seemed more proper. But I really am not sure.
I am gonna write another blog post later to document my solution of the next exercise.