Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d0fc453

Browse files
Solutions
1 parent c90879a commit d0fc453

22 files changed

+991
-0
lines changed

‎CH8/Checkpoints

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#8.1
2+
(a) True
3+
(b) 3
4+
(c) True
5+
(d) True
6+
(e) True
7+
(f) False
8+
(g) True
9+
(h) WELCOME TO PYTHON
10+
(i) 8
11+
(j) o
12+
(k) ome
13+
(l) totototo
14+
(m) 17
15+
(n) y
16+
(o) (whitespace character)
17+
(p) t
18+
(q) welcome to python
19+
(r) 15
20+
(s) False
21+
(t) False
22+
(u) False
23+
(v) Welcome to PythonWelcome to Python
24+
25+
#8.2
26+
All of them are correct statements except the statement
27+
"s3 = s1 - s2" because '-' is not defined over strings.
28+
29+
#8.3
30+
Welcome to Python
31+
Welcabcme tabc Pythabcn
32+
33+
#8.4
34+
(a) isEqual = (s1 == s2)
35+
(b) isEqual = (s1.lower() == s2.lower())
36+
(c) b = s1.startswith('AAA')
37+
(d) b = s1.endswith("AAA")
38+
(e) x = len(s1)
39+
(f) x = s1[0]
40+
(g) s3 = s1+s2
41+
(h) s3 = s1[1:]
42+
(i) s3 = s1[1:5]
43+
(j) s3 = s1.lower()
44+
(k) s3 = s1.upper()
45+
(l) s3 = s1.strip()
46+
(m) s3 = s1.replace('e','E')
47+
(n) x = s1.find('e')
48+
(o) x = s1.rfind('abc')
49+
50+
#8.5
51+
No. The string object is immutable.
52+
53+
#8.6
54+
0
55+
56+
#8.7
57+
s.islower() or s.isupper()
58+
59+
#8.8
60+
s.isalpha()
61+
62+
#8.9
63+
Defining methods for operators is called operator overloading.
64+
Operator overloading allows the programmer to use the
65+
built-in operators for user-defined methods.
66+
67+
#8.10
68+
+: __add__(self, other)
69+
-: __sub__(self, other)
70+
*: __mul__(self, other)
71+
/: __truediv__(self, other)
72+
%: __mod__(self, other)
73+
==: __eq__(self, other)
74+
!=: __ne__(self, other)
75+
<: __lt__(self, other)
76+
<=: __le__(self, other)
77+
>: __gt__(self, other)
78+
>=: __ge__(self, other)
79+
80+
#8.11
81+
Yes.
82+
83+
#8.12
84+
Yes.

‎CH8/EX8.1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 8.1 (Check SSN) Write a program that prompts the user to enter a Social Security
2+
# number in the format ddd-dd-dddd, where d is a digit. The program displays
3+
# Valid SSN for a correct Social Security number or Invalid SSN otherwise.
4+
5+
ssn = input("Enter your social security number (ddd-dd-dddd): ")
6+
s1 = ssn[0:3]
7+
s2 = ssn[4:6]
8+
s3 = ssn[7:11]
9+
10+
if (len(s1)+len(s2)+len(s3) == 9) and s1.isdigit() and s2.isdigit() and s3.isdigit():
11+
print("SSN is valid")
12+
else:
13+
print("SSN is not valid")

‎CH8/EX8.10.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 8.10 (Decimal to binary) Write a function that parses a decimal number into a binary
2+
# number as a string. Use the function header:
3+
# def decimalToBinary(value):
4+
# Write a test program that prompts the user to enter a decimal integer value and displays
5+
# the corresponding binary value.
6+
7+
def decimalToBinary(value):
8+
bin = ''
9+
while value > 0:
10+
if value % 2 == 0:
11+
bin = bin + '0'
12+
else:
13+
bin = bin + '1'
14+
value = value // 2
15+
bin = bin[::-1]
16+
return bin
17+
18+
19+
def main():
20+
val = eval(input("Enter a decimal: "))
21+
bin = decimalToBinary(val)
22+
print(bin)
23+
24+
25+
main()

