Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Run-length encoding
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Python implementation ==== {{hidden|headerstyle=background:#ccccff; text-align:left; |Imports and helper functions |<syntaxhighlight lang="python"> from itertools import repeat, compress, groupby def ilen(iterable): """ Return the number of items in iterable. >>> ilen(x for x in range(1000000) if x % 3 == 0) 333334 """ # using zip() to wrap the input with 1-tuples which compress() reads as true values. return sum(compress(repeat(1), zip(iterable))) </syntaxhighlight> }} <syntaxhighlight lang="python"> def rle_encode(iterable, *, length_first=True): """ >>> "".join(rle_encode("AAAABBBCCDAA")) '4A3B2C1D2A' >>> "".join(rle_encode("AAAABBBCCDAA", length_first=False)) 'A4B3C2D1A2' """ return ( f"{ilen(g)}{k}" if length_first else f"{k}{ilen(g)}" # ilen(g): length of iterable g for k, g in groupby(iterable) ) </syntaxhighlight><ref name="more-itertools">{{cite web|url=https://more-itertools.readthedocs.io/en/stable/_modules/more_itertools/more.html#run_length|date=August 2024|title=more-itertools 10.4.0 documentation}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)