Skip to content

Instantly share code, notes, and snippets.

View theabbie's full-sized avatar
❤️
Playing With Life Since 2002

Abhishek Choudhary theabbie

❤️
Playing With Life Since 2002
View GitHub Profile
@theabbie
theabbie / double_or_one_thing.py
Created April 10, 2022 07:47
Double or one thing google codejam solution
t = int(input())
def getSmallest(word):
n = len(word)
if n == 1:
return word
nchunk = getSmallest(word[1:])
return min(word[0] + nchunk, word[0] * 2 + nchunk)
for tt in range(1, t + 1):
@theabbie
theabbie / huffman.py
Last active April 10, 2022 05:10
Huffman coding implementation
from collections import Counter
import heapq
class Node:
def __init__(self, char = None, left = None, right = None):
self.char = char
self.left = left
self.right = right
def __gt__(self, node):
@theabbie
theabbie / modexp.py
Last active April 10, 2022 03:50
Modular exponentiation by squaring algorithm
# modexp(b, n, M) => (b ^ n) % M
def modexp(b, n, M):
if n == 0:
return 1 % M
k = modexp(b, n >> 1, M) ** 2
if n & 1:
return (b * k) % M
else:
return k % M
@theabbie
theabbie / floating_circle.py
Created March 5, 2022 12:11
Dcoder Floating Circle Solution
r = input()
if float(r) <= 0:
print(0)
else:
print("{:.2f}".format(eval("3.14 * {} * {}".format(r, r))))
@theabbie
theabbie / perfect_square.py
Created March 5, 2022 04:17
Check If Perfect Square
def isPerfectSquare(num):
beg = 0
end = num
while beg <= end:
mid = (beg + end) // 2
if mid * mid == num:
return True
elif beg == end:
break
elif mid * mid > num:
@theabbie
theabbie / the_last_man_survived.py
Created March 4, 2022 15:09
The Last Man survived Dcoder Solution
t = int(input())
for _ in range(t):
n = int(input())
men = set(range(1, n + 1))
curr = 1
while len(men) > 1:
curr = 1 + curr % n
while curr not in men:
curr = 1 + curr % n
@theabbie
theabbie / pandas_average.py
Created March 4, 2022 13:43
Dcoder Panda's Average Solution
t = int(input())
for _ in range(t):
n = int(input())
arr = input().split()
arr = [int(el) for el in arr]
total = sum(arr)
sums = [0]
mindiff = float('inf')
for el in arr:
@theabbie
theabbie / coins_and_countries.py
Created March 4, 2022 13:20
Dcoder Coins & Countries Solution
t = int(input())
def numWays(n, r):
ways = 0
if r == 1:
return 1
if n == 0:
return ways
for i in range(1, n - r + 2):
ways += numWays(n - i, r - 1)
@theabbie
theabbie / codys_assignment.py
Created March 4, 2022 12:30
Dcoder Cody's Assignment Segment Tree Solution
seg = {}
def makeSeg(arr, i, j):
if (i, j) in seg:
return seg[(i, j)]
if i == j:
seg[(i, j)] = arr[i]
return arr[i]
mid = (i + j) // 2
curr = min(makeSeg(arr, i, mid), makeSeg(arr, mid + 1, j))
@theabbie
theabbie / cashback.py
Created March 4, 2022 11:54
Dcoder Cashback Question Incorrect Approach
from functools import cmp_to_key
n = int(input())
costs = []
for _ in range(n):
c, x = input().split()
costs.append((int(c), int(x)))