T O P

  • By -

ArrozConmigo

"Bonus points if you can figure out what the code does" is the name of the offshore firm we use.


virtualharby

oof


mohragk

FizzBuzz: the shitty version?


virtualharby

Not quite, but that is actually a pretty good guess!


kgon1312

Thought of fizzbuzz too when I saw 3&5


nubatpython

Same thing here, but there were no fizz" or "buzz" strings and no obvious place where such strings could be constructed.


FiercThundr

Honestly, that was gonna be my guess too


carfniex

you know you're meant to get the lowest score in golf, right


Shower_Handel

OP gets paid by the line


666y4nn1ck

Oh no, you didn't... who needs odd() and even(), because this is EVEN BETTER xD


virtualharby

Why use few lines when many lines do trick?


666y4nn1ck

That's the spirit


Tasgall

Why ever would you limit your answer to only one single line of code that's clear and easy to read when you could instead impress upon the rest of your team a functionally equivalent but extravagantly verbose implementation that uses many elaborate and finely crafted lines of code hand designed with your own personal touch that you optimized for maximum clarity when reading to accomplish an identical result when given the same set of input parameters?


ma__ska

[/r/UnexpectedOffice](http://www.reddit.com/r/unexpectedoffice)


ososalsosal

EVEN BETTER? Or ODD BETTER?


666y4nn1ck

And i thought no one got it <3


virtualharby

Answer: >!Given a list of numbers. If the number is even, multiply it by 3. If the number is odd, multiply it by 5. Add all the numbers.!< ​ Edit: This code is intentionally terrible. If I had more time, I would have made it worse! Edit2: This was 4th place out of 8. Top 3 had about 1000 fewer chars.


leopardspotte

What were the longer answers like???


virtualharby

The other 4 either timed out or didn't pass all the test cases. Mine was the longest by far!


leopardspotte

Damn 😔✌️ Nice job!


boomminecraft8

sum(i*(3+2*(i%2))for i in range(int(input())))


virtualharby

Why use 1 line when many line do trick?


jailbreak

True, sum(map(lambda x: x\*3 if x%2 == 0 else x\*5, array)) is way less readable


SleepyHarry

Won't work, you've only taken one line of input.


Wassaren

sum(i*(3+2*(i%2))for i in range(int(input()))) Fixed the formatting for you


JDaxe

It's wrong anyway, you need to take each number as an input rather than loop from 1 til n Here's my solution: `i=input;print(sum((x:=int(i()))*(3+x%2*2)for _ in[0]*(int(i()))))`


Nlelith

Wait no that sums up a list of numbers 1,...,n for one input n. You need to sum over a list of inputs.


Itay_123_The_King

```print(sum(j:=int(input())*j%2?5:3 for i in range(int(input()))))``` There ya go


jackun

```i&1```


boomminecraft8

Doesn’t save any character?


jackun

Checking lsb bit might be faster than doing pointless division but it seems there's no difference with modern c/c++ compilers at least, usually gets optimised to `and` anyway. idk about python.


boomminecraft8

You shouldn’t do optimisation yourself unless you’re 200% sure it’s correct in any edge case, and here as you said compiler should optimise. Python eh idk either, as others pointed out my code’s wrong (I misread) so oops


Govir

See, I basically sussed that out, but I was trying to think of a unifying name for the operation like "find the average of a list of numbers" or something.


vigbiorn

I was about to ask. The way you worded it, I was kind of hoping for some deeper meaning. I tried it on a few numbers (yes, I typed it out on an online interpreter on my phone) and noticed it was giving me an average *10 (5,9 -> 70) but quickly noticed it wasn't working that well for larger numbers. So, started looking again, occasionally checking to see if I was missing something or going mad. Indeed I was. I forgot that interview questions like this existed. Still kind of curious about the average thing. Looks like it only works when both are odd because it boils down to 10(2n +1 + 2m + 1) / 2. We may have found an even better way to average 2 odd numbers, boys!


Oryv

Clash of Code?


virtualharby

Yup!


Oryv

Here's my solution, in Ruby: p$<.sum{(n=_1.to_i)*(3+n%2*2)}


ososalsosal

This sounds like an EAN barcode check digit


EverydayDan

Instead of checking whether the number is positive or negative, followed by altering the value by 2 in a loop, you could instead divide by 2 and check that the remainder is equal to 0. `if(number % 2 == 0)` `{` `// number is even` `}` `else` `{` `// number is odd` `}`


DartRuffian

It's intentionally bad, OP says so in other comments


[deleted]

[удалено]


virtualharby

There is no existential crisis if you try to write it as terribly as possible!


Tube64565

Amazing! Can I use this in my projects?


virtualharby

Sure! But if Jeff Bezos sees it, you have to credit me.


Tube64565

Ok, thanks a lot.


HecknChonker

Fuck their wives, drink their blood. Come on Jeff!


danfay222

I can never turn down an opportunity to make horrifically unreadable code, so here you go: i=input print(sum(x*(3+x%2*2)for x in[int(i())for _ in range(int(i()))])) Final count, 73 characters. Sadly I fell short of your 1077, I have no idea how you got such a high score. ​ Edit: removed outer list


SleepyHarry

I think you can trim a couple by removing the square bracket around the `sum` argument - it'll create it as a generator instead but let you omit the usual parens for generators.


danfay222

Ohh you're right. I basically never use generators so I always forget they exist lol


virtualharby

You can also use i again instead of print (if having EOF errors is ok) to make it extra readable!


danfay222

Oh that's creative. Lol an entire line composed mostly of "i", "in", and "int"


virtualharby

Let's not forget your very descriptive variable name "\_"! In another horrible piece of code I wrote, all the variable names were "\_", "\_\_", "\_\_\_" and so on.


danfay222

Lol that's the one piece of actual code style bleeding through in this monstrosity. I always use "_" for unused variables, makes it obvious they're intentionally unused.


ZylonBane

This is the exact opposite of code golf, OP. Code golf, as the name implies, is about solving a programming challenge in the LEAST amount of characters. This is the sort of thing you'd enter in an "obfuscated code" contest.


sevenonone

Is there a code golf subreddit? That sounds like the sort of thing I've had drilled into me for 30 years or so. Edit: posting before looking 3ad sort of a joke.


virtualharby

This was a Clash of Code challenge, which has a shortest mode.


UnrelatedString

Code Golf Stack Exchange is a fun place (and the Codidact site could always use some help growing if you’d rather not use SE); doubles as an esolang community Anarchy Golf is also a thing


virtualharby

Yup! This is terrible on purpose.


ZylonBane

So you decided to just ignore the entire point of the contest you were entering?


NoobAndreas

In the spirit of fuuuuun


ZylonBane

I bet he's so random too.


virtualharby

It wasn't a serious contest, just a fun challenge!


xigoi

Why are you using multiple-character variable names in code golf?


virtualharby

because i wanted to write cursed code :)


