Skip to content

Commit dc74c3f

Browse files
author
Harsh Bhatia
committed
uploaded till chapter 10
1 parent 30b0dcc commit dc74c3f

21 files changed

+334
-0
lines changed

10_1.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def sumlist(lon):
2+
new=[]
3+
for i in range(0,len(lon)):
4+
new.append(sum(lon[0:i+1]))
5+
print(new)
6+
7+
s=[1,2,7,5]
8+
sumlist(s)

10_2.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def chop(word):
2+
word=word[1:]
3+
word=word[:-1]
4+
print(word)
5+
6+
l=[1,2,3,4,5]
7+
chop(l)

10_3.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def is_sorted(ser):
2+
ser1=ser[:]
3+
ser.sort()
4+
if ser==ser1:
5+
return True
6+
else:
7+
return False
8+
9+
s=['a','t','c']
10+
print(is_sorted(s))

10_4.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def is_anagrams(let1,let2):
2+
if len(let1)==len(let2):
3+
if sorted(let1)==sorted(let2):
4+
return True
5+
else:
6+
return False
7+
else:
8+
return False
9+
l='abrss'
10+
l1='abssr'
11+
print(is_anagrams(l,l1))

10_5a.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def has_duplicates(lost):
2+
lost.sort()
3+
for i in range(0,len(lost)-1):
4+
if lost[i]==lost[i+1]:
5+
return True
6+
7+
s=['h','a','r','s','h']
8+
print(has_duplicates(s))

10_6.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def remove_duplicates(lost):
2+
lost.sort()
3+
k=0
4+
for i in range(0,len(lost)-1):
5+
print(i+1)
6+
if lost[i]==lost[i+1]:
7+
lost[i]='0'
8+
k=k+1
9+
for i in range(0,k):
10+
lost.remove('0')
11+
return lost
12+
13+
s=['h','a','r','r','h','h','r']
14+
print(remove_duplicates(s))

