Thursday, June 28, 2012

Worksheet for Programming Week 08

# For Loop

for a in range(1,10,2):
    print `a` + ": hello"

# For Loop / List

from random import randint
randomList = []

for x in range(10):
    randomList.append(randint(1,100))
    if randomList[x] % 2 != 0:
        print randomList[x]

print randomList

# List / Sum

def totalUp():
    listOfNum = [4,3,6,2]
    total = sum(listOfNum)
    return total

print totalUp()

# Functions

def areaOfRect(w,h):
    area = w * h
    return area

print areaOfRect(5,10)

# Function / List

def printList(x):
    a = x[0]
    b = x[-1]
    c = "First element is " + `a` + ", while Last element is " + `b`
    return c

print printList([154, 198, 123, 136, 239, 257, 207, 262, 178, 228])

# Function / List II

def sumList(x):
    a = x[0]
    b = x[-1]
    c = "The sum of " + `a` + " and " + `b` + " is " + `(a + b)`
    return c

print sumList([193, 220, 113, 248, 132, 289, 188, 124, 163, 255])

# List / Maximum

def findMax(x):
    maxNum = x[0]
    for a in range(len(x)):
        if x[a] > maxNum:
            maxNum = x[a]
    return maxNum

print findMax([171, 135, 160, 295, 105, 253, 169, 224, 298, 139])

# List / Maximum by 4

def findMaxBy4(x):
    maxNum = x[0]
    for a in range(len(x)):
        if x[a] % 4 == 0:
            if x[a] > maxNum :
                maxNum = x[a]
    return maxNum

print findMaxBy4([233, 135, 109, 117, 300, 268, 185, 259, 298, 171])

# List / Maximum / Minimum

def diff(x):
    maxNum = x[0]
    for a in range(len(x)):
        if x[a] > maxNum:
            maxNum = x[a]

    minNum = x[0]
    for a in range(len(x)):
        if x[a] < minNum:
            minNum = x[a]

    difference = maxNum - minNum
    return `maxNum` + " - " + `minNum` + " = " + `difference`

print diff([110, 116, 209, 163, 287, 222, 293, 268, 275, 213])

Reflection Journal for Programming Week 08

The code should be continuous...
1) Create a list named 'numList' with the following numbers: 4, 7, 9, 0, 9, 7, 43, 2, 6, 87
2) Print out the last element in 'numList'
3) Write a loop that prints out ALL the elements in numList (first number printed must be 4)
4) Write a loop that prints out ALL the elements in numList in REVERSE ORDER (first number printed must be 87)
5) Write a loop that prints out all the numbers in numList that are GREATER THAN 10

The code :

numList = [4, 7, 9, 0, 9, 7, 43, 2, 6, 87]
print "This is last element " + `numList[-1]`
print ' '
for x in range(len(numList)):
        print numList[x]
print ' '
numList.reverse()
for x in range(len(numList)):
    print numList[x]
print ' '
for x in range(len(numList)):
    if numList[x] > 10:
        print "Greater than 10 got " + `numList[x]`

Archery Score



































[Original]

from random import randint

def getScore():
    rdmScore = []
    for a in range(10):
        rdmScore.append(randint(100,300))
    return rdmScore

rdmScore = getScore()

def lowestScore():
    lowest = min(rdmScore)
    return lowest

def highestScore():
    highest = max(rdmScore)
    return highest

def averageScore():
    total = sum(rdmScore)
    avg = float(total)/float(len(rdmScore))
    return avg

print "Score (10 games): " + `rdmScore`
print "Average Score : " + `averageScore()`
print "Lowest Score : " + `lowestScore()`
print "Highest Score : " + `highestScore()`

[With Bonus]

from random import randint

def getScore():
    rdmScore = []
    for a in range(10):
        rdmScore.append(randint(100,300))
    return rdmScore

scoreList = getScore()

def lowestScore(scoreList):
    lowest = min(scoreList)
    return lowest

def highestScore(scoreList):
    highest = max(scoreList)
    return highest

def averageScore(scoreList):
    total = sum(scoreList)
    avg = float(total)/float(len(scoreList))
    return avg

players = []
for i in range(5):
    players.append(getScore())

for i in range(len(players)):
    print "Player: " + `i+1`
    print "Score (10 games): " + `players[i]`
    print "Average Score : " + `averageScore(players[i])`
    print "Highest Score : " + `highestScore(players[i])`
    print "Lowest Score : " + `lowestScore(players[i])`
    print

worst = 0
worstScore = averageScore(players[0])
for i in range(1,len(players)):
    if averageScore(players[i]) < worstScore:
        worst = i
        worstScore = averageScore(players[i])

print "Worst player is : Player " + `worst+1`
print "His average score is " + `worstScore`
print

best = 0
bestScore = averageScore(players[0])
for i in range(1,len(players)):
    if averageScore(players[i]) > bestScore:
        best = i
        bestScore = averageScore(players[i])
print "Best player is : Player " + `best+1`
print "His average score is " + `bestScore`


Thursday, 28th June 2012

Ate maggie mee with 2 eggs at 2+ am, too hungry after do programming lol
Brain encountered some logic errors, not sure how to solve
Think I too tired today also x.x

Managed to solve today's problem statement de bonus part by myself
After a long time though

I find it really stupid if someone wants me to approach the person and understand the person from just facial expressions, how the hell do I know