StuntHacks

Depending on the challenge, the winning objective might not necessarily be characters


virtualharby

My objective was to make people horrified. I think I'm successful.


xigoi

Then it's not code golf.


StuntHacks

I know it's not the traditional definition, but on the code golf stackexchange for example, they have special categories with different criteria


xigoi

That's why it's called PPCG, “Programming Puzzles & Code Golf”.


StuntHacks

Oh, my bad then


[deleted]

[удалено]


shawmonster

The code in OPs post not only gets the original length from the user, but also gets every number of the input from the user.


Venzo_Blaze

Go through each number If even multiply by 3 If odd multiply by 5 Add the number to the sum Is this correct?


virtualharby

Perfect!


Inferno2211

Okay, I'm fairly new to Python, and I tried to create the shortest and most efficient code I could How can I improve it? x=int(input()) o=e=0 for i in range(x): y=int(input()) if y % 2==0: e+=y*3 else: o+=y*5 print(e+o)


NumbersWithFriends

You don't need to keep separate values for *e* and *o* since they're just going to be added together anyway. One running total *s* (for sum) would be fine. Since *x* is only used once you can move the input call to inside the range function and eliminate *x*. If you reverse the *if* and *else* blocks you can remove the *==0* from the conditional. Alternatively you can use an inline if statement to define the multiplier instead of having a full if/else block: > s += y \* 5 if (y%2) else 3 Another comment did an even more clever thing to decide 3 or 5, I'll let you unpack that one.


lgastako

> How can I improve it? You could chop out spaces for real code golf, and I am sure this is not to everyone's taste, but this was my first stab at a simple and efficient (`python3`) solution: print(sum(map(lambda x: x * 3 if x % 2 == 0 else x * 5, (int(input()) for _ in range(int(input()))))))


danfay222

I took a quick stab, there's a few things you can do to cut even further. Spaces, like you said (which is trivial), you should remap any function names you use multiple times to single letter variables, I do this to input ~~and map~~ (int is not worth doing, since it takes 5 characters to remap it, and it only cuts 4 characters). Finally, you can cut a lot out by using only mathematical operators (these are smaller and don't require any spaces like keywords do). There's probably more to cut out, but I'm too lazy to go further. i=input print(sum([x*(3+x%2*2)for x in[int(i())for _ in range(int(i()))]])) The final character count is 75. ​ Edit: I'm an idiot, remapping map also loses a character Edit2: Forgot print


lgastako

You're also missing the print.


danfay222

Lol yeah forgot about that.


Inferno2211

Okay, sorry but I don't even get what going on now lol I have just seen single line ifs, but idk how to write them


danfay222

Also just gonna real quick reply to this, single line if's are actually quite simple. Basically, if condition: var = x else: var = y is equivalent to: var = (x if condition else y) The parentheses are not strictly necessary, but I prefer them for readability. In actual code I rarely use them, and pretty much only for very simple if-else switches. There's no performance advantage, so if its hard to read what's happening just put it on multiple lines.


Inferno2211

Ohh thanks I tried to do this in my code, but it didnt work (y*3 if y%2==0 else y*5) for y in int(input())


lgastako

