Skip to content

Commit d8357b5

Browse files
committed
everything in my pc programsforpython folder
0 parents  commit d8357b5

189 files changed

Lines changed: 4457 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2and3sum.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
def twosum(l,target):
2+
d = {}
3+
for i in range(len(l)):
4+
y = target - l[i]
5+
if l[i] in d:
6+
return [d[l[i]],i]
7+
else:
8+
d[y] = i
9+
#print(twosum([3,5,9,2,11],13))
10+
11+
def threesum(l,target):
12+
res = []
13+
l.sort()
14+
for i in range(len(l)-2):
15+
if i!=0 and l[i] == l[i-1]:#corner case 1
16+
continue
17+
to_find = -l[i]
18+
low = i+1
19+
high = len(l)-1
20+
while low<high:
21+
sum = l[low]+l[high]
22+
if sum == to_find:
23+
res.append([l[i],l[low],l[high]])
24+
while low<high and l[low]==l[low+1]:
25+
low+=1
26+
while low<high and l[high]==l[high-1]:
27+
high-=1
28+
low+=1
29+
high-=1
30+
elif sum>to_find:
31+
high-=1
32+
else:
33+
low+=1
34+
return res
35+
"""
36+
nums.sort()
37+
res = []
38+
for i in range(len(nums)-2):
39+
target = nums[i]
40+
41+
#corne case 1
42+
if i!=0 and nums[i]==nums[i-1]:
43+
continue
44+
45+
to_find = -target
46+
47+
low = i+1
48+
high = len(nums)-1
49+
50+
while low<high:
51+
sum = nums[low]+nums[high]
52+
53+
if sum == to_find:
54+
res.append([nums[low],nums[high],target])
55+
56+
#corner case 2
57+
while low<high and nums[low] == nums[low+1]:
58+
low+=1
59+
while low<high and nums[high] == nums[high-1]:
60+
high-=1
61+
low+=1
62+
high-=1
63+
64+
elif sum < to_find:
65+
low+=1
66+
elif sum > to_find:
67+
high-=1
68+
return res
69+
"""
70+
print(threesum([-1,0,1,2,-1,-4],0))

54146485.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# cook your dish here
2+
for i in range(int(input())):
3+
n = int(input())
4+
5+
arr = list(map(int, input().split()))
6+
arr.sort()
7+
def work(arr,n):
8+
sum = 0
9+
for i in arr:
10+
sum+=i
11+
if sum%3 != 0:
12+
return -1
13+
i = 0
14+
j = n - 1
15+
moves = 0
16+
while i<j:
17+
ri = arr[i]%3
18+
rj = arr[j]%3
19+
20+
if rj == 0 and ri == 0:
21+
i+=1
22+
j-=1
23+
elif ri == 0 and rj!=0:
24+
i+=1
25+
elif rj == 0 and ri!=0:
26+
j-=1
27+
elif ri<rj:
28+
arr[i]-=1
29+
arr[j]+=1
30+
i+=1
31+
j-=1
32+
moves+=1
33+
elif ri > rj:
34+
arr[i]+=1
35+
arr[j]-=1
36+
j-=1
37+
i+=1
38+
moves+=1
39+
elif ri == rj and ri == 1:
40+
arr[i]-=1
41+
arr[j]+=1
42+
i+=1
43+
moves+=1
44+
elif ri == rj and ri == 2:
45+
arr[j]+=1
46+
arr[i]-=1
47+
j-=1
48+
moves+=1
49+
return moves
50+
51+
print(work(arr,n))

Add

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Add{
2+
public static void main(String[] args){
3+
int a=10;
4+
int b=20;
5+
int sum;
6+
sum=a+b;
7+
System.out.println(sum);
8+
}
9+
}

AdventOfCode/day1.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
file = open("C:/Users/iamop/OneDrive/Desktop/AOC/input.txt",'r')
2+
3+
arr = list(map(int, file.readlines()))
4+
c = 0
5+
for i in range(len(arr)-1):
6+
if arr[i]<arr[i+1]:
7+
c+=1
8+
print(c)
9+
10+
"""part two"""
11+
file = open("C:/Users/iamop/OneDrive/Desktop/AOC/input.txt",'r')
12+
13+
arr = list(map(int, file.readlines()))
14+
15+
currsum = 0
16+
prevsum = 0
17+
c = 0
18+
windowstart = 0
19+
for windowend in range(len(arr)):
20+
currsum+=arr[windowend]
21+
if (windowend - windowstart) == 2:
22+
if currsum>prevsum:
23+
c+=1
24+
prevsum = currsum
25+
currsum -= arr[windowstart]
26+
windowstart+=1
27+
print(c-1)

