# code that calculates the sum of two binary numbers given as strings
# the result will print twice as I have done it the hard and informative, and the easy and correct ways
nums = ['$in1', '$in2']
# correct way to do this is use the lambda functionality with the key arg of the min/max functions
# my online python compiler does not support that functionality
def compArray(arr1, arr2):
if len(arr1) > len(arr2):
return [arr1, arr2]
return [arr2, arr1]
def binAdd(nums):
x, y = nums[0], nums[1]
x = list(x[::-1])
for i in range(len(x)): x[i] = int(x[i])
y = list(y[::-1])
for i in range(len(y)): y[i] = int(y[i])
smaller = compArray(x, y)[1]
greater = compArray(x, y)[0]
s = []
prev = 0
for i in range(len(smaller)):
d1 = smaller[i]
d2 = greater[i]
curr = d1 + d2
n = curr + prev - 2
d = 0
if n == 1 or n == -1:
d = 1
if n >= 0:
prev = 1
s.append(d)
for i in range(len(greater) - len(smaller)):
d1 = greater[i + len(smaller)]
n = d1 + prev
d = 0 # technically you do not need this, as the previous d is still accessible
if n < 2:
d = n
prev = 0
else:
d = n-2
prev = 1
s.append(d)
if(prev):
s.append(prev)
for i in range(len(s)): s[i] = str(s[i])
return(''.join(s[::-1]))
# the hard way
print(binAdd(nums))
# the right way
print(str(bin(int(nums[0], base=2) + int(nums[1], base=2))).replace('0b', ''))
# code that calculates the nth prime in-place
# less efficient than a sieve, but it is built on a function that verifies faster than the sieve could
# in1: nth prime to find (int)
def floor(n):
return int(n // 1)
def ciel(n):
if n - floor(n) > 0:
return floor(n) + 1
else:
return floor(n)
def sqrt(n):
return n**.5
def checkPrime(x):
j = ceil(sqrt(x))
i = 2
while i <= j:
if x%i == 0:
return False
i+=1
return True
i = 2
arr = 0
primes= 0
while(primes<$in1):
if checkPrime(i):
primes+=1
arr = i
i+=1
print(arr)
# code that verifies whether a number is divisible by 3
# this solution is dynamic, which means it is both more interesting and less functional than an iterative solution
# in1: number to verify (int)
def div3(n):
if n == 3 or n == 6 or n == 9:
return True
if n < 10:
return False
digits = str(n)
sumDigits = 0
for i in digits:
sumDigits += int(i)
return div3(sumDigits)
print($in1, div3($in1))
# code to calculate the minimum number of element swaps to order a list
# in2: elements to sort
# in1: correct position of each element of the corresponding index (list of ints)
def swap(arr, a, b):
arr[a], arr[b] = arr[b], arr[a]
return arr
def minSwaps(arr1, arr2):
arr = []
for i in range(len(arr1)):
arr.append((arr1[i], arr2[i]))
arrSorted = []
for i in arr: arrSorted.append(i)
arrSorted.sort()
swaps = 0
while arr != arrSorted:
for i in range(len(arr)):
if arr[i][1] != i:
arr = swap(arr, i, arr[i][1])
swaps +=1
return swaps
a = $in2
b = $in1
sorted = list(range(len(a)))
for i in range(len(a)):
sorted[b[i]] = a[i]
print(minSwaps(a, b, sorted))
def egz_dp(T):
n = (len(T) + 1)//2
# generate a 3d dp table of lists:
dp_table = [[[[] for i in range(n+1)] for j in range(n)] for k in range(len(T))]
# base case:
dp_table[0][T[0]%n][1] = [T[0]]
# for each other item in T:
for i in range(1, len(T)):
# for j from 0 to n:
for j in range(n):
# for k from 0 to n + 1:
for k in range(1, n+1):
# if the length of the previous element is k, then the current element is the previous
if len(dp_table[i - 1][j][k]) == k:
dp_table[i][j][k] = dp_table[i - 1][j][k]
# elseif this isn't the first k, and the len of the previous element was 1 less than k
elif k != 1 and len(dp_table[i - 1][j - T[i]%n][k-1]) == k - 1:
# then this list is the previous k list with the current i element appended
dp_table[i][j][k] = dp_table[i - 1][j - T[i]%n][k-1] + [T[i]]
#elseif this is the first k, and T[i] == j (mod n)
elif k == 1 and T[i]%n == j:
#then let the i,j,k list be T[i]
dp_table[i][j][k] = [T[i]]
#find the first subset generated that is of length n
for i in range(len(T)):
if len(dp_table[i][0][n]) == n:
return dp_table[i][0][n]
T = [-1, 1, 2, 3, 5, 7, 11, 13, 17, 23, 29]
n = (len(T) + 1) / 2
res = egz_dp(T)
print(res, sum(res)/n)