feat: trapping rain water problem done
This commit is contained in:
@@ -20,3 +20,32 @@ def find_missing(input_aray: list[int]):
|
|||||||
return sum(get_array_range(1, len(input_aray) + 1)) - sum(input_aray)
|
return sum(get_array_range(1, len(input_aray) + 1)) - sum(input_aray)
|
||||||
|
|
||||||
def trap_rain_water(input_aray: list[int]):
|
def trap_rain_water(input_aray: list[int]):
|
||||||
|
ceiling = max(input_aray)
|
||||||
|
# Step 1: create a matrix with the graph
|
||||||
|
container = []
|
||||||
|
for wall_index in range(0, len(input_aray)):
|
||||||
|
wall = []
|
||||||
|
for coord_index in range(0, ceiling):
|
||||||
|
if (coord_index < input_aray[wall_index]):
|
||||||
|
wall.insert(coord_index, "w")
|
||||||
|
else:
|
||||||
|
wall.insert(coord_index, "a")
|
||||||
|
container.insert(wall_index, wall)
|
||||||
|
|
||||||
|
total_water = 0
|
||||||
|
# Step 2: fill the air with water
|
||||||
|
for wall_x in range(0, len(input_aray)):
|
||||||
|
for wall_y in range(0, input_aray[wall_x]):
|
||||||
|
if (container[wall_x][wall_y] == "a"):
|
||||||
|
continue
|
||||||
|
# Step 2.1 count distance with next wall
|
||||||
|
temp_water = 0
|
||||||
|
for water_x in range(wall_x + 1, len(input_aray)):
|
||||||
|
if (container[water_x][wall_y] == "a"):
|
||||||
|
temp_water += 1
|
||||||
|
else:
|
||||||
|
total_water += temp_water
|
||||||
|
break
|
||||||
|
|
||||||
|
return total_water
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,34 @@
|
|||||||
from math import sqrt, pi, e
|
from math import sqrt, pi, e
|
||||||
|
|
||||||
|
def mean(list):
|
||||||
|
return sum(list) / len(list)
|
||||||
|
|
||||||
|
def weighted_mean(items, weights):
|
||||||
|
if (len(items) != len(weights)):
|
||||||
|
return
|
||||||
|
total = 0
|
||||||
|
for i in range(len(items)):
|
||||||
|
total += items[i] * weights[i]
|
||||||
|
return total / sum(weights)
|
||||||
|
|
||||||
|
def weighted_mean_inline(items, weights):
|
||||||
|
return sum(s * w for s, w in zip(items, weights)) / sum(weights)
|
||||||
|
|
||||||
|
# also called 50% quantile
|
||||||
|
def median(items):
|
||||||
|
ordered = sorted(items)
|
||||||
|
length = len(ordered)
|
||||||
|
pair = length % 2 == 0
|
||||||
|
mid = int(length / 2) - 1 if pair else int(n/2)
|
||||||
|
|
||||||
|
if pair:
|
||||||
|
return (ordered[mid] + ordered[mid+1]) / 2
|
||||||
|
else:
|
||||||
|
return ordered[mid]
|
||||||
|
|
||||||
|
def mode(items):
|
||||||
|
sums = []
|
||||||
|
|
||||||
def population_variance(difference_list, mean):
|
def population_variance(difference_list, mean):
|
||||||
summatory = 0.0
|
summatory = 0.0
|
||||||
for diff in difference_list:
|
for diff in difference_list:
|
||||||
@@ -29,6 +58,12 @@ def probability_density_function(x: float, mean: float, standard_deviation: floa
|
|||||||
|
|
||||||
def test_statistics_module():
|
def test_statistics_module():
|
||||||
print("=== Statistics module ===")
|
print("=== Statistics module ===")
|
||||||
|
list = [ 1, 2, 3, 4, 5, 6]
|
||||||
|
print(">> The mean of {0} is {1}".format(list, mean(list)))
|
||||||
|
weights = [0.2, 0.5, 0.7, 1, 0, 0.9]
|
||||||
|
print(">> The weighted_mean of {0} is {1} and it is equivalent to {2}".format(list, weighted_mean(list, weights), weighted_mean_inline(list, weights)))
|
||||||
|
print(">> The mean is {0}".format(median(list)))
|
||||||
|
|
||||||
differences = [ -6.571, -5.571, -1.571, 0.429, 2.429, 3.429, 7.429 ]
|
differences = [ -6.571, -5.571, -1.571, 0.429, 2.429, 3.429, 7.429 ]
|
||||||
print("The population variance is", population_variance(differences, sum(differences) / len(differences)), population_variance_inline(differences))
|
print("The population variance is", population_variance(differences, sum(differences) / len(differences)), population_variance_inline(differences))
|
||||||
print("The standard deviation is", standard_deviation(differences, False))
|
print("The standard deviation is", standard_deviation(differences, False))
|
||||||
|
|||||||
@@ -25,3 +25,17 @@ def test_find_missing():
|
|||||||
result = find_missing(input)
|
result = find_missing(input)
|
||||||
expected = 4
|
expected = 4
|
||||||
assert result == expected
|
assert result == expected
|
||||||
|
|
||||||
|
def test_rain_watter():
|
||||||
|
input = [3,0,1,0,4,0,2]
|
||||||
|
expected = 10
|
||||||
|
result = trap_rain_water(input)
|
||||||
|
assert result == expected
|
||||||
|
input = [3,0,2,0,4]
|
||||||
|
expected = 7
|
||||||
|
result = trap_rain_water(input)
|
||||||
|
assert result == expected
|
||||||
|
input = [1,2,3,4]
|
||||||
|
expected = 0
|
||||||
|
result = trap_rain_water(input)
|
||||||
|
assert result == expected
|
||||||
|
|||||||
Reference in New Issue
Block a user