Skip to content

Commit 282f22a

Browse files
committed
-
1 parent b9f7044 commit 282f22a

File tree

2 files changed

+107
-101
lines changed

2 files changed

+107
-101
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import numbers
2+
import collections
3+
import itertools
4+
5+
infinity = float('inf')
6+
7+
###############################################################################
8+
9+
_length_of_recurrent_perm_space_cache = {}
10+
11+
def calculate_length_of_recurrent_perm_space(k, ftt):
12+
from python_toolbox import nifty_collections
13+
from python_toolbox import cute_iter_tools
14+
cache = _length_of_recurrent_perm_space_cache
15+
if not isinstance(ftt, nifty_collections.FrozenTallyTally):
16+
ftt = nifty_collections.FrozenTallyTally(ftt)
17+
if k == 0:
18+
return 1
19+
elif k == 1:
20+
assert ftt
21+
# (Works because `FrozenTallyTally` has a functioning `__bool__`,
22+
# unlike Python's `Counter`.)
23+
return ftt.n_elements
24+
try:
25+
return cache[(k, ftt)]
26+
except KeyError:
27+
pass
28+
29+
levels = []
30+
current_ftts = {ftt}
31+
while len(levels) < k and current_ftts:
32+
k_ = k - len(levels)
33+
levels.append(
34+
{ftt_: ftt_.get_sub_ftts_for_one_crate_removed()
35+
for ftt_ in current_ftts
36+
if (k_, ftt_) not in cache}
37+
)
38+
current_ftts = set(itertools.chain(*levels[-1].values()))
39+
40+
# The last level is calculated. Time to make our way up.
41+
for k_, level in enumerate(reversed(levels), (k - len(levels) + 1)):
42+
if k_ == 1:
43+
for ftt_, sub_ftt_tally in level.items():
44+
cache[(k_, ftt_)] = ftt_.n_elements
45+
else:
46+
for ftt_, sub_ftt_tally in level.items():
47+
cache[(k_, ftt_)] = sum(
48+
(cache[(k_ - 1, sub_ftt)] * factor for
49+
sub_ftt, factor in sub_ftt_tally.items())
50+
)
51+
52+
return cache[(k, ftt)]
53+
54+
55+
56+
57+
###############################################################################
58+
59+
_length_of_recurrent_comb_space_cache = {}
60+
61+
def calculate_length_of_recurrent_comb_space(k, ftt):
62+
'''
63+
blocktodo gotta properly name these two sons of bitches
64+
'''
65+
from python_toolbox import nifty_collections
66+
from python_toolbox import cute_iter_tools
67+
cache = _length_of_recurrent_comb_space_cache
68+
if not isinstance(ftt, nifty_collections.FrozenTallyTally):
69+
ftt = nifty_collections.FrozenTallyTally(ftt)
70+
if k == 0:
71+
return 1
72+
elif k == 1:
73+
assert ftt
74+
# (Works because `FrozenTallyTally` has a functioning `__bool__`,
75+
# unlike Python's `Counter`.)
76+
return ftt.n_elements
77+
try:
78+
return cache[(k, ftt)]
79+
except KeyError:
80+
pass
81+
82+
levels = []
83+
current_ftts = {ftt}
84+
while len(levels) < k and current_ftts:
85+
k_ = k - len(levels)
86+
levels.append(
87+
{ftt_: ftt_.get_sub_ftts_for_one_crate_and_previous_piles_removed()
88+
for ftt_ in current_ftts
89+
if (k_, ftt_) not in cache}
90+
)
91+
current_ftts = set(itertools.chain(*levels[-1].values()))
92+
93+
# The last level is calculated. Time to make our way up.
94+
for k_, level in enumerate(reversed(levels), (k - len(levels) + 1)):
95+
if k_ == 1:
96+
for ftt_, sub_ftts in level.items():
97+
cache[(k_, ftt_)] = len(sub_ftts)
98+
else:
99+
for ftt_, sub_ftts in level.items():
100+
cache[(k_, ftt_)] = sum(
101+
(cache[(k_ - 1, sub_ftt)] for sub_ftt in sub_ftts)
102+
)
103+
104+
return cache[(k, ftt)]
105+
106+
107+

