Skip to content

Instantly share code, notes, and snippets.

View les-peters's full-sized avatar

Les Peters les-peters

View GitHub Profile
@les-peters
les-peters / N-from-D.py
Last active March 6, 2020 19:36
Cassidoo 2020-03-02
# Write a function that when given a positive integer n and a digit d (which is not 0),
# outputs a way to represent n using only addition, subtraction, multiplication,
# exponentiation, division, concatenation, parenthesis and the digit d.
# Example:
# n = 6, d = 1 => 1 + 1 + 1 + 1 + 1 + 1
# n = 10, d = 1 => 1 | 1 - 1
# n = 12, d = 1 => 1 | (1 + 1)
# n = 64, d = 2 => 2 ^ (2 + 2 + 2)
# n = 1, d = 9 => 9 / 9
@les-peters
les-peters / mean_median_mode.py
Created March 11, 2020 15:04
Cassidoo IQofW 2020-03-09
# This week's question:
# Given an array of unsorted integers, return the mean, median, and mode.
import math
from functools import reduce
def mean_median_mode(N):
print('given', N)
SN = sorted(N)
mean = 0
@les-peters
les-peters / moveZeros.py
Created August 17, 2020 18:02
Cassidoo 2020-08-17
def moveZeros(a):
a = [1, 2, 0, 1, 0, 0, 3, 6]
b = []
c = []
for i in a:
if i:
b.append(i)
else:
c.append(i)
@les-peters
les-peters / 2020-09-28.py
Created September 28, 2020 11:55
Pizza Planet!
import math
def gimmePizza(consumers, slices_per_pizza):
slices_to_consume = 0
for consumer in consumers:
slices_to_consume += consumer['num']
pizzas = math.ceil((slices_to_consume / slices_per_pizza))
return pizzas
def pickyEaters(consumers, slices_per_pizza):
@les-peters
les-peters / find_pivot_point.py
Created October 10, 2020 13:06
Cassidoo 2020-10-05
question = """
Given an array that was once sorted in ascending order is rotated at some
pivot unknown to you beforehand (so [0,2,4,7,9] might become [7,9,0,2,4], for
example). Find the minimum value in that array in O(n) or less.
"""
def find_pivot_point(a):
start = 0
end = len(a) - 1
mid = int((start + end) / 2)
@les-peters
les-peters / 2020-10-12.py
Created October 12, 2020 13:29
Lisp math simulator
import re
question = """
Given a basic Lisp-like string expression, parse it (where the available
functions are add, subtract, multiply, and divide, and they all take in 2 values).
"""
def babyLisp(str):
ops = {
'add': '+',
@les-peters
les-peters / 2020-10-26.py
Created October 26, 2020 13:42
Sort by bits
import re
arr = [0,1,2,3,4,5,6,7,8]
arr.sort(key=lambda x: len(re.sub(r'0','', "{0:b}".format(x))))
print(arr)
@les-peters
les-peters / 2020-11-02.py
Created November 2, 2020 20:41
Safe-to-Jump
from functools import reduce
def characterJump(jump_length, road):
return True if reduce((lambda x, y: x + y), list(map(lambda x: road[x], list(filter(lambda x: x % jump_length == 0, range(0, len(road))))))) == 0 else False
print(characterJump(3, [0,1,0,0,0,1,0]))
print(characterJump(4, [0,1,1,0,1,0,0,0,0]))
@les-peters
les-peters / 2020-11-23.py
Last active November 24, 2020 21:30
perfect_square
import re
question = """
Given a positive integer n, write a function that returns true if
it is a perfect square and false otherwise. Don’t use any built-in
math functions like sqrt. Hint: Use binary search!
Examples:
$ perfectSquare(25)
@les-peters
les-peters / 2020-12-07.py
Created December 7, 2020 19:06
String Multiply without Multiplying
import re
from itertools import cycle, islice
question = """
Given two non-negative integers n1 and n2 represented as strings,
return the product of n1 and n2, also represented as a string.
Neither input will start with 0, and don’t just convert it to an
integer and do the math that way.