Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
51 changes: 48 additions & 3 deletions chap6.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
# Enter your answrs for chapter 6 here
# Name:
# Enter your answers for chapter 6 here
# Name: Christopher Van Schyndel


# Ex. 6.6

def first(word):
return word[0]

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

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

# 1a. Returns an empty string ( no space ), as there is nothing in the middle of a two letter string.
# 1b. Also returns an empty string ( no space ), as you are indexing nothing.

# 2.

# This function takes a string and returns True if it is a Palindrome, and False otherwise.
def is_palindrome(string):
pass
length = (len(string) / 2)
stringchecker1 = ''
stringchecker2 = ''
for length in range(length):
stringchecker1 += string[length]
stringchecker2 += string[-(length+1)]
if stringchecker1 == stringchecker2:
return True
else:
return False

# Ex 6.8

# This function finds the greatest common denominator of two integers.
# It should, but will not work for decimals in python. It is likely that the decimal
# module is required to fix this. Ex. 1.0 % 0.2 gives the result 0.2. However, 1.6 % 0.2
# gives the result 0.
def gcd(a, b):
if a != 0 and b != 0:
r = a % b
print r
print str(b)
if r != 0:
gcd(b, r)
else:
print "The greatest common denominator is " + str(abs(b))
else:
print "Cannot find the gcd of 0!"



# Ex 6.8
28 changes: 24 additions & 4 deletions chap7.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
# Enter your answrs for chapter 7 here
# Name:

# Enter your answers for chapter 7 here
# Name: Christopher Van Schyndel

import math
# Ex. 7.5

def estimate_pi():
left = 2* math.sqrt(2) / 9801.0
k = 0
estimate = 0
# summation infinity -> k = 0
while True:
numerator = math.factorial(4*k) * (1103 + 26390*k)
denominator = math.factorial(k)**4 * 396 ** (4*k)
# Overflow error unless calculations are seperated out.
# step = left * math.factorial(4*k) * (1103 + 26390*k) / math.factorial(k)**4 * 396 ** (4*k)
step = left * numerator / denominator
estimate += step
k += 1
if step < 1e-15:
break
print k
return 1 / estimate


# How many iterations does it take to converge?

# How many iterations does it take to converge?
print estimate_pi() == math.pi
# It takes 3.
Binary file added my_app/.DS_Store
Binary file not shown.
Loading