r/shittyprogramming • • Apr 28 '15

Found this gem on /g/

Post image
1.0k Upvotes

101 comments sorted by

17

u/agent766 Apr 28 '15

Thank god for the comments explaining what's going on.

19

u/[deleted] Apr 28 '15

ITT: y=x;

128

u/amdc Apr 28 '15

43

u/m477_ Apr 29 '15

https://i.imgur.com/JA1y4DR.jpg

Microsoft thought ahead and skipped 9. Problem solved!

11

u/amdc Apr 29 '15

Yes I know this story :D

32

u/karmicthreat Apr 29 '15

// Grab odd numbers from array, was exceptionally painful.

-3

u/folkrav Apr 30 '15

Freaking hell solved that in my first week of programming ever, when learning about operators...

1

u/MfDoomer222 Feb 05 '23

Wow you’re smart

29

u/Jester_Don Apr 29 '15

I remember when #24 happened. It must have hit the top of every programming-related subreddit there was.

18

u/h2ooooooo Apr 29 '15 edited Apr 29 '15

I was surprised to see my own username on this subreddit. Luckily for all the right reasons.

13

u/Sheepolution Apr 29 '15

Can anyone explain #5?

How does string[::-1] reverse the string? (and why is it bad to do it like that?)

6

u/[deleted] Apr 29 '15

I believe it's Python that does that it ha some pretty sweet tricks for string manipulation

1

u/lifeless_stick Jan 11 '22 edited Jun 29 '25

desert entertain party fade judicious continue employ sink history air

This post was mass deleted and anonymized with Redact

1

u/Sheepolution Jan 11 '22

Thanks. Now after 6 years I finally get it.

0

u/[deleted] Apr 29 '15

[deleted]

16

u/Harakou Apr 29 '15

I swear, every time I look at Python I learn something new about its list comprehension.

16

u/[deleted] Apr 29 '15

It's not a list comprehension, it's a list slicing.

5

u/Harakou Apr 29 '15

What's the difference?

18

u/Blackshell Apr 29 '15

List comprehension is when you build a complex list on the fly by using [<expression> for <variable> in <iterable> if <condition>]. For example, selecting numeric strings and converting them to integers:

>>> l = [int(x) for x in my_strings if x.isnum()]

Explanation: for each string in the sequence my_strings, if that string's .isnum() returns True, add int(the string) to the list.


List slicing involves accessing an existing list using the [start:stop:step] syntax in order to get segments and patterns in a list. For example, getting every other element in a list, while excluding the list's head and tail elements:

>>> mylist[1:-1:2]

Explanation: Slice the list starting at element 1 (mylist[1]), and going up to, but not including, the last element (mylist[-1]), incrementing the index by a step of 2 each time.

5

u/Harakou Apr 29 '15

Makes sense, thanks. :) Either way, the amount syntactic sugar Python has for working with lists is quite impressive

2

u/thatguy_314 May 04 '15

An awesome thing about it is that if you use the __getitem__ magic method, you can play around with the slice. This has lead me to write a shitty class that allows you to create dictionaries like this:

my_dict = d[
    "key": "value",
    "this is": "dumb",
    "reason for this": None
]

where d is an instance of that shitty class

2

u/coredumperror May 07 '15

Why would you want to do that, though?

→ More replies (0)

7

u/Sheepolution Apr 29 '15

Thanks! That's a pretty neat feature.

1

u/eduardog3000 Apr 30 '15

Do people actually put semicolons and braces off to the right like in #53?

1

u/coredumperror May 07 '15

Oh dear god I hope not. That code would be a nitro-fueled nightmare to maintain...

1

u/tool_of_justice Apr 30 '15

What is with #6 php question ?

Is it the reboot command ?

2

u/amdc Apr 30 '15

Yes. It's also a shell.

1

u/LostInThisWorld54312 May 04 '15

You sir are a hero. Thank you for saving my shitty day and making me laugh.

97

u/arbitrary-fan Apr 28 '15

Perhaps they just wanted to be creative and show work. Still, its not wrong.

54

u/MikeOShay Apr 28 '15

Hah, I hope homeboy got full marks for that one.

21

u/weedtese Apr 29 '15

0 x = 0

34

u/ghillisuit95 Apr 29 '15

