Run-length encoding · אנכוד רצפים
Run-length encoding
Compress a string by replacing each run of the same character with that character followed by how many times it repeats: aaabbc → a3b2c1. Always write the count, even when it is 1.
Walk the string tracking the current character and how long its run is. When the next character differs, write out the run (character + count) and start a fresh run. Don't forget to write the last run after the loop ends.
ריצות-לונג'ית אנקודינג
דחסו מחרוזת על ידי החלפת כל רצף של אותות זהים באות עצמה ולאחריה כמה פעמים היא חוזרת: aaabbc → a3b2c1. תמיד כתבו את הספירה, גם כשהיא 1.
עברו על המחרוזת תוך מעקב אחרי האות הנוכחית ואורך הרצף שלה. כאשר האות הבא שונה, כתבו את הרצף (אות + ספירה) והתחילו רצף חדש. אל תשכחו לכתוב את הרצף האחרון לאחר שהלולאה נגמרת.
Write run_length_encode(s) that compresses runs of the same character into the character followed by its run length, e.g. aaabbc becomes a3b2c1. Always include the count, even when it is 1. · כתוב פונקציה run_length_encode(s) שדוחסת רצפים של אותיות זהות לאותית שלאחריו אורך הרצף, לדוגמה aaabbc הופך ל-a3b2c1. תמיד כלול את המניין, גם כאשר הוא 1.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.