-
Notifications
You must be signed in to change notification settings - Fork 0
/
week1.py
128 lines (106 loc) · 2.77 KB
/
week1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#-------------------------CONCEPT 1-------------------------
#QUESTION-1
def missing_no(l):
l1=sorted(l)
for i in range(len(l1)-1): #check if each element is less than next element by 1,
if l1[i]==l1[i+1]-1: #if not, there is an element missing
pass
else:
return l1[i]+1
return 'No no. was missing'
print(missing_no([0,1,2,3,4,6,7,8,9]))
#QUESTION-2
def rotate_list(l,k):
for i in range(len(l)-k):
pop=l.pop(0) #removing first element
l.append(pop) #adding it to end of list
return l
print(rotate_list([0,1,2,3,4,5,6,7,8,9],5))
#QUESTION-3
def removing(l,r):
l1=[]
for i in l:
if i!=r:
l1.append(i)
return l1
print(removing([0,1,2,3,4,5,6,7,8,9],6))
#-------------------------CONCEPT 2-------------------------
#QUESTION-1
def sumvalue(d):
values=list(d.values())
return sum(values)
print(sumvalue({'a':10,'b':20,'c':30}))
#QUESTION-2
def duplicate(s):
sl=s.lower()
d={}
for i in sl:
if i in d.keys():
d[i]+=1
else:
d[i]=1
return d
print(duplicate('trappraa'))
#QUESTION-3
def unique(d):
val=list(d.values())
keys=list(d.keys())
most_unique_count=0
most_unique_ind=0
for i in val:
l1=[]
for j in i:
if j not in l1:
l1.append(j)
if len(l1)>most_unique_count:
most_unique_count=len(l1)
most_unique_ind=val.index(i)
return keys[most_unique_ind]
print(unique({"Python" : [5,7,9,0,0], "is":[6, 7, 4, 5, 3],"Best":[9,9,6,5,5]}))
#-------------------------CONCEPT 3-------------------------
#QUESTION-1
def max_min(t,k):
l=sorted(list(t))
out=[]
for i in range(k):
out.append(l[i])
out.append(l[len(l)-i-1])
return tuple(out)
print(max_min((3, 7, 1, 18, 9),2))
#QUESTON-2
def updating(l,n):
for i in range(len(l)):
a=[]
for j in l[i]:
a.append(j+n)
l[i]=tuple(a)
return l
print(updating([(1, 3, 4), (2, 4, 6), (3, 8, 1)],3))
#QUESTION-3
def remove_duplicate(t):
l=[]
for i in t:
if i not in l:
l.append(i)
return tuple(l)
print(remove_duplicate((1, 3, 5, 2, 3, 5, 1, 1, 3)))
#-------------------------CONCEPT 4-------------------------
#QUESTION-1
def palindrome(s):
pal=True
for i in range(len(s)):
if s[i]==s[len(s)-1-i]:
pass
else:
pal=False
break
if pal==True:
return 'A palindrome it is'
else:
return 'A palindrome it is not'
print(palindrome('noon'))
#QUESTION-2
def substring(s,ss):
l=s.split(ss)
return len(l)-1
print(substring('tCatBatSatFatOrt','t'))