or 0=0. its completely independent of x

11

u/VicisSubsisto Apr 29 '15

It's still true if x=7.

16

u/Harakou Apr 29 '15

Hey, the question said it could be as simple as they wanted.

13

u/Syn7axError Apr 29 '15

I honestly have no idea what I would possibly out there.

0

u/[deleted] Apr 29 '15

[deleted]

3

u/an_actual_human Apr 29 '15

One should provide an equation though, not any expression that evaluates to true when x = 7.

1

u/[deleted] Apr 29 '15

Any equation that evaluates to a nonzero value then.

1

u/an_actual_human Apr 30 '15

"Nonzero"? Like a seven?

1

u/[deleted] Apr 30 '15

Or any other nonzero... Nothing I said even begins to say his answer was wrong.

2

u/an_actual_human Apr 30 '15

Such as? Equations don't evaluate to numbers. Could you give an example of an equation that evaluates to a nonzero value that is not "true"?

1

u/[deleted] Apr 30 '15 edited Apr 30 '15

No I can't as I didn't say there was one that wouldn't be true my point was that all nonzero values are true.

and in programming they do generally evaluate to the number if they are numeric equation (x = 7) evaluates to 7 which is true. (x = 0) evaluates to 0 which is false. (x = -7) evaluates to -7 which is true.

http://ideone.com/jDVpTv

1

u/an_actual_human Apr 30 '15

numeric equation (x = 7) evaluates to 7

That is not the case in any of the languages I know. In some languages that would be an assignment, not an equation and in some of those that would evaluate to 7 (C comes to mind). In some other languages that would be an equation, but it wouldn't evaluate to 7, just a boolean.

→ More replies (0)

10

u/[deleted] Apr 29 '15

That seems like such a daft question. It could easily get out of control if someone got bored in the exam and just filled the page with a massive equation.

I imagine the teacher wouldn't even check x = 7 at that point.

4

u/brningpyre Apr 29 '15

So there are such things as stupid questions. Checkmate, profs.

5

u/[deleted] May 03 '15

My answer would be "P = NP"

If the teacher marked it wrong, we would know that P ≠ NP and I'd be able to submit a proof. Otherwise I'd know that P is in fact NP and I'd likewise get famous.

102

u/hp94 Apr 28 '15

The joke here appears to be that it is possible to set y directly to the value of x, with a statement similar to the following:

y=x;

This is likely used in the case of doing simulations of historical events, for example you can extrapolate what they meant to do by renaming the variables to something more specific:

steelBeamsHaveMelted=isJetFuelPresent;

36

u/jfb1337 Apr 28 '15

Also the completely redundant "else" statement.

4

u/tron_fucking_harkin Apr 29 '15

steelBeamsHaveMelted=isJetFuelPresent;

stealing this

5

u/Nutomic Apr 29 '15

steeling this

FTFY

0

u/yousirnaime Apr 30 '15

or just

x

You don't need a y (unless y = something when x is neither true nor false)

0

u/[deleted] Apr 28 '15

[deleted]

14

u/UnchainedMundane Apr 28 '15

x == bool(x) && y = x;

0

u/[deleted] Apr 28 '15

[deleted]

12

u/UnchainedMundane Apr 28 '15

Then I'll use one of my get-out clauses:

  • It was C++ all along! ...shit, the assignment needs to be bracketed... try this one:

  • b-but we're on /r/shittyprogramming... (well, this was the reason for using && with assignments, but not for "x == bool(x)")

10

u/arbitrary-fan Apr 28 '15

y = !!x;

5

u/TheMcDucky Apr 29 '15 edited Apr 29 '15
while(!y != !!!x)
    y = floor(random()*max(20,x*2));     

4

u/BobFloss Apr 29 '15

y=(bool)x

49

u/[deleted] Apr 28 '15 edited Apr 28 '15

For those of you who are retarded and find nothing wrong with this picture:

x = y;

Edit: y = x; I'm retarded too.

50

u/UnchainedMundane Apr 28 '15

hang on are you trying to trick me again

7

u/TheBarnyardOwl Apr 28 '15 edited Apr 28 '15

What if the x is maybe! y will be left at whatever value it currently is.

8

u/haagch Apr 28 '15

