Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cawibo authored Mar 23, 2021
1 parent ab5de86 commit 5995c05
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ If you want to uninstall algorithms, it is as simple as:
- [fibonacci](algorithms/dp/fib.py)
- [hosoya triangle](algorithms/dp/hosoya_triangle.py)
- [K-Factor_strings](algorithms/dp/k_factor.py)
- [planting_trees](algorithms/dp/planting_trees.py)
- [graph](algorithms/graph)
- [check_bipartite](algorithms/graph/check_bipartite.py)
- [strongly_connected](algorithms/graph/check_digraph_strongly_connected.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/dp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
from .word_break import *
from .int_divide import *
from .k_factor import *
from .planting_trees import *
45 changes: 45 additions & 0 deletions algorithms/dp/planting_trees.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
An even number of trees are left along one side of a country road. You've been assigned the job to
plant these trees at an even interval on both sides of the road. The length L and width W of the road
are variable, and a pair of trees must be planted at the beginning (at 0) and at the end (at L) of
the road. Only one tree can be moved at a time. The goal is to calculate the lowest amount of
distance that the trees have to be moved before they are all in a valid position.
"""

from math import sqrt
import sys

def planting_trees(trees, L, W):
"""
Returns the minimum distance that trees have to be moved before they are all in a valid state.
Parameters:
tree (list<int>): A sorted list of integers with all trees' position along the road.
L (int): An integer with the length of the road.
W (int): An integer with the width of the road.
Returns:
A float number with the total distance trees have been moved.
"""
trees = [0] + trees

n_pairs = int(len(trees)/2)

space_between_pairs = L/(n_pairs-1)

target_locations = [location*space_between_pairs for location in range(n_pairs)]

cmatrix = [[0 for _ in range(n_pairs+1)] for _ in range(n_pairs+1)]
for ri in range(1, n_pairs+1):
cmatrix[ri][0] = cmatrix[ri-1][0] + sqrt(W + abs(trees[ri]-target_locations[ri-1])**2)
for li in range(1, n_pairs+1):
cmatrix[0][li] = cmatrix[0][li-1] + abs(trees[li]-target_locations[li-1])

for ri in range(1, n_pairs+1):
for li in range(1, n_pairs+1):
cmatrix[ri][li] = min(
cmatrix[ri-1][li] + sqrt(W + (trees[li + ri]-target_locations[ri-1])**2),
cmatrix[ri][li-1] + abs(trees[li + ri]-target_locations[li-1])
)

return cmatrix[n_pairs][n_pairs]
27 changes: 26 additions & 1 deletion tests/test_dp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
longest_increasing_subsequence,
longest_increasing_subsequence_optimized,
longest_increasing_subsequence_optimized2,
int_divide,find_k_factor
int_divide,find_k_factor,
planting_trees
)


Expand Down Expand Up @@ -182,6 +183,30 @@ def test_kfactor(self):
k5=1
self.assertEqual(find_k_factor(n5,k5),71284044)

class TestPlantingTrees(unittest.TestCase):
def test_simple(self):
# arrange
trees = [0, 1, 10, 10]
L = 10
W = 1

# act
res = planting_trees(trees, L, W)

# assert
self.assertEqual(res, 2.414213562373095)

def test_simple2(self):
# arrange
trees = [0, 3, 5, 5, 6, 9]
L = 10
W = 1

# act
res = planting_trees(trees, L, W)

# assert
self.assertEqual(res, 9.28538328578604)

if __name__ == '__main__':
unittest.main()

0 comments on commit 5995c05

Please sign in to comment.