Thursday, June 20, 2013

Python : Exam Statistics


grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades):
    for grade in grades:
        print grade

def grades_sum(grades):
    total = 0
    for grade in grades:
        total += grade
    return total
   
def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / len(grades)
    return average
   

def grades_variance(grades, average):
    variance = 0
    for i in grades:
        variance += (average - i) ** 2
    return variance / len(grades)

def grades_std_deviation(variance):
    std_dev = variance ** 0.5
    return std_dev
   
print print_grades(grades)
print grades_sum(grades)
print grades_average(grades)
print grades_variance(grades,grades_average(grades))
print grades_std_deviation(grades_variance(grades, grades_average(grades)))

Friday, June 14, 2013

Python : Battleship


import random

board = []
for x in range(5):
    board.append(["O"] * 5)
def print_board(board):
    for row in board:
        print " ".join(row)
print "Let's play Battleship!"
print_board(board)
def random_row(board):
    return random.randint(0, len(board) - 1)
def random_col(board):
    return random.randint(0, len(board[0]) - 1)
ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col
# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
    guess_row = input("Guess Row:")
    guess_col = input("Guess Col:")
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
        break
    else:
        if turn > 3:
            print "Game Over"
        else:

            if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
                print "Oops, that's not even in the ocean."
            elif(board[guess_row][guess_col] == "X"):
                print "You guessed that one already."
            else:
                print "You missed my battleship!"
                board[guess_row][guess_col] = "X"
    print "You have", (3-turn), "turns remain."
    #    print_board(board)

Saturday, June 8, 2013

Python : Student Becomes Teacher




lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}
alice = {
    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

# Add your function below!
def average(x):
    return sum(x) / len(x)

* * *

def get_average(student):
    return average(student["homework"]) * 0.1 + average(student["quizzes"]) * 0.3 + average(student["tests"]) * 0.6

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score  >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

print get_letter_grade(get_average(lloyd))

def get_class_average(students):
    student_averages = []
    for i in students:
        student_averages.append(get_average(i))
    return average(student_averages)

students = [lloyd, alice, tyler]

print get_class_average(students)

Thursday, June 6, 2013

Python : Taking a Vacation



def hotel_cost(nights):
    return nights * 140

def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475

def rental_car_cost(days):
    per_day = days * 40
    if days >= 7:
        return per_day - 50
    elif days >= 3:
        return per_day - 20
    else:
        return per_day

def trip_cost(city, days, spending_money):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money
 
print trip_cost("Los Angeles", 5, 600)

# return trip

def hotel_cost(nights):
    return nights * 140

bill = hotel_cost(5)

def add_monthly_interest(balance):
    return balance * (1 + (0.15 / 12))

def make_payment(payment, balance):
    new_balance = add_monthly_interest(balance - payment)

    return "You still owe: " + str(new_balance)
 
hotel_cost(5)  
print make_payment(100, hotel_cost(5) )

Wednesday, June 5, 2013

PEP 20


    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

Tuesday, June 4, 2013

Python : PygLatin


pyg = 'ay'

original = raw_input('Enter a word:')
word = original.lower()
first = original[0]
new_word = word + pyg

if len(original) > 0 and original.isalpha():
    if first == "a" or first == "e" or first == "i" or first == "o" or first == "u":
        print 'vowel'
    else:
        new_word = original[1:] + original[0:1] + pyg
        print new_word
else:
    print 'empty'