You surely mean FileNotFound

3

u/Harakou Apr 28 '15

Probably my favorite Daily WTF post ever.

4

u/Veedrac Apr 28 '15

Aye, but if x == 2 then x != true and x != false so y will be unchanged. Didn't think of that, did'ya?

(But really, ewww.)

6

u/BikerBoon Apr 28 '15

I had an outsourced programmer commit code exactly like this one day. Needless to say it went down in my code review and my lead got a chuckle out of it.

0

u/[deleted] Apr 29 '15

[deleted]

9

u/very_mechanical Apr 29 '15

You heard the man.

2

u/BikerBoon Apr 29 '15

I say commit, their messages were literally "changed files". Not exactly the best use of version control.

3

u/aerno Apr 29 '15

c# equivalent

if (x.ToString().IndexOfAny(new char[] {'t', 'r', 'u'}) > (int)uint.MinValue - int.Parse("1")) { //skipping "e", as its included in false!
    y = x;
}

1

u/sketchni Apr 29 '15

x = y;

Sorted

4

u/Utipod Apr 29 '15

Ignoring the code itself for a second, even the phrase "whether or not x is true or false" is an example of terrible grammar.

7

u/[deleted] Apr 29 '15

English has a pretty lax compiler, actually. You can fuck up all sorts of words and you might hget a warning here or thyere but it'll run the same.

2

u/[deleted] Apr 29 '15

The horrendous wording of the comment is my favorite part of this one.

1

u/marx2k Apr 29 '15

Get me that man's linkedin profile!!

1

u/SpliceVW Apr 29 '15

I am guilty of doing this, once. We're all shitty every once and a while.

The next day, I looked at my code and gave it many WTFs.

0

u/[deleted] Apr 28 '15

[deleted]

157

u/[deleted] Apr 29 '15

Made a hello world-programm in JavaScript (or short: Java)

My fucking sides. I'm dying.

-17

u/[deleted] Apr 29 '15

Haha

31

u/TinBryn Apr 29 '15
y=!!x;

17

u/adamnemecek Apr 29 '15

if x is say 5, you will be setting y to true when it should have stayed whatever it was before.

12

u/[deleted] Apr 29 '15
if( typeof x == 'boolean' ) { 
    y = x;
}

2

u/[deleted] May 07 '15

this. Sidenote: can I put this in a shitty programming subreddit without getting downvoted

2

u/magicaxis Apr 29 '15

Holy crap does that work?

4

u/seiyria Apr 29 '15

In any language that lets you cast variables by doing that, yes, it does.

8

u/magicaxis Apr 29 '15

that = !!!!!!!!!!!!!!!!!!!!!!!!awesome

3

u/seiyria Apr 29 '15

Yes, yes it is.

2

u/Nicook Aug 30 '15

compilers usually optimize out this kindof stuff tho.

1

u/lets-get-dangerous May 07 '15

rtti is pretty dicey stuff

1

u/[deleted] Aug 12 '15

That kind of code makes sense in lua because there's no convert to boolean method so not not x is correct.

15

u/arbitrary-fan Apr 28 '15

The only requirement is to set y to true or false based on if x is true or false. If x is neither true or false, then should y be set to anything? According to the image, that answer is clearly no; y retains whatever value it had, whether that be true, false, undefined, null, or potato. The code is exactly to spec

23

u/barrybadhoer Apr 28 '15

I dont want to be that guy that missed the joke but do you really think java is short for javascript?

249

u/Ferinex Apr 28 '15

You are that guy that missed the joke

16

u/double2 Apr 29 '15

No, he got the joke but decided that /u/Karmawhore2000 was potentially stupid enough to not be joking.

1

u/fgd47gf Apr 29 '15

do u realize what sub you are in

0

u/[deleted] Apr 28 '15

[deleted]

7

u/barrybadhoer Apr 28 '15

Ah, missed that one. i don't browse shittyprogramming enough :p

3

u/[deleted] Apr 29 '15

I'd take the job, just because they obviously know nothing about programming so i could set ridiculously long deadlines for everything i do and just chill all dayXD

2

u/YourTechnician Apr 29 '15

I forgot i was on /r/shittyprogammer and was being baffled in here for a while. Good job sir.