from typing import List def combinations_with_repeat(weights: List[float], weight) -> int: # Здесь предполагается, что мы можем брать несколько грузиков одного веса. if not (weights and weight): return 0 n = 0 weights_sorted = sorted(weights, reverse=True) w = weights_sorted[0] if w > weight: return combinations(weights_sorted[1:], weight) n_w_max = int(weight / w) if not weight/w - n_w_max: n += 1 for i in range(n_w_max+1): n += combinations(weights_sorted[1:], weight - i*w) return n def combinations_without_repeat(weights: List[float], weight: float) -> int: # Здесь предполагается, что мы можем использовать каждый грузик только один раз if not (weights and weight): return 0 weights_sorted = sorted(weights, reverse=True) if weights_sorted[0] > weight: return combinations_without_repeat(weights_sorted[1:], weight) n = 0 if weights_sorted[0] == weight: n += 1 for i in range(2): n += combinations_without_repeat(weights_sorted[1:], weight - i*weights_sorted[0]) return n
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question