Skip to content

Commit f920cce

Browse files
committed
source code for chapter 3
1 parent d8e3872 commit f920cce

File tree

7 files changed

+251
-0
lines changed

7 files changed

+251
-0
lines changed

ch03/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__all__ = ['disjoint', 'exercises', 'find', 'find_max', 'prefix_averages', 'unique']

ch03/disjoint.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2013, Michael H. Goldwasser
2+
#
3+
# Developed for use with the book:
4+
#
5+
# Data Structures and Algorithms in Python
6+
# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
7+
# John Wiley & Sons, 2013
8+
#
9+
# This program is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 3 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
def disjoint1(A, B, C):
23+
"""Return True if there is no element common to all three lists."""
24+
for a in A:
25+
for b in B:
26+
for c in C:
27+
if a == b == c:
28+
return False # we found a common value
29+
return True # if we reach this, sets are disjoint
30+
31+
def disjoint2(A, B, C):
32+
"""Return True if there is no element common to all three lists."""
33+
for a in A:
34+
for b in B:
35+
if a == b: # only check C if we found match from A and B
36+
for c in C:
37+
if a == c # (and thus a == b == c)
38+
return False # we found a common value
39+
return True # if we reach this, sets are disjoint

ch03/exercises.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2013, Michael H. Goldwasser
2+
#
3+
# Developed for use with the book:
4+
#
5+
# Data Structures and Algorithms in Python
6+
# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
7+
# John Wiley & Sons, 2013
8+
#
9+
# This program is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 3 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
def example1(S):
23+
"""Return the sum of the elements in sequence S."""
24+
n = len(S)
25+
total = 0
26+
for j in range(n): # loop from 0 to n-1
27+
total += S[j]
28+
return total
29+
30+
def example2(S):
31+
"""Return the sum of the elements with even index in sequence S."""
32+
n = len(S)
33+
total = 0
34+
for j in range(0, n, 2): # note the increment of 2
35+
total += S[j]
36+
return total
37+
38+
def example3(S):
39+
"""Return the sum of the prefix sums of sequence S."""
40+
n = len(S)
41+
total = 0
42+
for j in range(n): # loop from 0 to n-1
43+
for k in range(1+j): # loop from 0 to j
44+
total += S[k]
45+
return total
46+
47+
def example4(S):
48+
"""Return the sum of the prefix sums of sequence S."""
49+
n = len(S)
50+
prefix = 0
51+
total = 0
52+
for j in range(n):
53+
prefix += S[j]
54+
total += prefix
55+
return total
56+
57+
def example5(A, B): # assume that A and B have equal length
58+
"""Return the number of elements in B equal to the sum of prefix sums in A."""
59+
n = len(A)
60+
count = 0
61+
for i in range(n): # loop from 0 to n-1
62+
total = 0
63+
for j in range(n): # loop from 0 to n-1
64+
for k in range(1+j): # loop from 0 to j
65+
total += A[k]
66+
if B[i] == total:
67+
count += 1
68+
return count

ch03/find.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2013, Michael H. Goldwasser
2+
#
3+
# Developed for use with the book:
4+
#
5+
# Data Structures and Algorithms in Python
6+
# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
7+
# John Wiley & Sons, 2013
8+
#
9+
# This program is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 3 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
def find(S, val):
23+
"""Return index j such that S[j] == val, or -1 if no such element."""
24+
n = len(S)
25+
j = 0
26+
while j < n:
27+
if S[j] == val:
28+
return j # a match was found at index j
29+
j += 1
30+
return -1

ch03/find_max.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2013, Michael H. Goldwasser
2+
#
3+
# Developed for use with the book:
4+
#
5+
# Data Structures and Algorithms in Python
6+
# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
7+
# John Wiley & Sons, 2013
8+
#
9+
# This program is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 3 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
def find_max(data):
23+
"""Return the maximum element from a nonempty Python list."""
24+
biggest = data[0] # The initial value to beat
25+
for val in data: # For each value:
26+
if val > biggest # if it is greater than the best so far,
27+
biggest = val # we have found a new best (so far)
28+
return biggest # When loop ends, biggest is the max

ch03/prefix_averages.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2013, Michael H. Goldwasser
2+
#
3+
# Developed for use with the book:
4+
#
5+
# Data Structures and Algorithms in Python
6+
# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
7+
# John Wiley & Sons, 2013
8+
#
9+
# This program is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 3 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
def prefix_average1(S):
23+
"""Return list such that, for all j, A[j] equals average of S[0], ..., S[j]."""
24+
n = len(S)
25+
A = [0] * n # create new list of n zeros
26+
for j in range(n):
27+
total = 0 # begin computing S[0] + ... + S[j]
28+
for i in range(j + 1):
29+
total += S[i]
30+
A[j] = total / (j+1) # record the average
31+
return A
32+
33+
def prefix_average2(S):
34+
"""Return list such that, for all j, A[j] equals average of S[0], ..., S[j]."""
35+
n = len(S)
36+
A = [0] * n # create new list of n zeros
37+
for j in range(n):
38+
A[j] = sum(S[0:j+1]) / (j+1) # record the average
39+
return A
40+
41+
def prefix_average3(S):
42+
"""Return list such that, for all j, A[j] equals average of S[0], ..., S[j]."""
43+
n = len(S)
44+
A = [0] * n # create new list of n zeros
45+
total = 0 # compute prefix sum as S[0] + S[1] + ...
46+
for j in range(n):
47+
total += S[j] # update prefix sum to include S[j]
48+
A[j] = total / (j+1) # compute average based on current sum
49+
return A

ch03/unique.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2013, Michael H. Goldwasser
2+
#
3+
# Developed for use with the book:
4+
#
5+
# Data Structures and Algorithms in Python
6+
# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
7+
# John Wiley & Sons, 2013
8+
#
9+
# This program is free software: you can redistribute it and/or modify
10+
# it under the terms of the GNU General Public License as published by
11+
# the Free Software Foundation, either version 3 of the License, or
12+
# (at your option) any later version.
13+
#
14+
# This program is distributed in the hope that it will be useful,
15+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
# GNU General Public License for more details.
18+
#
19+
# You should have received a copy of the GNU General Public License
20+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
def unique1(S):
23+
"""Return True if there are no duplicate elements in sequence S."""
24+
for j in range(len(S)):
25+
for k in range(j+1, len(S)):
26+
if S[j] == S[k]:
27+
return False # found duplicate pair
28+
return True # if we reach this, elements were unique
29+
30+
def unique2(S):
31+
"""Return True if there are no duplicate elements in sequence S."""
32+
temp = sorted(S) # create a sorted copy of S
33+
for j in range(1, len(temp)):
34+
if S[j-1] == S[j]:
35+
return False # found duplicate pair
36+
return True # if we reach this, elements were unique

0 commit comments

Comments
 (0)