 Well, thank you So my name is Alessandro Mishin the day job. I work at the open solution who is sponsoring my talk here and we do Software development in Python and we work mostly with your special data so Geographic geography and satellite images and this kind of stuff as you hope be I do coding competitions I'm not very strong. I mean there are Really people who are really great and I'm just a hobbyist, but I like very much the the trail and the things you learn and Crazy stuff that you end up doing This talk will be about the code shortening code shortening is a kind of Coding competition where you don't strive to solve a problem in the smallest amount of time or with other Technical stuff, but you want to do it in the smallest possible source code and it's just for fun and In we will go through The solution of one of the problem one of the most popular problems where I have the shortest solutions of of them all and We will see that in this particular case it's really really Interesting what you need to do to get the shortest code so first of all Code shortening is usually say called code golf because you strive to do the less I mean you want to have the shortest score and it's similar to golf so I I mostly use code golf as a as a way to call the sport and and We will be talking about the sites con problem on Sposh, Sposh is the sphere online judge and Actually, if there is something I assume you read the abstract I hopefully you have had a look at the coding competition yourself and I I promise you to go into some frightening depth of the title language and to show you some mind-blowing tricks and To end up showing you the most obfuscated Contribute and see Python code you have ever seen and hopefully will ever see Now if there is something that you I really would like you to learn from this talk is to not overemphasize abstracts because then you ever end up in a room full of people who can see you and say, okay I'm here blow my mind Anyway, let's see what we what we can't can do So first of all for people who don't do Coding competition and don't know what Sposh is Sposh is a coding platform with an online judge Basically, you can upload problems and people can try to solve it It supports a huge amount of languages and he said it has a really amazing trove of problems. There are Around 20,000 problems A lot of them are quite easy But some of them are really really hard and some of them can be done just in Python That is the only known solutions are in Python Most of them are for C++, etc. Because they are far you need fast memory management, etc. One of the characteristic of Sposh with respect to other coding competition that you know that you may know is that While users course are public and you have ranks so you know who are the regulars who are the Good ones and who is good in C++ or Python, etc. Solutions are not public are not made public At the end of the when you do a competition, so you can actually compete anytime and some of the Problems are really old and have been solved by a lot of people if you If you look into the web someone published their solution and you can find solutions for the easy problems, but the hard problems are solved by people who really value their Position in in Sposh, so they don't publish their code and you are not You have to do it yourself This means by the way, I'm publishing the code for one of the best solutions in Python for this problem So please do not use that solution to get half point in Actually, you have to promise it that you are not going to cover the solution Otherwise you have to leave Now sites gone is an unusual problem. Usually the Sposh problems are standard algorithm problems, but Sites gone is one of the Few code code golf problems. It's really old. It has been uploaded in 2005 and it's actually one of the top 20 most popular problems of Sposh and 8,000 people try to do it and with 1,400 solutions in Python alone actually team pedders one of our I mean one of the fathers of Python, it's one of the best solvers even if in pearl He used pearl to do So let's go to the meat. This is the The statement of the problem It's called sites gone or sites contents and given a set of integer I mean the language it's really strange, but given a set of integers find the sum of all positive integers in it Basically, you're given an input that is Nashi file an ASCII file and you get the first number in the input is the number of Following integer that you have that you need to consider and that you have to sum only the positive integers The score that you will get is equal to the size of the source code of your program Except symbols with ASCII codes below equal or below 32 this is then then you have a sample input if you get four it's the number of Following numbers that you have to consider then Five man five six minus one and you have only to sum the positive integer. So five plus six It's a lot very easy. This is very easy. Actually, it's not very interesting as a code shortening problem by itself because it's easy Now what is the solution? for Python do to the reason I'm giving this talk is that I have the One of the two best solutions I wrote one of the two best solutions that are being uploaded to sposh and quite quite strangely the the code The problem has to be published in 2005 and someone already after six months found the 29 solutions uti urpala and Only after nine years Halba nor rainbow, which is very no. I mean, it's one of the best code shorters in in Python and Find the solutions just days before I find I found as well the solution. So it's after nine years two people in two weeks managed to get 28 In Python 3 it's more or less the same that is the solution can be Rewritten in the same solutions is rewritten in pattern 3 and it's just slightly longer because of the syntax is slightly different We are going to see the two point the pattern two solutions because it's the first one I developed and it's Just slightly shorter So let's see General Python golf. What is the master plan actually? The the steps that you do are more or less always the same first you aim to correctness You build reference solutions you need to check that you understood what the problem wants and Because every time you submit the solutions first it must be correct And then they count how many characters you use to and give you a score So first correctness then algorithm algorithm wizardry usually you start with the standard algorithm and you can get to the same solution in a really large in Large number of different with different algorithms. Sometimes you use specialized them something you generalize them the reason for this is that At the second at the next level the language wizardry it means that you try to find strange constructions in Python Syntaxes that usually are not used to get the code short and short and sometimes they work better with some strange algorithms and with the straight forward one So let's start with the reference solutions. This is around 50 characters This is the extremely non Short solution. You just read the first input you initialize your counter range count and T Of the following number you get any number check if it's Bigger than zero summit and return the results. This works If you count it with usual code golf score that is all the characters in your solutions 107 solutions are 107 score, but with sites gone since you can You don't count all spaces and new lines. It's 70 You can easily rewrite it. I mean everybody is able to write in in a more compact format just using summer and probably using max Here you already need to know that somehow the input inside the range will be evaluated before All the input inside the max input actually already does the casting to a integer so everything is Is working nicely and you already get in the in the range of 45 characters Now second step is to try alternative algorithms Now if you don't read the fine prints like you forget the you have bizarre special character as key character Exception there's nothing to see. I mean you just some in numbers Let's see let's start looking at it as Standard code short and code shortening solutions first These are all tricks that you can find on the web. It's a quite involved Processing understanding what is what what works and what not and it's very nice And you spend a lot of sleepless nights doing it, but it's more or less standard first of all Inputs you can just give a short array to input and you will save one characters This is just one characters. You call it input I But it saved you one characters then the most used one of the most used tricks for code shortening is to Transform four Into string into string multiplication in this case you see the max zero comma I Becomes a piece of a string I make it times I so I read the first numbers. I do a long string of max Max zero dot I etc. And then I evaluate as I valid it goes and Fields all the input reads the input from there from the standard input and then this makes a big Tappel the valve will return a big couple that you pass to some Which just give the correct results as you are something max is making sure that you are something only the positive values and We are down to 40 now. There is In a trick that I like a lot the fact that you actually can remove the summer Since you are already doing in a valve and you have a comma on the other side You drop the comma on the other side and put the plus at the beginning now you are already doing the summer and There is this nice trick that the sum at the beginning of the string will not be a problem because plus it's both you or a Unary and the binary operator. So you we are already using a lot of small tricks and etc And we're getting to 35 now We're starting getting closer, but the closer you get the harder it is now there is one of the nicer tricks in In standard chord golf that is you don't print stuff You input it how actually input prints the arguments Before reading the input so you can throw the whole print you already have a shortened name for input and you print with input actually this solution ends with a Syntax error, but it's no problem because the the system doesn't care what happens after you have printed the solution. So and We are to 33 great Now To my knowledge, this is the shortest possible solution. We start using a standard code shortening. I can't be proven wrong But I tried a lot and I didn't find anything else and I see a lot of people with 33 in the score in the ranking So I guess it's I mean a quite advanced solution Is your mind blown yet? So, so wow Hopefully not because this was child's play you can find this stuff I mean it takes sometimes that you can find this stuff on the web now we left some fine print somewhere Alternative algorithms Actually since there is a strange exception we may use as many ascii characters below 32 as we want. What can we do with them? Inside code the only legal characters are space tab a new line and I didn't think to anything that you can really do with that because they are just spaces But you have another place where you can put real ascii characters string literals for string literals the legal characters inside inside string literals are a lot are About 30 actually the null null ascii characters is not legal inside pipe on strings must be escaped with 0 slash x 0 0 and The new line is not legal because I want to do single quotes String literals so it will be a new line so but I have 30 characters. This is a lot. I can do a big String literals as big as I want and I have to find creative way to use it Now it's not that difficult so to try to think what to do because the basic building blocks of your modify alternative algorithm is build a string liter with a lot of ascii special characters Turn that string literal into something useful maybe code and then do something with that new string leaves with new string and now it's a string actually after after you translated into code Now how can we do something with a string of codes? We exactly Basically what we are saying is that what we are looking at what what you're looking for what we are looking for is Kind of a decrypt function that takes an encrypted string something that has been encrypted into ascii special character Decrypt it and then pass it to an exec Actually, these are the steps that you do to prepare the solution because you cannot write it obviously in an editor And you decide what is your original solution something that need to work? We use one of the previous solutions then you have an ASCII 32 encrypt Function you need to find it that encrypts it into just ascii special characters then you Put it into you you have to write your ASCII 32 the encrypt function is as Few characters as you can and then you put the string there and you get the solution and you can submit the solution So what we are interested in is in buildings this ASCII 32 decrypt so Do you does anybody have any? Suggestion what we need is something that? takes as input an ASCII string literal as I just give string and then outputs another string with possibly python code and It must be as short as possible because this is I mean we are we want to go below 33 Does anybody any idea of what can be used to do this? What trick we need? Translate string translation translate. It's the first power horse because It you don't need to import it. It's a method so it's it's really long the spelling bus Don't care. I mean if you save the import. It's already in the okay range now What's translate does it's returns a copy of the string? Well all characters have been mapped through a given translation table which must be a string of length 256 But we can fill it with control characters, so we really don't need the 256 characters we just need To put inside the character that there we need in the translation in the decoding in the decryption story This is how our Decrypt function and how our solution will look like so we have an encrypted string It's a literal string full with ASCII characters Then we have a translate we will translate it and we need to have a the crypt table that Will make this go into code that then we exec now the machinery the translation machinery our exec plus the Decryption is 20 characters plus All known ASCII the number of known ASCII contra characters that we need to put in To the trend the script table. I mean coach is written with known ASCII with known ASCII With ASCII above 32 characters, so we will need to put some of those into the the script table so we have we spent 20 characters and let's see how How many characters do we need to make a nasty control character string become some nice code This is how you do it and I mean I leave the details actually there are small stuff like the fact that you don't want to use the first character in the Decrypt table you can't use the 13th character because it's the new line so There are small details that I Which I don't want to bother you and so if we get this the first the solution that we first used We can use Counts the number of different characters in this and it's 19 and it's the card the characters that are there Now obviously 20 plus 19. It's not really great. We already have 33 and this is 39 So we start doing tricks for example the underscore. It's just stupid. I can put it I I already I was already using I and I thought I can go to 18 and so and then I Can't this is a nice trick. I don't need the zero the zero. It's just in to you can just Instantiate in to as you already using input that has already in to and you go to 17 characters, so you see We just mapped the old problem into a new problem This is a quite unusual shortening problem is the we are looking for a solution to a problem Which has the minimum number of different characters? Let's call it sites come to a problem. It's the same as sites come But the we are looking for the minimum number of characters, so we will put aside the translate stuff It's working. It's the it's as a matter of fact. I found at least two other way to do the decryption who Could be who gave good results. This is just the easier I Spent a lot Wake nights on this so now How can we do better? Okay? We try to use the same tricks as before trying to Find language tricks to get to use less and less characters, but it's not as easy as it appears for example if you Change the printing to input. It's it doesn't get you anything because the letters are already used so A lot a lot a lot of time afterwards I arrive arrived to something that is quite convoluted, but it was 20 plus 14 that is 14 different characters and even 13 different characters and If you look at the details of this stuff, I mean It will take you some time to To understand what it does, but it works. I tried pasted all of them to be sure that I was not writing stupid things But we are 33 I mean all this work and we are back to 30 to a 33 solution How can we do better? We are starting to look into the abyss So how do you write codes with less? Different characters may be octal strings So in an octal if you can write a string literal with just slash and numbers from zero to seven And what do you do with the dock octal string? This is print hell word. I put the Semicolon so you can see that this stuff has all the Numbers and we have to go downward is the only way forward. We are inside an exec and what we want to do is another exec so this is kind of reminiscence of exception where you need to go deeper and deeper in the Dreaming to actually get the work done Now Does it actually work? It does so you can get a string Execute and inside the string you want an exec of a string literal What is nice is that if you use all the The octal characters you are back to 33 But obviously, I mean here you have some wagon room. I mean you don't need all of the Numbers if you are very careful we I collected something like Hundred or probably more different solutions so I can just look for all the solutions see if I can get a Solution that doesn't use the characters a character now If you can read this This is actually a working solution And it happens to not use the character three in his octal representation and If you spent enough now It's looking for this stuff you write programs to see what characters you can use in all the cases I know I some and this is all way. This is less than Than 32 if this is the first. Oh, no, this is not this out there to do Okay, this is 32 days, which is less than 33 So this is the first time we managed to use this as key character trick to get a better solution Dance than normal python golf Can we do better? Actually, I remember that once I wrote a solution with this trick that was 31 characters But I can't find it anymore and it's some spoils, but when you download the solution it Make garbage of the ASCII characters, so I don't know what the solution is I know that there is at least a 31 solution with just this trick and you The number that the octal number that you don't need is seven If you want, it's a nice exercise But we are 32 or 33 28. It's really far away. How can we get in just let's come back In order to get to 28 we need to remove four characters from here. It means that If we accept that you are doing the exec and you have the slash and you need the double quotes You have just three Letters left to use How do we do it? now following going on on the inception Metaphor metaphor the only way is downward We do another exec of An exec of a string literal. Why would you do that first? Because you can it doesn't act it doesn't add any Anything to the score because you already have all the letters to for the string lead for building the solution inside the string And the point is that now you can't build the string literal as From Python a string literal you can't build because after the parsing of a string of the source code, it's not a string literal anymore and the slash Octa representation doesn't work So you need to one more exec in order to build the string that Compose your exec a string literal which is the solution So what can we do? This is just to see how it works. We get a Statement like print This is the octa representation. This is the string that represents the octa representation of print and Within the first exec we can actually compose that string we get the pipe the four for example The four is actually like a rep of the integer four and the rep of integer four is just the sum One plus one plus one plus one Now obviously we are not going very far because rapper is really long But actually it's not that bad because he it's already there because of the exec and the parenthesis We but we are I mean it's a lot of cost because of the parentheses of the P and And are and also we also put the class there So we now need almost the final trick this is the second to last trick What how can we shorten rap? And you take her yes conversions actually rapper if you look at a man page is Returning a string containing a printable representation of an object and this is the same value value yielded by conversion with its reverse quotes I didn't know reverse quote existed before starting to play with Short coding, but this is great. You throw all the parenthesis and rapper Away and you just get back quotes. You have introduced plus back quotes and one Three characters and with these three characters You can't build everything everything except zero You can't build all the numbers one two three up to seven, but not the zero so We can do all a completely arbitrary code Literal string code and run it with the exact exact sec with just See plus double quotes zero one slash reverse comma CE and X but This gives you was 29. How do we get? lower We don't like the zero. How do I? Throw the zero out you don't do single characters But you get the 60 and you put one plus one plus one plus one. These are 60. I assure you and these works and We got 28 now very fast. This is exactly like inception as a matter of fact and Is that many exactly in exactly the exact two unstable? No, actually you can't It does work. It's a lot of work to build the the solution because you have a lot of tricky cases but we have more or less the same structure of In inception reality in the movie It's our Python interpreter and there we pass a string and that string the main work that we don't do is that We decrypt the ASCII 32 encrypted string Then we exec it within the first exact what we are doing. We build numbers with the conversion tricks and We be all the numbers that we need and with all the pieces we build a exact plus literal Solution that then we exact and we go one more level the one level deeper and we pass the string literal we exec it once more and finally we run a completely arbitrary code and We choose the code that solved this particular problem, but we can solve any problem with 28 characters above 32 and Whatever it takes for below And this is my friend I promise to show you the Most C Python code you have ever seen and actually you can see it in an editor this is a hex damper of the actual solution that I submitted and Obviously you don't understand anything, but I can show you what happens after translation You get this very nice string as well, which is the second second deep layer and then we get to this which is the October presentation that we want to exec and we go to this which is just the solution we start with and now It took me really a lot of time Both to find the solution and to prepare the talk because it's really if you want to explain and I hope This worked and your money in your mind is actually blown now And if it's not I have just one word and the last slide for you Six characters and they did probably the same stuff we did now Thank you. Is there any question? My mind is definitely blown Is the Python 3 solution longer because the back takes trick doesn't work Actually, I don't remember for sure. You don't have exact the exact of parentheses So you lose two at the very beginning for the parentheses And then I think yes, it's the back The back tick that doesn't work, but I really don't know how I mean I would have to look at The solution and that will not do much because I cannot read it, but okay my brain hurts But my question is did you develop any tools for building code in that different levels? Actually, yes I mean, it's not possible to write this stuff and I did not only tools to write the code But also tool to analyze all the possibilities because whenever you try to look for Solutions that means for example one octal numbers you need to Do tries and actually there was a previous solution that was different. It didn't use translate where I tried a huge space of functions that could do Numero translation between Ascii below 32 and the character that I needed for the solution. So it was quite Quite a journey. Awesome As a matter of fact, I Slightly cheated you in the sense that if you look here actually there is one more trick you see the x28 for example and there is one more trick in which if you have a zero in the octal representation sometimes you can turn to the Extraditional representation and since I already had the X the X I could use that So I tried to minimize the case where you needed to some very very long ones Does the pearl solution also? encode into characters underneath 32 Haskin 32 I would pay to see the the persolution I didn't I guess so because There was quite in 2005 2006 there was Quite a lot of chat in the forum about that and they were talking about How they could not read the solution but just have a tool that builds the part First step and then another tool that be the second step and so on and so on so I guess they were doing This kind of three tricks. Have you considered switching to parole as a language? Actually, Tim Peders did if you look Tim Peders is actually fourth. I mean and with seven so Hi, hi, are you trying to do this better solution? I must admit that I as I was writing the The presentation I had a couple of ideas. I I don't want to To think of them. Otherwise, I will need more sleepless nights The question What's the golf score of your entry? Sorry, what what's the actual golf score? What's the length of it including the horrible characters? Oh, I'm sorry. The golf source. What is what is the length of the string you've actually submitted including? I Think it I'm not sure you mean the the length of this I'm not sure I guess was several kilobytes Sound like 30 The limit actually there is a limit which is 50. So I mean I still had some room This is the last question Can you actually submit this with a browser? No, I uploaded the file. Yes This is the reason I cannot download it anymore because the browser the browser or maybe the code Mix garbage out of it when you try to download it again Okay, thank you very much. Thank you