Skip to content

Commit 8b80600

Browse files
ch 7:small also added
1 parent dc74c3f commit 8b80600

File tree

9 files changed

+187
-0
lines changed

9 files changed

+187
-0
lines changed

3_5.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
Chapter 3
6+
'''
17
def straight():
28
print ('-------',end="")
39

6_1.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''Exercise 6.1
7+
Write a compare function that returns 1 if x > y, 0 if x == y, and -1 if x < y.'''
8+
def compare(x,y):
9+
if x>y:
10+
return 1
11+
elif x==y:
12+
return 0
13+
else:
14+
return -1
15+
16+
print compare(2,1)
17+
print compare(1,1)
18+
print compare(1,3)

6_2.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''Exercise 6.2
7+
Use incremental development to write a function called hypotenuse that returns
8+
the length of the hypotenuse of a right triangle given the lengths of the two legs as
9+
arguments. Record each stage of the development process as you go'''
10+
import math
11+
def hypotenuse(x,y):
12+
x=x*x
13+
# print x
14+
y=y*y
15+
print math.sqrt(x+y)
16+
17+
hypotenuse(3,4)

6_3.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''
7+
Exercise: 6.3
8+
'''
9+
10+
def is_between(x,y,z):
11+
if x<=y:
12+
if y<=z:
13+
return "True"
14+
else:
15+
return "False"
16+
else:
17+
return "False"
18+
19+
print is_between(3,4,2)

6_6.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''
7+
Exercise: 6.6: incomplete
8+
'''
9+
def first(word):
10+
return word[0]
11+
12+
def last(word):
13+
return word[-1]
14+
15+
def middle(word):
16+
return word[1:-1]
17+
18+
print middle("aa")
19+
print middle("b")
20+
print middle('')
21+
22+
def is_palindrome(word):
23+
wordl=int(len(word))
24+
if (wordl%2==0):
25+
print "even"
26+
else:
27+
for i in wordl:
28+
if word[i]==word[-i]:
29+
return 0
30+
else:
31+
return 1
32+
33+
print is_palindrome("lol")

7_1.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''
7+
Exercise 7.1: Rewrite the function print_n from Section 5.8 using iteration instead of recursion.
8+
'''
9+
def print_n(s, n):
10+
while n != 0:
11+
print s
12+
n=n-1
13+
14+
print_n(2,7)

7_2.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''
7+
Exercise 7.2:Encapsulate loop in a function called square_root that takes a as a parameter,
8+
chooses a reasonable value of x, and returns an estimate of the square root of a.
9+
'''
10+
import math
11+
def square_root(a):
12+
x = 10
13+
while True:
14+
# print x
15+
y = (x + a/x) / 2
16+
if abs(y- x )< 0.000001:
17+
break
18+
x = y
19+
return x
20+
21+
print square_root(4)

7_3.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''
7+
INCOMPLETE
8+
Exercise 7.2:Encapsulate loop in a function called square_root that takes a as a parameter,
9+
chooses a reasonable value of x, and returns an estimate of the square root of a.
10+
'''
11+
import math
12+
def square_root(a):
13+
x = 3
14+
while True:
15+
# print x
16+
y = (x + a/x) / 2
17+
if abs(y- x )< 0.001:
18+
break
19+
x = y
20+
return x
21+
22+
def test_square_root():
23+
i=1
24+
while i<10:
25+
print i
26+
print square_root(i)
27+
i=i+1
28+
29+
# test_square_root()
30+
print square_root(3)

Chapter1to5.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Complete soltion of Book
3+
"Python For Software Design by Allen B. Downey"
4+
by Harsh Bhatia ( www.harshbhatia.net )
5+
'''
6+
'''
7+
Exercise 1.1 :
8+
Write a well-structured English sentence with invalid tokens in it. Then write another
9+
sentence with all valid tokens but with invalid structure.
10+
Sol:
11+
I have $22 in my pocket.
12+
Total Profit = $-100 .
13+
14+
Exercise 2.1 :
15+
Sol:
16+
010 = 8
17+
0100 = 64
18+
01000 = 512
19+
01001 = 513
20+
21+
.
22+
.
23+
.
24+
25+
NOTE :
26+
Chapter 1- 5 do not cotain any queations that might put a little strain on brain, so are not included in this solution book, If you are stuck in one please let me know.
27+
28+
'''
29+

0 commit comments

Comments
 (0)