Skip to content

Commit 6d252ec

Browse files
committed
-
1 parent e81276b commit 6d252ec

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

source_py3/python_toolbox/math_tools/sequences.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This program is distributed under the MIT license.
33

44
import numbers
5+
import collections
56

67

78
infinity = float('inf')
@@ -11,7 +12,7 @@
1112
_n_highest_cache_completed = -1
1213
def stirling(n, k, skip_calculation=False):
1314
'''
14-
blocktododoc specify first or second kind
15+
blocktododoc specify first kind
1516
'''
1617
global _n_highest_cache_completed
1718
if k not in range(n + 1):
@@ -55,3 +56,22 @@ def stirling(n, k, skip_calculation=False):
5556
def abs_stirling(n, k):
5657
return abs(stirling(n, k))
5758

59+
###############################################################################
60+
61+
_shitfuck_cache = {}
62+
63+
def shitfuck(k, recurrences):
64+
from python_toolbox import nifty_collections
65+
assert isinstance(recurrences, nifty_collections.FrozenCounter)
66+
levels = [collections.Counter({recurrences: 1})]
67+
while len(levels) < k and any(((k - len(levels) + 1), recurrences)
68+
not in _shitfuck_cache for x in levels[-1]):
69+
new_level = collections.Counter()
70+
for recurrences_, factor in levels[-1].items():
71+
for smaller_recurrences in recurrences_.counters_with_one_removed:
72+
new_level[(k - len(levels), smaller_recurrences)] += factor
73+
levels.append(new_level)
74+
# The last level is calculated. Time to make our way up.
75+
if len(levels) == k:
76+
77+

source_py3/python_toolbox/nifty_collections/frozen_counter.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import itertools
77
import collections
88

9+
from python_toolbox import caching
10+
11+
from .lazy_tuple import LazyTuple
912
from .frozen_dict import FrozenDict
1013

1114
try: # Load C helper function if available
@@ -201,3 +204,14 @@ def __and__(self, other):
201204
if new_count > 0:
202205
result[element] = new_count
203206
return FrozenCounter(result)
207+
208+
@caching.CachedProperty
209+
@LazyTuple.factory()
210+
def counters_with_one_removed(self):
211+
for key_to_reduce in self.keys():
212+
return type(self)(
213+
{key: (value - (key == key_to_reduce)) for key, value
214+
in self.items()}
215+
)
216+
217+

0 commit comments

Comments
 (0)