Skip to content

create solutions cleaner and solve 1,2,4,15 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion amazon_problems/problem_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
0 0 0 0 0
1 1 0 0 1
1 1 0 0 1
"""
"""
2 changes: 1 addition & 1 deletion amazon_problems/problem_11.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
/ \
22 35
You can assume each node has a parent pointer.
"""
"""
18 changes: 1 addition & 17 deletions amazon_problems/problem_12.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,4 @@
• init(size): initialize the array with size
• set(i, val): updates index at i with val where val is either 1 or 0.
• get(i): gets the value at index i.
"""

class BitArray:

def __init__(self, size):
self.data=[]
self.size = size
for i in range(size):
self.data.append(0)

def set(i , val):
if 0 <= size < self.size
self.data[i] = val

def get(i):
if 0 <= size < self.size
return data[i]
"""
17 changes: 1 addition & 16 deletions amazon_problems/problem_15.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,4 @@
For example, carrace should return true,
since it can be rearranged to form racecar, which is a palindrome.
daily should return false, since there's no rearrangement that can form a palindrome.
"""
#idea : we can't have more than one letter that has an odd frequency
def solve(s):
freq = [0] * 256
for c in s:
freq[ord(c)] += 1

cp = 0
for f in freq:
cp += f & 1

return cp <= 1


print(solve('daily')) # returns False
print(solve('carrace')) #returns True
"""
34 changes: 1 addition & 33 deletions amazon_problems/problem_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,4 @@
that contains at most k distinct characters.

For example, given s = "abcba" and k = 2, the longest substring with k
distinct characters is "bcb"."""


def main(s, k):
start = 0
current = ""
max_lengths = 0
while start < len(s):
for i in range(start + k - 1, len(s)):
ss = s[start:i + 1]
counter = count_character(ss)
if counter > k:
break
else:
if len(ss) > max_lengths:
max_lengths = len(ss)
current = ss
start += 1
return current


def count_character(string):
current = [string[0]]
counter = 1
for i in string:
if i not in current:
counter += 1
current.append(i)
return counter


# print(count_character("abbbcaabca"))
print(main("abbbcef", 3))
distinct characters is "bcb"."""
21 changes: 1 addition & 20 deletions amazon_problems/problem_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,4 @@
Implement run-length encoding and decoding.
You can assume the string to be encoded have no digits
and consists solely of alphabetic characters.
You can assume the string to be decoded is valid """


def main(string):
if len(string) == 0:
return ""
else:
current = string[0]
counter = 0
output = ""
for i in string:
if current == i:
counter += 1
else:
output += str(counter) + current
counter = 1
current = i
output += str(counter) + current
return output
print(main("AAAABBBCCDAA"))
You can assume the string to be decoded is valid """
9 changes: 1 addition & 8 deletions amazon_problems/problem_30.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,4 @@
and returns the number of digits the input has.

Constraint: don't use any loops.
"""

# idea: use the log of ten

import numpy as np

def number_of_digit(x):
return np.floor(np.log10(x,10)) + 1
"""
28 changes: 1 addition & 27 deletions amazon_problems/problem_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,4 @@
• max(), which returns the maximum value in the stack currently.
If there are no elements in the stack, then it should throw an error or return null.
Each method should run in constant time.
"""


class stack(object):
"""stack object for the solution"""

def __init__(self):
self.max = None
self.count = 0
self.array = []

def push(self, val):
self.array.append(val)
if val >= self.max:
self.max = val

def pop(self):
if len(self.array) == 0:
return None
else:
value = self.array.pop()
if value == self.max:
self.max = max(self.array)
return value

def max(self):
return self.max
"""
15 changes: 1 addition & 14 deletions amazon_problems/problem_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,4 @@
not take any elements.

