Skip to content

Commit

Permalink
Added 2.7 and 2.11
Browse files Browse the repository at this point in the history
  • Loading branch information
rodvand committed Jan 26, 2013
1 parent 0dd4868 commit ab93166
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
57 changes: 57 additions & 0 deletions chapter2/2.11-ball_table3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
t = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]

def y(t):
# formula 1.1 - find the vertical position of a ball at a specific (t) time
v0 = 5 # initial velocity
g = 9.81 # gravity

return (v0*t)- (0.5*g*(t**2))

# calculate the y for the different t values
y = [y(T) for T in t]

# save the two lists in one list

t1 = [t, y]

i = 0
print '---------------------'
print '|__t______|y(t)_____|'

# loop through our two lists and print the values
while i < len(t1[0]):
tvalues = t1[0] # our t values
yvalues = t1[1] # our y values
print '| %g |%4.4f |' % (tvalues[i], yvalues[i])
i = i + 1
print '---------------------'

# by using list comprehension we can mix the two values into new elements in our new list
t2 = [[T, Y] for T, Y in zip(t, y)]
print '---------]2[---------'
# we print the elements, we know that t is index 0 and y(t) is index 1 in each of our list elements
for T2 in t2:
print '| %g |%4.4f |' % (T2[0], T2[1])
print '---------------------'


'''
Unix> python ball_table3.py
---------------------
|__t______|y(t)_____|
| 0.1 |0.4509 |
| 0.2 |0.8038 |
| 0.3 |1.0585 |
| 0.4 |1.2152 |
| 0.5 |1.2737 |
| 0.6 |1.2342 |
---------------------
---------]2[---------
| 0.1 |0.4509 |
| 0.2 |0.8038 |
| 0.3 |1.0585 |
| 0.4 |1.2152 |
| 0.5 |1.2737 |
| 0.6 |1.2342 |
---------------------
'''
38 changes: 34 additions & 4 deletions chapter2/2.7-ball_table2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
"""
Compute from formula (1.1)
with a list of t (0.1 - 0.6)
"""
t = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]

def y(t):
# formula 1.1 - find the vertical position of a ball at a specific (t) time
v0 = 5 # initial velocity
g = 9.81 # gravity

return (v0*t)- (0.5*g*(t**2))

# calculate the y for the different t values
y = [y(T) for T in t]

i = 0
print '---------------------'
print '|__t______|y(t)_____|'

# loop through our two lists and print the values
while i < len(y):
print '| %g |%4.4f |' % (t[i], y[i])
i = i+1
print '---------------------'


'''
martirod@lazarus ~/privat $ python ball_table2.py
---------------------
|__t______|y(t)_____|
| 0.1 |0.4509 |
| 0.2 |0.8038 |
| 0.3 |1.0585 |
| 0.4 |1.2152 |
| 0.5 |1.2737 |
| 0.6 |1.2342 |
---------------------
'''

0 comments on commit ab93166

Please sign in to comment.