AdventOfCode/day10.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
f = open("C:/Users/iamop/OneDrive/Desktop/AOC/input9.txt","r")
2+
3+
x = f.read().splitlines()
4+
5+
"""pts = {')':3,"]":57,"}":1197,">":25137}
6+
7+
d = {')':'(',']':'[','}':'{','>':'<'}
8+
ans = 0
9+
for k in x:
10+
stack = []
11+
for i in k:
12+
if i in '{([<':
13+
stack.append(i)
14+
elif d[i] == stack[-1]:
15+
stack.pop()
16+
else:
17+
ans+=pts[i]
18+
break
19+
print(stack)"""
20+
21+
22+
"""part two"""
23+
pts2 = {')':1,']':2,'}':3,'>':4}
24+
d = {'(':')','[':']','{':'}','<':'>'}
25+
d2 = {')':'(',']':'[','}':'{','>':'<'}
26+
answers = []
27+
for k in x:
28+
stack = []
29+
wrongseq = False
30+
for i in k:
31+
if i in '{([<':
32+
stack.append(i)
33+
elif d2[i] == stack[-1]:
34+
stack.pop()
35+
else:
36+
wrongseq = True
37+
break
38+
if not wrongseq:
39+
total = 0
40+
for i in stack[::-1]:
41+
total*=5
42+
total+=pts2[d[i]]
43+
answers.append(total)
44+
45+
answers.sort()
46+
47+
mid_i = len(answers)//2
48+
print(len(answers))

AdventOfCode/day2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""file = open("C:/Users/iamop/OneDrive/Desktop/AOC/input2.txt",'r')
2+
3+
arr = list(map(str, file.readlines()))
4+
5+
final_h,final_d = 0,0
6+
7+
8+
for i in arr:
9+
_,d = i.split()
10+
if i[0] == 'f':
11+
final_h+=int(d)
12+
elif i[0] == 'u':
13+
final_d-=int(d)
14+
else:
15+
final_d+=int(d)
16+
print(final_h*final_d)
17+
"""
18+
"""part two"""
19+
file = open("C:/Users/iamop/OneDrive/Desktop/AOC/input2.txt",'r')
20+
21+
arr = list(map(str, file.readlines()))
22+
final_h,final_d = 0,0
23+
aim = 0
24+
25+
for i in arr:
26+
_,d = i.split()
27+
if i[0] == 'f':
28+
final_h+=int(d)
29+
final_d += int(d)*aim
30+
elif i[0] == 'u':
31+
aim-=int(d)
32+
else:
33+
aim+=int(d)
34+
print(final_h*final_d)

AdventOfCode/day3.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
def btod(binary):
2+
decimal = 0
3+
for digit in binary:
4+
decimal = decimal*2 + int(digit)
5+
return decimal
6+
f = open("C:/Users/iamop/OneDrive/Desktop/AOC/input3.txt","r")
7+
8+
arr = list(map(str, f.readlines()))
9+
arr1 = arr.copy()
10+
"""
11+
gamma = ''
12+
for i in range(len(arr[0])-1):
13+
c0,c1 = 0,0
14+
for j in arr:
15+
if j[i] == '1':
16+
c1+=1
17+
else:
18+
c0+=1
19+
if c1>c0:
20+
gamma+='1'
21+
else:
22+
gamma+='0'
23+
delta = ''
24+
for i in gamma:
25+
if i == '1':
26+
delta+='0'
27+
else:
28+
delta+='1'
29+
30+
31+
print(btod(gamma)*btod(delta))"""
32+
33+
34+
"""parrt two"""
35+
36+
"""for o2"""
37+
for i in range(len(arr[0])-1):
38+
temp1 = []
39+
temp2 = []
40+
c0,c1 = 0,0
41+
for j in arr:
42+
if j[i] == '1':
43+
c1+=1
44+
temp1.append(j)
45+
else:
46+
c0+=1
47+
temp2.append(j)
48+
if len(temp1) == 0 or len(temp2) == 0:
49+
break
50+
if c1>=c0:
51+
arr = temp1
52+
else:
53+
arr = temp2
54+
55+
"""for co2"""
56+
for i in range(len(arr1[0])-1):
57+
temp1 = []
58+
temp2 = []
59+
c0,c1 = 0,0
60+
for j in arr1:
61+
if j[i] == '1':
62+
c1+=1
63+
temp1.append(j)
64+
else:
65+
c0+=1
66+
temp2.append(j)
67+
if len(temp1) == 0 or len(temp2) == 0:
68+
break
69+
if c0<=c1:
70+
arr1 = temp2
71+
else:
72+
arr1 = temp1
73+
o2 = arr[0]
74+
co2 = arr1[0]
75+
76+
print(btod(o2[:len(o2)-1])*btod(co2[:len(co2)-1]))

0 commit comments

Comments
 (0)