Skip to content

Commit

Permalink
Lotsa changes!
Browse files Browse the repository at this point in the history
  • Loading branch information
epequeno committed Oct 3, 2015
1 parent c0d0c1c commit b3640c5
Show file tree
Hide file tree
Showing 27 changed files with 128 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
*.pyc
.solutions
4 changes: 3 additions & 1 deletion ch06/6.05.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ def ack(m, n):
return ack(m - 1, 1)
else:
return ack(m - 1, ack(m, n - 1))
ack(m, n)
ack(m, n)

# For larger values of m and n this function "blows up"
2 changes: 1 addition & 1 deletion ch06/6.06.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def middle(word):

# Current Status = Complete

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


def is_palindrome(word):
Expand Down
12 changes: 5 additions & 7 deletions ch06/6.08.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
# greatest common divisor.
# If you need help, see en.wikipedia.org/wiki/Euclidean_algorithm.

# Current Status: Incomplete
# Current Status: Complete


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

print gcd(1989, 867)
print gcd(436, 12)
18 changes: 5 additions & 13 deletions ch07/7.03.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,13 @@ def libmath(n):
n = float(n)
return math.sqrt(n)

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


def printout():
print '{:<12}\t{:<12}\t{}'.format('newtons', 'libmath', 'delta')
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
n = newtons(i)

This comment has been minimized.

Copy link
@epequeno

epequeno Oct 3, 2015

Author Owner

I should have made this change a long time ago. The way I was formatting before was awful.

l = libmath(i)
ab = abs(n - l)
print '{:<12}\t{:<12}\t{}'.format(n, l, ab)

printout()
4 changes: 2 additions & 2 deletions ch07/7.05.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def estimate_pi():
k += 1.0
sigma += last_term
result = ((2 * math.sqrt(2)) / 9801) * sigma
print 1 / result
return 1 / result

estimate_pi()
print estimate_pi()
print math.pi

# real 0m0.014s
Expand Down
23 changes: 5 additions & 18 deletions ch09/9.04.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@

# Current Status: Incomplete

word_file = open('words.txt')
target = 'acefhlo'


def make_list():
word_list = []
for word in word_file:
word_list.append(word.rstrip('\r\n'))
return word_list

word_list = make_list()
with open('words.txt', 'r') as fd:
word_list = fd.read().splitlines()


def uses_only(word, string):
Expand All @@ -23,15 +17,8 @@ def uses_only(word, string):
return True


def make_sentence(x):
count = 0
for word in word_list:
if uses_only(word, 'acefhlo'):
print word
count += 1
return count

print make_sentence(word_list)
words = [word for word in word_list if uses_only(word, target)]
print "There are {} words that use only '{}', here's a sample: {}".format(len(words), target, words[:10])

# There are 188 words in the list that use only the letters found in 'acefhlo'
# so it's very likely to make a sentence with those words.
2 changes: 1 addition & 1 deletion ch09/9.05.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def uses_all(word, string):

def find_uses_all_vowels(list):
count = 0
string = 'aeiouy'
string = 'aeiou'
for word in list:
if uses_all(word, string):
count += 1
Expand Down
5 changes: 3 additions & 2 deletions ch09/9.06.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
def is_abecedarian(word):
return word == ''.join(sorted(word))

print ("There are %s abecedarian words."
% len([word for word in word_list if is_abecedarian(word)]))
words = [word for word in word_list if is_abecedarian(word)]

print "There are {} abecedarian words.".format(len(words))
2 changes: 1 addition & 1 deletion ch09/9.08.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def is_pal_num():

is_pal_num()

# My solution is much different from the authors solution. For exercize
# My solution is much different from the authors solution. For exercise
# 9.7 I couldn't solve it and had to look at the authors and ended up with
# a solution that was almost identical. In this case I solved it independently
# and got something totally different. I kind of like mine a little better
Expand Down
2 changes: 1 addition & 1 deletion ch09/9.09.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ def main():

candidates = main()

print "You are now %s years old." % candidates[5]
print "You are now {} years old.".format(candidates[5])
3 changes: 2 additions & 1 deletion ch10/10.07.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

# Current Status: Complete

words = open('words.txt')
with open('words.txt') as fd:
words = fd.read().split()


def method_one(words):
Expand Down
5 changes: 3 additions & 2 deletions ch10/10.08.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

# Current Status: Complete

word_list = [word.strip('\r\n') for word in open('words.txt')]
with open('words.txt') as fd:
word_list = fd.read().splitlines()


def bisect(myWord, myList):
Expand All @@ -37,4 +38,4 @@ def bisect(myWord, myList):
return original.index(myWord)


print bisect("steven", word_list)
print bisect("danger", word_list)
8 changes: 5 additions & 3 deletions ch10/10.09.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

# Current Status: Complete

word_dict = {word.strip('\r\n'): None for word in open('words.txt')}
with open('words.txt') as fd:
word_list = fd.read().splitlines()

word_dict = {word: None for word in word_list}

def find_rev_pairs():
def find_rev_pairs(word_dict):
for word in word_dict:
if word[::-1] in word_dict:
print word, word[::-1]

find_rev_pairs()
find_rev_pairs(word_dict)
5 changes: 4 additions & 1 deletion ch10/10.10.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