source_py3/python_toolbox/math_tools/sequences.py

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -56,104 +56,3 @@ def stirling(n, k, skip_calculation=False):
5656
def abs_stirling(n, k):
5757
return abs(stirling(n, k))
5858

59-
###############################################################################
60-
61-
_length_of_recurrent_perm_space_cache = {}
62-
63-
def calculate_length_of_recurrent_perm_space(k, ftt):
64-
from python_toolbox import nifty_collections
65-
from python_toolbox import cute_iter_tools
66-
cache = _length_of_recurrent_perm_space_cache
67-
if not isinstance(ftt, nifty_collections.FrozenTallyTally):
68-
ftt = nifty_collections.FrozenTallyTally(ftt)
69-
if k == 0:
70-
return 1
71-
elif k == 1:
72-
assert ftt
73-
# (Works because `FrozenCrateCounter` has a functioning `__bool__`,
74-
# unlike Python's `Counter`.)
75-
return ftt.n_elements
76-
try:
77-
return cache[(k, ftt)]
78-
except KeyError:
79-
pass
80-
81-
levels = []
82-
current_ftts = {ftt}
83-
while len(levels) < k and current_ftts:
84-
k_ = k - len(levels)
85-
levels.append(
86-
{ftt_: ftt_.get_sub_ftts_for_one_crate_removed()
87-
for ftt_ in current_ftts
88-
if (k_, ftt_) not in cache}
89-
)
90-
current_ftts = set(itertools.chain(*levels[-1].values()))
91-
92-
# The last level is calculated. Time to make our way up.
93-
for k_, level in enumerate(reversed(levels), (k - len(levels) + 1)):
94-
if k_ == 1:
95-
for ftt_, sub_ftt_tally in level.items():
96-
cache[(k_, ftt_)] = ftt_.n_elements
97-
else:
98-
for ftt_, sub_ftt_tally in level.items():
99-
cache[(k_, ftt_)] = sum(
100-
(cache[(k_ - 1, sub_ftt)] * factor for
101-
sub_ftt, factor in sub_ftt_tally.items())
102-
)
103-
104-
return cache[(k, ftt)]
105-
106-
107-
108-
109-
###############################################################################
110-
111-
_length_of_recurrent_comb_space_cache = {}
112-
113-
def calculate_length_of_recurrent_comb_space(k, ftt):
114-
'''
115-
blocktodo gotta properly name these two sons of bitches
116-
'''
117-
from python_toolbox import nifty_collections
118-
from python_toolbox import cute_iter_tools
119-
cache = _length_of_recurrent_comb_space_cache
120-
if not isinstance(ftt, nifty_collections.FrozenTallyTally):
121-
ftt = nifty_collections.FrozenTallyTally(ftt)
122-
if k == 0:
123-
return 1
124-
elif k == 1:
125-
assert ftt
126-
# (Works because `FrozenCrateCounter` has a functioning `__bool__`,
127-
# unlike Python's `Counter`.)
128-
return ftt.n_elements
129-
try:
130-
return cache[(k, ftt)]
131-
except KeyError:
132-
pass
133-
134-
levels = []
135-
current_ftts = {ftt}
136-
while len(levels) < k and current_ftts:
137-
k_ = k - len(levels)
138-
levels.append(
139-
{ftt_: ftt_.get_sub_ftts_for_one_crate_and_previous_piles_removed()
140-
for ftt_ in current_ftts
141-
if (k_, ftt_) not in cache}
142-
)
143-
current_ftts = set(itertools.chain(*levels[-1].values()))
144-
145-
# The last level is calculated. Time to make our way up.
146-
for k_, level in enumerate(reversed(levels), (k - len(levels) + 1)):
147-
if k_ == 1:
148-
for ftt_, sub_ftts in level.items():
149-
cache[(k_, ftt_)] = len(sub_ftts)
150-
else:
151-
for ftt_, sub_ftts in level.items():
152-
cache[(k_, ftt_)] = sum(
153-
(cache[(k_ - 1, sub_ftt)] for sub_ftt in sub_ftts)
154-
)
155-
156-
return cache[(k, ftt)]
157-
158-
159-

0 commit comments

Comments
 (0)