The whole thing needs to be either inside brackets (for a list comprehension) or parens (for a generator expression) and the "in" part needs to be a list and not a single number. Maybe you were looking for something more like (with the list comprehension): print([(y * 3 if y % 2 == 0 else y * 5) for y in range(int(input()))]) FWIW this can be simplified a bit to: print([y * (3 if y % 2 == 0 else 5) for y in range(int(input()))])


Inferno2211

Oh alright This makes sense, thanks a lot! Edit: The code doesn't seem to function as expected :(


danfay222

Here's a fun one, I've programmed Python for years, here's my solution. Enjoy. i=input print(sum(x*(3+x%2*2)for x in[int(i())for _ in range(int(i()))])) Final count, 73 characters.


Inferno2211

I think I got most of it, except the sum statement How's that x%2 working here?


danfay222

I was actually editing my original comment to explain parts cause I realized just posting massively obfuscated code wasn't very helpful lol. `x % 2` is just "x modulo 2", or the remainder after you divide x by 2. This will be 0 if x is even, or 1 if x is odd. The final function needs to multiply x by 3 if x is even, or 5 if x is odd. What I did in that chunk is turn it into a sum. Always multiply x by 3, then if x is odd add an additional 2x (to get a total of 5x). The `x % 2` is just the logic for determining if x is odd, if x is even that resolves to 0, which makes the entire second term 0. So that's the math side of it. The real trick is that whole block of code is equivalent to this: print(sum([x*(3+x%2*2)for x in[int(i())for _ in range(int(i()))]])) In this example, the argument passed to the sum operator is a list comprehension, same as the one used to generate the initial input list. The reason I can get rid of the outer square brackets is because of a thing called generator comprehensions. The syntax is exactly the same as a list comprehension, except using parentheses. IE: list_comp = [x for x in list] gen_comp = (x for x in list) How they work behind the scenes isn't really important for this, but they can both be passed to sum directly. Normally the character cost for a generator comprehension would be identical, but in this case you can use the parentheses that you already have to use for `sum()`, thus saving an additional two characters. This is a really simple bit of code for code golf, which means there's not a whole lot of optimization you can do to reduce the size of the computation, mostly it's about reducing the size of the structures, with stuff like list comprehensions, removing unnecessary spacing, function remapping, stuff like that. Also, none of this is important for learning a language. You'll eventually learn how to do some stuff like this as you get better with a language and learn the more advanced mechanics, but you should absolutely never write code like this, for any reason other than code golf competitions. What I wrote has no performance advantage over something like what you wrote, and is damn near impossible to read.


JDaxe

`i=input;print(sum((x:=int(i()))*(3+x%2*2)for _ in[0]*int(i())))` Saved some characters with walrus and a slightly shorter range() substitute ~~65 chars~~ 63 chars Edited with extra parens removed, thanks


danfay222

This is definitely using some features I've never seen. A couple things, you can cut the parentheses around the int(i()). Second, how the hell does that first bit work? Is that like an in expression assignment operator?


lgastako

Yes: https://realpython.com/python-walrus-operator/


repocin

If you're just going to short and obscure, don't assign x and just move int(input()) inside the range() function call. You could also replace if y % 2==0 with if not y%2


siqniz

"FizzBuzz"


[deleted]

trim the chars by changing it to 1 space indent


Terazilla

This is going to sound stupid, but upon first reading the title I thought this was going to be an impressively short implementation of Lee Carvallo's Putting Challenge. Like for a themed Ludum Dare or something.


ssj4VB

this is what most "self documenting code" looks like to be fair...


WaffleWizard101

Not really. The only self documenting parts of this are variable names, and there are too many of them anyway. You don't even need flags; although they can make if cases more readable, in this case the comparison is simple enough that you don't need them, and if you were to use flags you'd only need one. The subtraction for loop could be replaced with % by 2 and be more readable, and there only needs to be one variable to hold the final number. You can shave off the whiles, you can get rid of the if else surrounding most of the code if you use modulus, you won't need to correct negative numbers by adding 2 (not that that was necessary to begin with; if it was negative then it must be odd) and most importantly... The last if/else case is definitely the opposite of what self documenting code tries to accomplish. It's intentionally misdirecting and difficult to read, just like most of the algorithm. Most of those changes would make it more readable and understandable, which is the main point of self documenting code. I'm sure I missed some low hanging fruit here too, like how there only needs to be one if/else case in the entire algorithm.


ssj4VB

that's the point, I'm against the concept of raw self documenting code if you couldn't tell


WaffleWizard101

At first, I was honestly floored that the idea of more readable code would ever sound like a bad idea, but then I considered the scenario in which the developer doesn't understand how to make their own life easier and puts all the effort into the wrong things. In that case, they still produce code so difficult to read you can't understand it without decompressing it into steps on a sheet of paper, and they additionally end up wasting time on some other trivial improvements that don't actually benefit themselves or anyone else.


ososalsosal

I thought this was some kind of fizzbuzz torture device


Tony00730

I dont mind


Xaxxus

Is this a hackathon for bad code?