Skip to content

Commit f0109b2

Browse files
committed
Codechef problem solutions
1 parent daa4801 commit f0109b2

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

Codechef solutions/INPSTFIX.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
def fun(n, eq):
3+
eq = [i for i in eq]
4+
ans = []
5+
stack = []
6+
precedence = {'(':0,'+':1,'-':1,'*':2,'/':2,'^':3}
7+
for i in eq:
8+
if i == '(':
9+
stack.append(i)
10+
elif i >= 'A' and i <= 'Z':
11+
ans.append(i)
12+
elif i == ')':
13+
while len(stack)> 0 and stack[-1] != '(':
14+
ans.append(stack.pop())
15+
stack.pop()
16+
elif i in '+-*/^':
17+
if len(stack)==0 or stack[-1] == '(':
18+
stack.append(i)
19+
else:
20+
while len(stack)> 0 and precedence[i] <= precedence[stack[-1]]:
21+
ans.append(stack.pop())
22+
else:
23+
stack.append(i)
24+
25+
for i in stack[::-1]:
26+
if i != '(':
27+
ans.append(i)
28+
res = ''
29+
for i in ans:
30+
res += i
31+
return res
32+
33+
for testcase in range(int(input())):
34+
print(fun(int(input()), input()))

Codechef solutions/ZCO12002.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from sys import stdin,stdout
2+
3+
4+
def upper_bound(num,exit):
5+
6+
l = 0
7+
r = len(exit)-1
8+
temp = None
9+
10+
while l < r:
11+
mid = (l+r)//2
12+
13+
if exit[mid] == num:
14+
return exit[mid]
15+
16+
elif exit[mid] > num:
17+
temp = exit[mid]
18+
r = mid
19+
20+
else:
21+
l = mid+1
22+
23+
if exit[r] >= num:
24+
return exit[r]
25+
26+
return temp
27+
28+
def lower_bound(num,entry):
29+
30+
l = 0
31+
r = len(entry)-1
32+
temp = None
33+
34+
while l < r:
35+
mid = (l + r) // 2
36+
37+
if entry[mid] > num:
38+
r = mid
39+
40+
elif entry[mid] == num:
41+
return entry[mid]
42+
43+
else:
44+
temp = entry[mid]
45+
l = mid+1
46+
47+
if entry[l] <= num:
48+
return entry[l]
49+
50+
return temp
51+
52+
53+
num_contests,num_entry,num_exit = map(int,stdin.readline().split())
54+
contests = []
55+
56+
for c in range(num_contests):
57+
v,w = map(int,stdin.readline().split())
58+
contests.append([v,w])
59+
60+
entry = sorted(list(map(int,stdin.readline().split())))
61+
exits = sorted(list(map(int, stdin.readline().split())))
62+
63+
max_tym = float('inf')
64+
65+
for con in range(num_contests):
66+
temp1 = upper_bound(contests[con][1],exits)
67+
temp2 = lower_bound(contests[con][0],entry)
68+
69+
if temp1 and temp2:
70+
if (temp1 - temp2) + 1 < max_tym:
71+
max_tym = (temp1 - temp2) + 1
72+
73+
74+
print(max_tym)

0 commit comments

Comments
 (0)