# Current Status: Complete

word_dict = {word.strip('\r\n'): None for word in open('words.txt', 'r')}
with open('words.txt') as fd:
word_list = fd.read().splitlines()

word_dict = {word: None for word in word_list}


def split_word(word):
Expand Down
6 changes: 4 additions & 2 deletions ch11/11.01.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import uuid

words = open('words.txt')
with open('words.txt') as fd:
words = fd.read().splitlines()

result = dict()


Expand All @@ -17,4 +19,4 @@ def dictionary():
result[line] = uuid.uuid4()
return result

dictionary()
print dictionary()
5 changes: 4 additions & 1 deletion ch11/11.09.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import rotate

word_dict = {word.strip('\r\n'): None for word in open('words.txt', 'r')}
with open('words.txt') as fd:
word_list = fd.read().splitlines()

word_dict = {word: None for word in word_list}


def find_rot_pairs():
Expand Down
Binary file removed ch11/rotate.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion ch12/12.02.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import random

words = open('words.txt')
with open('words.txt') as fd:
words = fd.read().splitlines()


def sort_by_length(words):
Expand Down
4 changes: 3 additions & 1 deletion ch12/12.04.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

# Status: Complete

words = [line.strip('\r\n') for line in open('words.txt', 'r')]
with open('words.txt', 'r') as fd:
words = fd.read().splitlines()

def make_anagram_dict(word_list):
'''Take a list of words, return a dict with a fingerprint as the key
Expand All @@ -45,6 +46,7 @@ def print_anagrams(anagrams):

print "Sample from anagram dict:"
for i in range(1, 6):
# call once, print twice
fp_next = fp.next()
print "%s) %s:" % (i, fp_next), anagrams[fp_next]

Expand Down
20 changes: 6 additions & 14 deletions ch13/13.01.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,21 @@
# Also, you might consider using the string methods strip, replace and
# translate.

import string
from string import punctuation, whitespace

punctuations = [mark for mark in string.punctuation]
whitespaces = [char for char in string.whitespace]
book = 'origin.txt'

#split into words
def words():
data = open('origin.txt', 'r')
main = []
for line in data:
for item in line.split():
main.append(item)
return main
data.close()
with open(book, 'r') as fd:
words = fd.read().split()

#remove punctuation, whitespace, uppercase
def clean(word):
cleansed = ''
for char in word:
if ((char in punctuations) or (char in whitespaces)):
if ((char in punctuation) or (char in whitespace)):
pass
else:
cleansed += char.lower()
return cleansed

print "The book has %s 'words'" % len([clean(word) for word in words()])
print "{} has {} 'words'".format(book, len([clean(word) for word in words]))
7 changes: 2 additions & 5 deletions ch13/13.02.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
#books by different authors, written in different eras. Which author uses
#the most extensive vocabulary?

import string

punctuations = [mark for mark in string.punctuation]
whitespaces = [char for char in string.whitespace]
from string import punctuation, whitespace

origin = 'origin.txt'
huck = 'huck.txt'
Expand All @@ -45,7 +42,7 @@ def words(book):
def clean(word):
result = ''
for letter in word:
if (letter in whitespaces) or (letter in punctuations):
if (letter in whitespace) or (letter in punctuation):
pass
else:
result += letter.lower()
Expand Down
7 changes: 2 additions & 5 deletions ch13/13.03.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#Exercise 3 Modify the program from the previous exercise to print the 20
# most frequently-used words in the book.

import string

punctuations = [mark for mark in string.punctuation]
whitespaces = [space for space in string.whitespace]
from string import punctuation, whitespace

origin = 'origin.txt'
huck = 'huck.txt'
Expand Down Expand Up @@ -36,7 +33,7 @@ def words(book):
def clean(word):
result = ''
for char in word:
if (char in whitespaces) or (char in punctuations):
if (char in whitespace) or (char in punctuation):
pass
else:
result += char.lower()
Expand Down
7 changes: 2 additions & 5 deletions ch13/13.04.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
# them are typos? How many of them are common words that should be in the word
# list, and how many of them are really obscure?

import string

punctuations = [mark for mark in string.punctuation]
whitespaces = [space for space in string.whitespace]
from string import punctuation, whitespace

origin = 'origin.txt' # Origin of Species, 1859
huck = 'huck.txt' # Huck Finn, 1884
Expand Down Expand Up @@ -37,7 +34,7 @@ def words(book):
def clean(word):
result = ''
for char in word:
if (char in whitespaces) or (char in punctuations):
if (char in whitespace) or (char in punctuation):
pass
elif not char.isalpha():
pass
Expand Down
4 changes: 2 additions & 2 deletions ch16/16.08.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# 2. Write a program that takes a birthday as input and prints the user's age
# and the number of days, hours, minutes and seconds until their next birthday.

# Current Status: incomplete
# Current Status: Complete

import datetime

Expand All @@ -27,7 +27,7 @@ def __init__(self, year=1, month=1, day=1, hour=0, minute=0, second=0):
self.date = datetime.datetime(year, month, day, hour, minute, second)

today = Time().now
birthday = Time(1983, 3, 31).date
birthday = Time(1953, 5, 24).date


def day_of_week():
Expand Down
Loading

0 comments on commit b3640c5

Please sign in to comment.