‎CH8/EX8.11.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 8.11 (Reverse a string) Write a function that reverses a string. The header of the function
2+
# is:
3+
# def reverse(s):
4+
# Write a test program that prompts the user to enter a string, invokes the reverse
5+
# function, and displays the reversed string.
6+
def reverse(s):
7+
new = s[len(s):0:-1] + s[0]
8+
return new
9+
10+
11+
def main():
12+
st = input("Enter a string: ")
13+
print("The reverse of", st, "is", reverse(st))
14+
15+
16+
main()

‎CH8/EX8.12.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 8.12 (Bioinformatics: find genes) Biologists use a sequence of letters A, C, T, and G to
2+
# model a genome. A gene is a substring of a genome that starts after a triplet ATG
3+
# and ends before a triplet TAG, TAA, or TGA. Furthermore, the length of a gene
4+
# string is a multiple of 3 and the gene does not contain any of the triplets ATG, TAG,
5+
# TAA, and TGA. Write a program that prompts the user to enter a genome and displays
6+
# all genes in the genome. If no gene is found in the input sequence, the program
7+
# displays no gene is found.
8+
9+
def findGen(genome):
10+
found = False
11+
for i in range(len(genome)):
12+
startInd = str(genome).find("ATG")
13+
if startInd == -1:
14+
break
15+
endInd1, endInd2, endInd3 = \
16+
str(genome).find("TAG"), str(genome).find("TAA"), str(genome).find("TGA")
17+
if endInd1 == -1:
18+
endInd1 = len(genome) * 2
19+
if endInd2 == -1:
20+
endInd2 = len(genome) * 2
21+
if endInd3 == -1:
22+
endInd3 = len(genome) * 2
23+
endInd = min(endInd1, endInd2, endInd3)
24+
gen = str(genome)[startInd + 3:endInd]
25+
if len(gen) % 3 == 0:
26+
print(gen)
27+
found = True
28+
genome = genome[endInd + 3:len(genome)]
29+
30+
if not found:
31+
print("No genes found")
32+
33+
34+
def main():
35+
g = input("Enter a genome string: ")
36+
findGen(g)
37+
38+
39+
main()

‎CH8/EX8.13.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 8.13 (Longest common prefix) Write a method that returns the longest common prefix
2+
# of two strings. For example, the longest common prefix of distance and
3+
# disinfection is dis. The header of the method is:
4+
# def prefix(s1, s2)
5+
# If the two strings have no common prefix, the method returns an empty string.
6+
# Write a main method that prompts the user to enter two strings and displays their
7+
# common prefix.
8+
9+
def prefix(s1, s2):
10+
for i in range(len(s1)):
11+
s = s1[0:len(s1) - i]
12+
if str(s2).startswith(str(s)):
13+
return s
14+
15+
return ""
16+
17+
18+
def main():
19+
s1, s2 = input("Enter two strings: ").split()
20+
print(prefix(s1, s2))
21+
22+
23+
main()

