Skip to content

Commit

Permalink
small modification to 9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
epequeno committed Dec 28, 2012
1 parent c67b5a9 commit 4f53aa5
Show file tree
Hide file tree
Showing 66 changed files with 569 additions and 450 deletions.
3 changes: 2 additions & 1 deletion ch06/6.01.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

# Current Status: Complete


def compare(x, y):
if x > y:
return 1
elif x == y:
return 0
else:
return -1

print compare(2, 2)
5 changes: 3 additions & 2 deletions ch06/6.02.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import math


def hypotenuse(x, y):
return math.sqrt(x**2 + y**2)
return math.sqrt(x ** 2 + y ** 2)

print hypotenuse(3, 4)

"""pretty much just wrote this out with one try (coming back to this problem
Expand Down
3 changes: 2 additions & 1 deletion ch06/6.03.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Exercise 6.3 Write a function is_between(x, y, z) that returns True
# Exercise 6.3 Write a function is_between(x, y, z) that returns True
# if x <= y <= z or False otherwise.

# Current Status = Complete
Expand All @@ -7,6 +7,7 @@
y = int(raw_input('y?\n'))
z = int(raw_input('z?\n'))


def is_between(x, y, z):
return x <= y and y <= z

Expand Down
11 changes: 7 additions & 4 deletions ch06/6.04.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
# Exercise 6.4 Draw a stack diagram for the following program. What does the
# Exercise 6.4 Draw a stack diagram for the following program. What does the
# program print?

# Current Status: Complete


def b(z):
prod = a(z, z)
print z, prod
return prod



def a(x, y):
x = x + 1
return x * y


def c(x, y, z):
sum = x + y + z
pow = b(sum)**2
pow = b(sum) ** 2
return pow

x = 1
y = x + 1
print c(x, y+3, x+y)
print c(x, y + 3, x + y)

# The program prints 8100. c() calls b() with the sum of 1, 5, 3 which is 9.
# b() then calls a(9, 9). The first 9 has 1 added to it so a returns
Expand Down
17 changes: 9 additions & 8 deletions ch06/6.05.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Exercise 6.5 The Ackermann function, A(m, n), is defined:
# [picture]
# Write a function named ack that evaluates Ackerman’s function. Use your
# Write a function named ack that evaluates Ackerman’s function. Use your
# function to evaluate ack(3, 4), which should be 125. What happens for larger
# values of m and n?

# Current Status = Complete

m = 3
m = 3
n = 4


def ack(m, n):
if m == 0:
return n + 1
elif n == 0:
return ack(m - 1, 1)
else:
return ack(m - 1, ack(m, n - 1))
if m == 0:
return n + 1
elif n == 0:
return ack(m - 1, 1)
else:
return ack(m - 1, ack(m, n - 1))
ack(m, n)
24 changes: 15 additions & 9 deletions ch06/6.06.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
# if the first and last letters are the same and the middle is a palindrome.
# The following are functions that take a string argument and return the
# first, last, and middle letters:



def first(word):
return word[0]


def last(word):
return word[-1]


def middle(word):
return word[1:-1]

# We'all see how they work in Chapter 8.
# 1. Type these functions into a file named palindrome.py and test them out.
# What happens if you call middle with a string with two letters? One letter?
# What happens if you call middle with a string with two letters? One letter?
# What about the empty string, which is written '' and contains no letters?
# 2. Write a function called is_palindrome that takes a string argument and
# returns True if it is a palindrome and False otherwise. Remember that you
Expand All @@ -23,12 +28,13 @@ def middle(word):

word = str(raw_input('Want to see if it is a palindrome?\n'))


def is_palindrome(word):
if len(word) <= 2 and word[0] == word[-1]:
print 'True'
elif word[0] == word[-1]:
is_palindrome(word[1:-1])
else:
print 'False'
if len(word) <= 2 and word[0] == word[-1]:
print 'True'
elif word[0] == word[-1]:
is_palindrome(word[1:-1])
else:
print 'False'

is_palindrome(word)
3 changes: 2 additions & 1 deletion ch06/6.07.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# Current Status: Complete

# I really couldn't figure this one out for the life of me so this solution
# is stolen directly from
# is stolen directly from
# http://stackoverflow.com/questions/4429462/python-recursion-exercise


def is_power(a,b):
if(a%b != 0):
return False
Expand Down
5 changes: 3 additions & 2 deletions ch06/6.08.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Exercise 6.8 The greatest common divisor (GCD) of a and b is the largest
# number that divides both of them with no remainder
# number that divides both of them with no remainder
# One way to find the GCD of two numbers is Euclid's algorithm, which
# is based on the observation that if r is the remainder when a is divided by
# b, then gcd(a, b) = gcd(b, r). As a base case, we can consider gcd(a, 0) = a.
Expand All @@ -9,12 +9,13 @@

# Current Status: Incomplete


def gcd(a, b):
if a == 0:
return b
elif b == 0:
return a
else:
return gcd(b, a % b)

print gcd(1989, 867)
1 change: 1 addition & 0 deletions ch07/7.01.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Current Status: Complete


def print_n(s, n):
i = 0
while i < n:
Expand Down
19 changes: 10 additions & 9 deletions ch07/7.02.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

n = raw_input('Square root of what?\n')


def square_root(n):
n = float(n)
x = n / 2
i = 0
while i < 10:
y = (x + n / x) / 2
x = y
i += 1
return x
n = float(n)
x = n / 2
i = 0
while i < 10:
y = (x + n / x) / 2
x = y
i += 1
return x

print square_root(n)
print square_root(n)
46 changes: 25 additions & 21 deletions ch07/7.03.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,37 @@

import math


def newtons(n):
n = float(n) # convert input to float so printout() doesn't have to
x = n / 2 # rough estimate
i = 0
while i < 10:
y = (x + n / x) / 2 # newtons method
x = y
i += 1
return y
n = float(n) # convert input to float so printout() doesn't have to
x = n / 2 # rough estimate
i = 0
while i < 10:
y = (x + n / x) / 2 # newtons method
x = y
i += 1
return y


def libmath(n):
n = float(n)
return math.sqrt(n)
n = float(n)
return math.sqrt(n)

# this function has a mix of int, str and float so there is a bit of
# this function has a mix of int, str and float so there is a bit of
# conversion going on.


def printout():
for i in range(1, 10):
n = str(newtons(i)) # newtowns() gets int and returns float. change to str.
l = str(libmath(i)) # same here
ab = abs(newtons(i) - libmath(i)) # out as int in as float, no str
if (len(n) or len(l)) == 3:
print i, n, ' ', l, ' ', ab
elif len(n) == 12:
print i, n, '', l, ' ', ab
else:
print i, n, l, '', ab
for i in range(1, 10):
n = str(newtons(i)) # newtowns() gets int and returns float. change
# to str.
l = str(libmath(i)) # same here
ab = abs(newtons(i) - libmath(i)) # out as int in as float, no str
if (len(n) or len(l)) == 3:
print i, n, ' ', l, ' ', ab
elif len(n) == 12:
print i, n, '', l, ' ', ab
else:
print i, n, l, '', ab

printout()
5 changes: 3 additions & 2 deletions ch07/7.04.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@author: steven
"""

