On Arithmetic Coding

Back to General discussions forum

Rodion (admin)     2023-04-28 18:55:54
User avatar

Hi Friends. Long ago I wished to add some task on this algorithm, but had difficulty inventing something "ingestable". You'll see idea is simple, but implementation needs care and may be bit difficult to debug. However algorithm leds to further interesting ideas (like PPM - perhaps we'll add such variations later) so it is a pity not to have it.

The algorithm was not very popular for most part of XX century because arithmetics worked much slower in comparison with logic and data-moving operations, while benefit is not very large. But nowadays such obstacles are not important as 64-bit long values are multiplied in few clocks of CPU.

Now it is live, after about a week of struggling :) regretfully looking over the problem statement which tries to provide algorithm explanation, I see it is quite lengthy.

So let's regard it as a "work in progress" yet and if you would like to give any advices on making it more user-friendly (or perhaps you'll find some nasty bugs) - let's polish it.

gardengnome     2023-04-29 07:43:58
User avatar

Hi Rodion,

One tiny bug: when I run my Python code in the browser, the newline characters somehow don't appear in the output and thus the solution is rejected. Copy & paste from my local environment works however. Otherwise I have not found any issues.

Thanks!

Rodion (admin)     2023-04-29 08:11:38
User avatar

Mathias, Hi and thanks a lot for report! Just a bit confused - which newline characters do you mean? those in the input?

gardengnome     2023-04-29 08:19:33
User avatar

No, those in the output. If I run my code in the sandbox from the problem page, the output does not seem to contain the line breaks after each test case, and it gets rejected by the checker. However running the same code on my machine and manually copying & pasting the output over works.

Rodion (admin)     2023-04-29 21:11:31
User avatar

Hm-m-m, it may be I confused something in the problem description, but there shoud be no line breaks in the output (as in most "good old style" problems), they are separated with spaces. I looked into your solution:

aac = AdaptiveArithmeticCoding()
for _ in range(int(input())):
    print(aac.decode(input()))

Unless I'm mistaken print(...) adds linebreaks...

gardengnome     2023-04-29 21:24:00
User avatar

My mistake. Among all the text, I missed the key "separated with spaces" part. Anyway, many thanks for checking, that was an easy fix.

CSFPython     2023-04-30 15:17:11

Thanks for another very entertaining problem.

Please login and solve 5 problems to be able to post at forum