MCPcopy
hub / github.com/TheAlgorithms/Python / optimal_merge_pattern

Function optimal_merge_pattern

greedy_methods/optimal_merge_pattern.py:23–50  ·  view source on GitHub ↗

Function to merge all the files with optimum cost Args: files [list]: A list of sizes of different files to be merged Returns: optimal_merge_cost [int]: Optimal cost to merge all those files Examples: >>> optimal_merge_pattern([2, 3, 4]) 14 >>> optimal_merg

(files: list)

Source from the content-addressed store, hash-verified

21
22
23def optimal_merge_pattern(files: list) -> float:
24 """Function to merge all the files with optimum cost
25
26 Args:
27 files [list]: A list of sizes of different files to be merged
28
29 Returns:
30 optimal_merge_cost [int]: Optimal cost to merge all those files
31
32 Examples:
33 >>> optimal_merge_pattern([2, 3, 4])
34 14
35 >>> optimal_merge_pattern([5, 10, 20, 30, 30])
36 205
37 >>> optimal_merge_pattern([8, 8, 8, 8, 8])
38 96
39 """
40 optimal_merge_cost = 0
41 while len(files) > 1:
42 temp = 0
43 # Consider two files with minimum cost to be merged
44 for _ in range(2):
45 min_index = files.index(min(files))
46 temp += files[min_index]
47 files.pop(min_index)
48 files.append(temp)
49 optimal_merge_cost += temp
50 return optimal_merge_cost
51
52
53if __name__ == "__main__":

Callers

nothing calls this directly

Calls 2

popMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected