Skip to content

Commit 7215b42

Browse files
committed
Fix keon#768
1 parent a8a3d70 commit 7215b42

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

algorithms/dp/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
from .word_break import *
2121
from .int_divide import *
2222
from .k_factor import *
23+
from .planting_trees import *

algorithms/dp/planting_trees.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
An even number of trees are left along one side of a country road. You've been assigned the job to
3+
plant these trees at an even interval on both sides of the road. The length L and width W of the road
4+
are variable, and a pair of trees must be planted at the beginning (at 0) and at the end (at L) of
5+
the road. Only one tree can be moved at a time. The goal is to calculate the lowest amount of
6+
distance that the trees have to be moved before they are all in a valid position.
7+
"""
8+
9+
from math import sqrt
10+
import sys
11+
12+
def planting_trees(trees, L, W):
13+
"""
14+
Returns the minimum distance that trees have to be moved before they are all in a valid state.
15+
16+
Parameters:
17+
tree (list<int>): A sorted list of integers with all trees' position along the road.
18+
L (int): An integer with the length of the road.
19+
W (int): An integer with the width of the road.
20+
21+
Returns:
22+
A float number with the total distance trees have been moved.
23+
"""
24+
trees = [0] + trees
25+
26+
n_pairs = int(len(trees)/2)
27+
28+
space_between_pairs = L/(n_pairs-1)
29+
30+
target_locations = [location*space_between_pairs for location in range(n_pairs)]
31+
32+
cmatrix = [[0 for _ in range(n_pairs+1)] for _ in range(n_pairs+1)]
33+
for ri in range(1, n_pairs+1):
34+
cmatrix[ri][0] = cmatrix[ri-1][0] + sqrt(W + abs(trees[ri]-target_locations[ri-1])**2)
35+
for li in range(1, n_pairs+1):
36+
cmatrix[0][li] = cmatrix[0][li-1] + abs(trees[li]-target_locations[li-1])
37+
38+
for ri in range(1, n_pairs+1):
39+
for li in range(1, n_pairs+1):
40+
cmatrix[ri][li] = min(
41+
cmatrix[ri-1][li] + sqrt(W + (trees[li + ri]-target_locations[ri-1])**2),
42+
cmatrix[ri][li-1] + abs(trees[li + ri]-target_locations[li-1])
43+
)
44+
45+
return cmatrix[n_pairs][n_pairs]

tests/test_dp.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
longest_increasing_subsequence,
1414
longest_increasing_subsequence_optimized,
1515
longest_increasing_subsequence_optimized2,
16-
int_divide,find_k_factor
16+
int_divide,find_k_factor,
17+
planting_trees
1718
)
1819

1920

@@ -182,6 +183,32 @@ def test_kfactor(self):
182183
k5=1
183184
self.assertEqual(find_k_factor(n5,k5),71284044)
184185

186+
class TestPlantingTrees(unittest.TestCase):
187+
def test_simple(self):
188+
# arrange
189+
trees = [0, 1, 10, 10]
190+
L = 10
191+
W = 1
192+
193+
# act
194+
res = planting_trees(trees, L, W)
195+
196+
print(res)
197+
# assert
198+
self.assertEqual(res, 2.414213562373095)
199+
200+
def test_simple2(self):
201+
# arrange
202+
trees = [0, 3, 5, 5, 6, 9]
203+
L = 10
204+
W = 1
205+
206+
# act
207+
res = planting_trees(trees, L, W)
208+
209+
print(res)
210+
# assert
211+
self.assertEqual(res, 9.28538328578604)
185212

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

0 commit comments

Comments
 (0)