# The built-in function eval takes a string and evaluates it using the
# The built-in function eval takes a string and evaluates it using the
# Python interpreter.
# For example:
# >>> eval('1 + 2 * 3')
Expand All @@ -23,6 +23,7 @@

# Current Status: Complete


def eval_loop():
while True:
n = raw_input('Input?\n:: ')
Expand All @@ -32,5 +33,5 @@ def eval_loop():
result = eval(n)
print result
print result

eval_loop()
9 changes: 5 additions & 4 deletions ch07/7.05.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
@author: steven
"""

# The brilliant mathematician Srinivasa Ramanujan found an infinite series
# The brilliant mathematician Srinivasa Ramanujan found an infinite series
# that can be used to generate a numerical approximation of pi:
# http://en.wikipedia.org/wiki/Srinivasa_Ramanujan#Mathematical_achievements
#
# Write a function called estimate_pi that uses this formula to compute and
# Write a function called estimate_pi that uses this formula to compute and
# return an estimate of pi. It should use a while loop to compute terms of
# the summation until the last term is smaller than 1e-15 (which is Python
# the summation until the last term is smaller than 1e-15 (which is Python
# notation for 10^−15 ). You can check the result by comparing it to math.pi.
# You can see my solution at thinkpython.com/code/pi.py.

# Current Status: Complete

import math


def estimate_pi():
k = 0.0
last_term = 1.0
sigma = 0
while last_term > 1e-15:
last_term = ((math.factorial(4.0 * k)) * (1103.0 + 26390.0 * (k))) \
/ ((math.factorial(k)**4.0) * (396.0**(4.0 * k)))
/ ((math.factorial(k) ** 4.0) * (396.0 ** (4.0 * k)))
k += 1.0
sigma += last_term
result = ((2 * math.sqrt(2)) / 9801) * sigma
Expand Down
13 changes: 7 additions & 6 deletions ch08/8.05.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

# Current Status: Complete

word = 'banana'
target = 'a'
myword = 'banana'
mytarget = 'a'


def count(word, target):
count = 0
i = 0
for letter in word:
if letter == target:
count += 1
return count
i += 1
return i

print count(word, target)
print count(myword, mytarget)
9 changes: 5 additions & 4 deletions ch09/9.01.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Write a program that reads words.txt and prints only the words with
# Write a program that reads words.txt and prints only the words with
# more than 20 characters (not counting whitespace).

# Current Status: Complete

wordList = open('words.txt')


def words(word):
wordCount = 0
lineCount = 0
Expand All @@ -13,7 +14,7 @@ def words(word):
print word.rstrip('\r\n')
wordCount += 1
lineCount += 1
percent = (float(wordCount) / float(lineCount)) * 100.0
print "%0.3f" % percent + "%"
percent = (float(wordCount) / float(lineCount)) * 100.0
print "%0.3f%%" % percent

words(wordList)
words(wordList)
Loading

0 comments on commit 4f53aa5

Please sign in to comment.