Excuse me again, I just relized that my algorithm was flawed. I just inserted
in my function the brilliant algorithm of Mark Dickinson and now it works:
import itertools as itools
def segment(it, n=1):
if n < 1:
raise ValueError(f"Number of segment must be > 0, {n} found"
See my implementation, is generic and not only for strings. It could be added
to more-itertools, I suppose:
https://mail.python.org/archives/list/python-ideas@python.org/message/E452LQGA3XKU5ADPTG54XP36ENXDZN2B/
___
Python-ideas mailing list -- python-i
Why not create a new custom class which has this function there? You can
use that object whenever you need the segments.
This is a very rare use case and doesn't make sense to implement that in
str.
On Wed, 25 Dec, 2019, 09:25 python-ideas--- via Python-ideas, <
python-ideas@python.org> wrote:
>
Excuse me, ignore my previous post, this is the correct implementation. It
works for every iterable:
import itertools as itools
def segment(it, n=1):
if n < 1:
raise ValueError(f"Number of segment must be > 0")
try:
len_it = len(it)
import itertools as itools
def segment(it, n=1):
try:
len_it = len(it)
it_true = it
except TypeError:
it_true = tuple(it)
len_it = len(it_true)
size, rest = divmod(len_it, n)
sizes = [size] * n
for i in range(rest):
sizes[-i] +=
Hi
I've looked at the example that smfiles (aka 1668151593 at tencent)
provided. It looks to me like smfiles is asking for help with a homework
problem. If smfiles tells me I'm wrong, I'll apologise.
By the way, because the number of parts is fixed, and because the order
matters, the OP's problem
On Sat, Dec 14, 2019, at 04:06, smfiles wrote:
> I think it's necessary to add a segment() method to str type or string
> module. This method is used to split a string into m parts and return
> all cases. With segment(), you can avoid tedious calculation and
> indexing if you want to segment a
I had not known about math.comb() and math.perm() being added in 3.8. Those
kinda feel to me like "not every one line function needs to be in the
standard library." But I guess wiser people than me saw a reason they are
needed.
On Sat, Dec 14, 2019, 3:00 PM Tim Peters wrote:
> Not really convolu
[David Mertz ]
> Here's a discussion of both a conceptually simple and a convoluted but
> fast version (the latter, naturally, by Tim Peters).
Not really convoluted - it's the natural thing you'd do "by hand" to
move from one partition to the next: look at the current partition,
throw out the 1s,
Apologies for the bad formatting. Here are the relevant bits, better formatted
(I hope):
>>> segment = lambda s, m: (tuple(s[i:j] for i, j in zip((0,)+c,
c+(len(s),)))
... for c in itertools.combinations(range(1, len(s)), m-1))
>>>
>>> list(segment("12345", m=3))
[('1',
On Sat, Dec 14, 2019 at 3:56 PM David Mertz wrote:
> [...] compositions are simply the permutations on the full length of the
> list of each partition.
>
Using permutations of partitions would be overkill. For compositions of a
given fixed length, it's much easier to compute them directly using
Here's a discussion of both a conceptually simple and a convoluted but fast
version (the latter, naturally, by Tim Peters). This is for integer
partitions, but compositions are simply the permutations on the full length
of the list of each partition.
http://code.activestate.com/recipes/218332-gene
This feels much too special purpose for a string method, and probably for
anything in the standard library. I'm not sure when someone would want this.
But it's an only very sightly special case of integer composition (
https://en.wikipedia.org/wiki/Composition_(combinatorics)). And related to
that
On 12/14/19 4:06 AM, smfiles wrote:
> I think it's necessary to add a segment() method to str type or string
> module. This method is used to split a string into m parts and return all
> cases. With segment(), you can avoid tedious calculation and indexing if you
> want to segment a string.
>
>
What are the practical uses for this? I don't recall having done this in my
soon 20 years career.
> On 14 Dec 2019, at 15:42, smfiles <1668151...@qq.com> wrote:
>
> I think it's necessary to add a segment() method to str type or string
> module. This method is used to split a string into m p
15 matches
Mail list logo