10_7.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
Task: take input from file and convert into list word by word
3+
by slash
4+
python 3.3
5+
'''
6+
k=[]
7+
q=open("line.txt")
8+
w=q.readlines()
9+
for i in range(0,len(w)):
10+
f=w[i]
11+
f=f.strip()
12+
while True:
13+
r=f.find(' ')
14+
if r<0:
15+
z=f
16+
else:
17+
z=f[0:r]
18+
f=f[r+1:]
19+
#print(r)
20+
f.strip()
21+
k.append(z)
22+
if r<0:
23+
break;
24+
print(k)

3_5.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def straight():
2+
print ('-------',end="")
3+
4+
def mid():
5+
print ('|')
6+
7+
def space():
8+
print('| ',end="")
9+
10+
def plus():
11+
print("+",end="")
12+
13+
def plusend():
14+
print("+")
15+
16+
def first():
17+
plus()
18+
straight()
19+
plus()
20+
straight()
21+
plusend()
22+
23+
def second():
24+
space()
25+
space()
26+
mid()
27+
28+
def draw(n,m):
29+
for i in range(0,n):
30+
first()
31+
for j in range(0,m):
32+
second()
33+
34+
first()
35+
36+
37+
draw(2,3)
38+
39+
40+

6_4.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def b(z):
2+
prod = a(z,z)
3+
print(z,prod)
4+
return prod
5+
6+
def a(x,y):
7+
x=x+1
8+
return x*y
9+
10+
def c(x,y,z):
11+
sum = x+y+z
12+
pow = b(sum)**2
13+
return pow
14+
15+
x=1
16+
y=x+1
17+
print (c(x,y+3,x+y))

6_5.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def arc(m,n):
2+
if m==0:
3+
return n+1
4+
else:
5+
if m>0:
6+
if n==0:
7+
return arc(m-1,1)
8+
else:
9+
if n>0:
10+
return arc(m-1,arc(m,n-1))
11+
12+
r=arc(3,4)
13+
print(r)

6_7.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def is_divisible(a,b):
2+
if a%b == 0:
3+
return 'true'
4+
else:
5+
return 'false'
6+
7+
def power(a,b):
8+
c = a/b
9+
d=c/b
10+
if d==b or d==1:
11+
return 'true'
12+
else:
13+
return 'false'
14+
15+
def is_power(a,b):
16+
r=is_divisible(a,b)
17+
v=power(a,b)
18+
if r=='true':
19+
if v=='true':
20+
print('a is power of b')
21+
else:
22+
print('a is divisible by b but not power of b')
23+
else:
24+
if r=='false':
25+
print('a is nor divisible nor power of b')
26+
27+
28+
is_power(7,4)

6_8.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def gcd(a,b):
2+
if b==0:
3+
print("gcd is equal to",a)
4+
else:
5+
r=a%b
6+
return gcd(b,r)
7+
8+
9+
gcd(25,125)

7_4.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def eval_loop(s):
2+
print(eval(s))
3+
4+
while True:
5+
line = input('type "done" to exit')
6+
7+
if line == 'done':
8+
break
9+
10+
else:
11+
eval_loop(line)

8_12.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def rotate_word(s,n):
2+
for i in range(0,len(s)):
3+
n=int(n)
4+
k=ord(s[i])
5+
if k>64 and k<91:
6+
k=ord(s[i])+ n
7+
if k>90:
8+
k=65+(k-90)
9+
else:
10+
if k >96 and k<123:
11+
k=ord(s[i])+ n
12+
if k>122:
13+
k=97+(k-122)
14+
print(chr(k),end="")
15+
16+
rotate_word('Cheer','7')

8_9.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def is_palindrome(s):
2+
if s[::-1]==s:
3+
print('yes')
4+
else:
5+
print('no')
6+
7+
is_palindrome('apapa')

9_1.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fin=open('words.txt')
2+
for line in fin:
3+
word=line.strip()
4+
if len(word)>5:
5+
print(word)

9_2.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def has_no_e(n):
2+
for letter in n:
3+
if letter == 'e':
4+
return False
5+
return True
6+
7+
fin=open('words.txt')
8+
for line in fin:
9+
word=line.strip()
10+
r=has_no_e(word)
11+
if r==True:
12+
print(word)

9_3.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def has_no(n,e):
2+
for i in e:
3+
for letter in n:
4+
if letter == i:
5+
return False
6+
return True
7+
8+
9+
10+
t=input('PLease enter forbidden no')
11+
12+
fin=open('words.txt')
13+
for line in fin:
14+
word=line.strip()
15+
r=has_no(word,t)
16+
if r==True:
17+
print(word)

9_4.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def uses_all(word,test):
2+
for t in test:
3+
if t not in word:
4+
return False
5+
return True
6+
7+
print(uses_all('heeeeel','he'))

9_7.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
def check(word):
2+
r=len(word)
3+
i=0
4+
z=0
5+
if r>5:
6+
while r>1:
7+
#print(r)
8+
if word[i]==word[i+1]:
9+
r=r-2
10+
i=i+2
11+
z=z+1
12+
if z>1:
13+
return True
14+
#print("equal")
15+
else:
16+
#print("not equal")
17+
r=r-1
18+
i=i+1
19+
z=0
20+
#print(z)
21+
22+
else:
23+
#print("cannot possible")
24+
return False
25+
def equal(word,j):
26+
if word[j]==word[j+1]:
27+
return True
28+
29+
30+
count=0
31+
f=open("englishword.txt")
32+
'''
33+
k="bookkeeper"
34+
if check(k)==True:
35+
print(k)
36+
37+
'''
38+
q=0
39+
40+
for line in f:
41+
k=line.strip()
42+
#k=f.readline()
43+
if check(k)==True:
44+
print(k)
45+
else:
46+
q=q+1
47+
48+
print("total no of words checkedL:",end="")
49+
print(q)
50+
51+

9_8.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def odometer_palindrome(digit):
2+
q=len(digit)
3+
h=q/2
4+
h=int(h)
5+
q=q
6+
i=0
7+
r=1
8+
rev=digit[::-1]
9+
print(digit[i+r:h])
10+
print(rev[i+r:h])
11+
if digit[i:h]==rev[i+r:h+r]:
12+
print(q-r,"letter palindrome")
13+
else:
14+
print("nothing")
15+
16+
digit="612145"
17+
print(odometer_palindrome(digit))
18+
19+
#front palindrome : digit[i:h]==rev[i+r:h+r]

0 commit comments

Comments
 (0)