Do this in O(N) time.
"""


def solve(arr):
mx = 0
sum = 0
for a in arr:
sum += a
if sum < 0: sum = 0
mx = max(mx, sum)
return mx

print(solve([34, -50, 42, 14, -5, 86])) # returns 137
print(solve([-5, -1, -8, -9])) # returns 0
"""
69 changes: 1 addition & 68 deletions amazon_problems/problem_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,71 +29,4 @@
9
14
13
12"""

a = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]]

b = [[1, 2, 3],
[4, 5, 6]]

c = [[1, 2, 3]]

d = [[1],
[2],
[3],
[4],
[5],
[6]]

e = [[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]]


def main():
matrix = e
rs = 0
re = len(matrix) - 1
cs = 0
ce = len(matrix[0]) - 1
ci = 0
cj = 0
total = len(matrix) * len(matrix[0])
now = 0
ori = 0

while now < total:

if ori == 0:
for cj in range(cs, ce + 1):
print(matrix[ci][cj])
now += 1
rs += 1
ori = 1

elif ori == 1:
for ci in range(rs, re + 1):
print(matrix[ci][cj])
now += 1
ce -= 1
ori = 2

elif ori == 2:
for cj in range(ce, cs - 1, -1):
print(matrix[ci][cj])
now += 1
re -= 1
ori = 3

elif ori == 3:
for ci in range(re, rs - 1, -1):
print(matrix[ci][cj])
now += 1
cs += 1
ori = 0


main()
12"""
16 changes: 1 addition & 15 deletions apple_problems/problem_1.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
"""This problem was asked by Apple.

Implement a job scheduler which takes in a function
f and an integer n, and calls f after n milliseconds."""

import time as t

def dummy():
print("I'm Called")

def job_scheduller(f,n):
start = t.time()*1000
end = start + n
while start < end:
start = t.time()*1000
f()

job_scheduller(dummy,1000)
f and an integer n, and calls f after n milliseconds."""
34 changes: 1 addition & 33 deletions apple_problems/problem_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,4 @@
Recall that a queue is a FIFO (first-in, first-out)
data structure with the following methods: enqueue, which
inserts an element into the queue, and dequeue, which removes it.
."""


class Queue:

def __init__(self):
self.inbounds = []
self.outbounds = []

def enqueue(self, data):
self.inbounds.append(data)

def dequeue(self):
if not self.outbounds:
while self.inbounds:
self.outbounds.append(self.inbounds.pop())
return self.outbounds.pop()


# Testing the Queue.
queue = Queue()
queue.enqueue(5)
queue.enqueue(6)
queue.enqueue(7)
print(queue.inbounds)

queue.dequeue()
print(queue.inbounds)
print(queue.outbounds)

queue.dequeue()
print(queue.inbounds)
print(queue.outbounds)
."""
2 changes: 1 addition & 1 deletion cisco_problems/problem_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
For example, 10101010 should be 01010101. 11100010 should be 11010001.

Bonus: Can you do this in one line?
"""
"""
35 changes: 35 additions & 0 deletions clean_solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

def get_files(skip=True):
files = os.listdir()
needed_folders = []
for file in files:
if skip:
if '.' in file or '0' in file or "LICENSE" == file or "data_structure" == file:
continue
needed_folders.append(file)
return needed_folders

def clean_content(files):
for file in files:
if '.py' in file:
file_obj = open(file, 'r')
content = file_obj.readlines()
content = ''.join(content[:])
task = content.split('"""')[1]
file_obj = open(file, 'w')
file_obj.truncate()
task = '"""' + task + '"""'
file_obj.write(task)

def remove_solutions():
needed_folders = get_files()
for folder in needed_folders:
os.chdir(folder)
files = get_files(skip=False)
clean_content(files)
os.chdir("..")

if __name__ == '__main__':
remove_solutions()

16 changes: 1 addition & 15 deletions context_logic_problems/problem_1.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
"""This question was asked by ContextLogic.

Implement division of two positive integers without using the division,
multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder."""

# main method for making division, quiet easy: just subtract second nomber for the first until 0. and count.
def division(a,b):
i=0
while a >= b:
i += 1
a = a-b
return i

print(division(6,2))
print(division(10,5))
print(division(20,5))
print(division(120,20))

multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder."""
Loading