Skip to content

Instantly share code, notes, and snippets.

defmodule StrongestEvenNumber do
def strongest_even(n, m) do
calc_possible_strength(1, n, m, 1)
end
def calc_possible_strength(multiple, min, max, starting_value) do
multiple = multiple * 2
if multiple > max do
calc_possible_strength(starting_value + 1, min, max, starting_value + 1)
WITH RECURSIVE factorial(n, fact) -- Declare Recurisve Query with n and fact as outputs
AS (
SELECT -- Initial Query that sets everything up
0 AS n, -- 0 as n = starting index
1::NUMERIC(22,6) AS fact -- Configure 1 as the starting factorial and set as numeric type (0! == 1)
UNION ALL -- UNION ALL subqueries to the final result
SELECT
n + 1 AS n, -- Increment index n by one each run through
(fact * (n + 1))::NUMERIC(22,6) AS fact -- Calculate factorial of n utilizing the previous calculated factorial
FROM
class BST:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert(self, value):
current_node = self
while True:
if value < current_node.value:
if current_node.left is not None:
current_node = current_node.left
else:
current_node.left = BST(value)
break
else:
def contains(self, value):
current_node = self
while current_node is not None:
if value < current_node.value:
current_node = current_node.left
elif value > current_node.value:
current_node = current_node.right
else:
return True
return False
"""
10
/ \
5 15
/ \ / \
2 5 13 22
/ \
1 14
"""
class BST:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BST:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def remove(self, value, parent_node=None):
def remove(self, value, parent_node=None):
current_node = self
while current_node is not None:
# value is less than the current node's value look left
if value < current_node.value:
parent_node = current_node
current_node = current_node.left
# value is greater than the current node's value, look right
elif value > current_node.value:
parent_node = current_node
# find min value by keep going to the left till you can't anymore, then
# return the value of the selected node
def get_min_value(self):
current_node = self
while current_node.left is not None:
current_node = current_node.left
return current_node.value