Skip to content

Commit cdd47f7

Browse files
Add files via upload
1 parent 8cf5b38 commit cdd47f7

10 files changed

+192
-0
lines changed

Collections Deque using eval.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
n= input()
4+
d= deque()
5+
code= [raw_input().split() for i in range(n)]
6+
for i in code:
7+
if len(i)> 1:
8+
val= i[-1]
9+
op= i[0] + "(" + val + ")"
10+
eval('d.'+ op)
11+
else:
12+
eval('d.'+ i[0] +"()")
13+
print " ".join(map(str, d))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
N = int(raw_input())
4+
assert N>=1 and N<=5
5+
cubes=[]
6+
for i in range(N):
7+
n = int(raw_input())
8+
assert n>=1 and n<=10**5
9+
cubes.append((map(int, raw_input().split())))
10+
11+
for i in cubes:
12+
d= deque(i)
13+
ans= "Yes"
14+
vp=[1000000000000]
15+
while (ans=="Yes" and len(d)>1):
16+
if d[0] <= d[-1]:
17+
k=d.pop()
18+
if k > vp[-1]:
19+
ans= "No"
20+
else:
21+
k=d.popleft()
22+
if k > vp[-1]:
23+
ans= "No"
24+
25+
vp.append(k)
26+
if len(d)==1:
27+
if d[0]> vp[-1]:
28+
ans= "No"
29+
print ans
30+
31+

Collections Moderate Problem.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
N= input()
4+
d= OrderedDict()
5+
words= [raw_input() for i in range(N)]
6+
counter= Counter(words)
7+
print len(counter)
8+
for i in words:
9+
d[i]= counter[i]
10+
print " ".join(map(str,d.values()))
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
s= raw_input()
4+
string= [i for i in s]
5+
counter= Counter(string)
6+
string= sorted(set(string))
7+
for i in range(3):
8+
for j in string:
9+
if counter[j]== max(counter.values()):
10+
print j+" "+ str(max(counter.values()))
11+
counter.pop(j)
12+
break
13+
print counter
14+

Collections Named Tuple.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
N,students= input(), namedtuple('students',raw_input().split())
4+
stud= [students(*raw_input().split()) for i in range(N)] #passed string of input as a single key
5+
print sum([float (i.MARKS) for i in stud]) /N
6+
7+
8+
'''
9+
Code 01
10+
11+
>>> from collections import namedtuple
12+
>>> Point = namedtuple('Point','x,y')
13+
>>> pt1 = Point(1,2)
14+
>>> pt2 = Point(3,4)
15+
>>> dot_product = ( pt1.x * pt2.x ) +( pt1.y * pt2.y )
16+
>>> print dot_product
17+
11
18+
Code 02
19+
20+
>>> from collections import namedtuple
21+
>>> Car = namedtuple('Car','Price Mileage Colour Class')
22+
>>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y')
23+
>>> print xyz
24+
Car(Price=100000, Mileage=30, Colour='Cyan', Class='Y')
25+
>>> print xyz.Class
26+
Y
27+
28+
29+
'''

Collections OrderedDict.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
N= input()
4+
d= OrderedDict()
5+
for i in range(N):
6+
item = raw_input().split()
7+
itemPrice= int(item[-1])
8+
itemName= " ".join(item[:-1])
9+
if d.get(itemName): # .get is used to check if itemName already exists
10+
d[itemName] += itemPrice
11+
else:
12+
d[itemName] = itemPrice
13+
for i in d.keys():
14+
print i, d[i]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import deque
3+
4+
def stackable(cubes):
5+
curr = cubes.popleft() if cubes[0] > cubes[-1] else cubes.pop()
6+
while cubes:
7+
left, right = cubes[0], cubes[-1]
8+
if left >= right and left <= curr:
9+
curr = cubes.popleft()
10+
elif right > left and right <= curr:
11+
curr = cubes.pop()
12+
else:
13+
return False
14+
return True
15+
16+
for i in range(int(raw_input())):
17+
n, cubes = input(), deque(map(int, raw_input().split()))
18+
print('Yes' if stackable(cubes) else 'No')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
N = int(raw_input())
4+
assert N>=1 and N<=5
5+
cubes=[]
6+
for i in range(N):
7+
n = int(raw_input())
8+
assert n>=1 and n<=10**5
9+
cubes.append((map(int, raw_input().split())))
10+
11+
for i in cubes:
12+
d= deque(i)
13+
ans= "Yes"
14+
vp=[1000000000000]
15+
while (ans=="Yes" and len(d)>1):
16+
if d[0] <= d[-1]:
17+
k=d.pop()
18+
if k > vp[-1]:
19+
ans= "No"
20+
else:
21+
k=d.popleft()
22+
if k > vp[-1]:
23+
ans= "No"
24+
25+
vp.append(k)
26+
if len(d)==1:
27+
if d[0]> vp[-1]:
28+
ans= "No"
29+
print ans
30+
31+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
X=input()
4+
shoeSizes=map(int, raw_input().split())
5+
shoeSizes=Counter(shoeSizes)
6+
N= input()
7+
customer= [ map(int, raw_input().split()) for i in range(N)]
8+
money=0
9+
for i in customer:
10+
if (i[0] in shoeSizes) and ( shoeSizes[i[0]] > 0) :
11+
money+=i[1]
12+
shoeSizes[i[0]]-=1
13+
14+
print money

Collections defaultdict.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
from collections import *
3+
n, m = map(int, raw_input().split())
4+
A= [raw_input() for i in range(n)]
5+
B= [raw_input() for i in range(m)]
6+
d= defaultdict(list)
7+
index=0
8+
for i in A:
9+
d[i].append( A.index(i, index) + 1)
10+
index+=1
11+
for i in B:
12+
if i not in A:
13+
print -1
14+
else:
15+
print " ".join(map(str, d[i])) # print a list of elements with uniform space/gap
16+
17+

0 commit comments

Comments
 (0)