‎CH8/EX8.14.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 8.14 (Financial: credit card number validation) Rewrite Exercise 6.29 using a string
2+
# input for a credit card number.
3+
4+
5+
def main():
6+
number = input("Enter a credit card number as a string: ").strip()
7+
8+
if isValid(number):
9+
print(number + " is valid")
10+
else:
11+
print(number + " is invalid")
12+
13+
14+
# Return true if the card number is valid
15+
def isValid(number):
16+
return (number.startswith("4") or number.startswith("5") or
17+
number.startswith("6") or number.startswith("37")) and \
18+
(sumOfDoubleEvenPlace(number) + sumOfOddPlace(number)) % 10 == 0
19+
20+
21+
# Get the result from Step 2
22+
def sumOfDoubleEvenPlace(cardNumber):
23+
result = 0
24+
25+
for i in range(len(cardNumber) - 2, -1, - 2):
26+
result += getDigit(int(cardNumber[i]) * 2)
27+
28+
return result
29+
30+
31+
# Return this number if it is a single digit, otherwise, return
32+
# the sum of the two digits
33+
def getDigit(number):
34+
return number % 10 + (number // 10 % 10)
35+
36+
37+
# Return sum of odd place digits in number
38+
def sumOfOddPlace(cardNumber):
39+
result = 0
40+
41+
for i in range(len(cardNumber) - 1, -1, -2):
42+
result += int(cardNumber[i])
43+
44+
return result
45+
46+
47+
main()

‎CH8/EX8.15.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 8.15 (Business: check ISBN-10) An ISBN-10 (International Standard Book Number)
2+
# consists of 10 digits:d1d2d3d4d5d6d7d8d9d10. The last digit, is a checksum,
3+
# which is calculated from the other nine digits using the following formula:
4+
# (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5
5+
# + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11
6+
# If the checksum is 10, the last digit is denoted as X, according to the ISBN convention.
7+
# Write a program that prompts the user to enter the first 9 digits as a string
8+
# and displays the 10-digit ISBN (including leading zeros). Your program should
9+
# read the input as a string.
10+
11+
digits = input("Enter the first 9 digits of an ISBN-10 as a string: ")
12+
if digits.isdigit():
13+
lastDigit = 0
14+
for i in range(9):
15+
lastDigit += int(digits[i]) * (i+1)
16+
lastDigit = lastDigit % 11
17+
isbn = ''
18+
if lastDigit == 10:
19+
isbn = digits + 'X'
20+
else:
21+
isbn = digits + str(lastDigit)
22+
23+
print("The ISBN-10 number is", isbn)
24+
else:
25+
print("incorrect input")

‎CH8/EX8.16.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 8.16 (Business: check ISBN-13) ISBN-13 is a new standard for identifying books. It
2+
# uses 13 digits: d1d2d3d4d5d6d7d8d9d10d11d12d13. The last digit, is a checksum,
3+
# which is calculated from the other digits using the following formula:
4+
# 10 - (d1 + 3d2 + d3 + 3d4 + d5 + 3d6 + d7 + 3d8 + d9 + 3d10 + d11 + 3d12) % 10
5+
# If the checksum is 10, replace it with 0. Your program should read the input as a
6+
# string.
7+
8+
digits = input("Enter the first 12 digits of an ISBN-13 as a string: ")
9+
10+
checksum = 0
11+
for i in range(12):
12+
checksum += int(digits[i]) * (1 if i % 2 == 0 else 3)
13+
14+
checksum = 10 - checksum % 10
15+
if checksum == 10:
16+
digits += '0'
17+
else:
18+
digits += str(checksum)
19+
20+
print("The ISBN-13 number is", digits)

‎CH8/EX8.17.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 8.17 (The Point class) Design a class named Point to represent a point with x- and ycoordinates.
2+
# The class contains:
3+
# しかく Two private data fields x and y that represent the coordinates with get methods.
4+
# しかく A constructor that constructs a point with specified coordinates with default
5+
# point (0, 0).
6+
# しかく A method named distance that returns the distance from this point to another
7+
# point of the Point type.
8+
# しかく A method named isNearBy(p1) that returns true if point p1 is close to this
9+
# point. Two points are close if their distance is less than 5.
10+
# しかく Implement the __str__ method to return a string in the form (x, y).
11+
# Draw the UML diagram for the class, and then implement the class. Write a test
12+
# program that prompts the user to enter two points, displays the distance between
13+
# them, and indicates whether they are near each other.
14+
import math
15+
16+
17+
class Point:
18+
def __init__(self, x=0, y=0):
19+
self.__x__ = x
20+
self.__y__ = y
21+
22+
def getX(self):
23+
return self.__x__
24+
25+
def getY(self):
26+
return self.__y__
27+
28+
def distance(self, point):
29+
dis = math.sqrt((self.__x__ - point.getX()) ** 2 + (self.__y__ - point.getY()) ** 2)
30+
return dis
31+
32+
def isNearBy(self, point):
33+
d = self.distance(point)
34+
return True if d < 5 else False
35+
36+
def __str__(self):
37+
return '( ' + str(self.__x__) + ',' + str(self.__y__) + ' )'
38+
39+
40+
def main():
41+
x1, y1, x2, y2 = eval(input("Enter two points x1, y1, x2, y2: "))
42+
p1 = Point(x1, y1)
43+
p2 = Point(x2, y2)
44+
print("The distance between the two points is", p1.distance(p2))
45+
if p1.isNearBy(p2):
46+
print("The two points are near each other")
47+
else:
48+
print("The two points are not near each other")
49+
50+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /