Easier Than Fizz Buzz - Why Can't Programmers Print 100 to 1? (article)
-
jsfiddle/jsperf link for comparison?
I take it back. I thought I had a test for that. just verified and I didn't.
but still cutting iterations by two allow half the calls to print function
-
Hence me not calling you on that bit :D
-
I'm a little surprised nobody has posted this sort of thing yet:
I thought it, but was too lazy to write something to autogenerate it.
-
for( int i = 0; i == 0 || i > 1; --i ){ if( i ==0 ) i = 101; printf("%d\n" i - 1); }
Filed Under: Not falling for it
-
Ok, I had to possibly break the rules slightly, I have one statement outside of the loop...
#include <stdio.h> #include <stdio_ext.h> int main(void) { for(int i = 0; printf("%u", 100 - i++) < 4; i <= 100 ? printf("\n") : printf("")); __fpurge(stdout); return 0; }
Tested using:
onyx@jarvis ~> gcc --version gcc (Debian 4.9.2-10) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. onyx@jarvis ~> gcc stoopid.c -o stoopid.o -std=c99
Maximum level reached. I'm here all the time. Try the
/t/1000
.
-
Is it weird that I can't think of a suitably WTFy solution in TRWTF of languages?
-
It's javascript.
for (var i = 0; i < 100; i += 1) { if (!a) a = []; a.push(100-i); } var a; console.log(a.join('\n'));
-
is up with highlighting a code block like a comment block? \*checks source* `` :wtf::question:
-
No idea. View raw, I even hinted js.
-
well yes.... but i cannot bring myself to abuse that "feature" of JS.
that "feature" is evil and must not be abused or nasal demons will erupt
-
for (var i = 0; i < 100; i += 1) { if (!a) a = []; a.push(100-i); } var a; console.log(a.join('\n'));
Ah; hint is
javascript
-
Didn't you have the same issue with
js
not working and being detected as SQL in another thread?I remember seeing that and pointing out the hint (and wondering why it doesn't have/support multiple hints per language).
-
Print 100 to 1.
Easy.
You need to start with "for(int i=0;" and continue from there - you cannot write anything before "for(int i=0;" and you can't use two loops.
A little trickier, but can be done in about 3 lines. most of the time will be spent starting up the IDE. Of course, if the give you
for(int i=0;
, that step has probably been done for you.Typing. Typing is the slow part.
for(int i=0; i < 100; i++) { Console.WriteLine(100 - i); }
30 seconds + about 2 minutes for the rant. But I spend a lot of time ranting about my employer, so I guess I'm atypical. :P
-
And while reading through the other replies, I was surprised to see these solutions missing:
for(int i=0; i>-100; i--) { Console.WriteLine(100 + i); }
for(int i=0; i>=-100; --i) { Console.WriteLine(Math.Abs(i)); }
-
it is possible to get a CIS degree without knowing how to program at all.
Is it safe to say that the people with CIS degrees who don't know how to program are... CIS scum?
-
More worrying IMO is the whining part. One of the first things I learnt was to never, ever, complain about my previous workplaces. Some remarks are good to show criticism (eg. bad communication between teams) but to go on and on about your boss being an ass to your possibly new boss is a really stupid way of handling yourself.
And for those reasons, the first question is really a gotcha question, like, "What is your greatest weakness?" or "Why shouldn't we hire you?" or "When did you stop raping children?" Supposedly, these questions are to test how well the candidate responds when caught off guard in an uncomfortable situation, and while there may be some merit in applying this test to entry-level people, it's somewhat insulting to throw it at someone with 10+ years experience.
If they were to ask me:
Talk about a few things in your current organization or manager that you don't like / aren't happy with.
My response would be, "Isn't it bad form to speak negatively of a current or previous employer in situations like these?" If they pressed me for an answer, I might tell them that I'm bound by NDA from speaking about such things, or ask them if they would feel comfortable having ex-employees speaking so frankly about their own organizational woes.
-
I guess to "prove" they aren't the crappy candidates themselves?
No, to prove that the problem can be solved so crappily that it doesn't matter how fast you did it, you still suck.
-
for (var i = 0; i < 100; i += 1) {
if (!a) a = [];
a.push(100-i);
}
var a;
console.log(a.join('\n'));I will cheat and drop the space so i can have for(inti=0; instead.
[code]
for (inti = 0; inti===0 || inti.length < 100;) {
inti === 0 ? (inti=[]) : inti.push(100-inti.length);
}
console.log(inti.join('\n'));
[/code]Also, one variable is now all of the things. My colleages sure are glad that I do not code like this in practice, I save my for you lot.
Of course, the above clearly can't work in a strongly typed language where you have to declare i as an int
-
Think about the person doing the interview. They don't want to listen to your bitching, the[sic] want to know if you're a fitted candidate and usually it's better to have a positive position.
This, +1. If you aren't addressing this, you're wasting everyone's time.
I disagree to a point. If you've come from a shitty environment it can be a good way to gauge the competency of your prospective employer. If your old company was batshit insane and did insane things, any sane person is going to listen to that and think "Holy shit that place was stupid". The place that's like "You complained a lot about your old job" is likely just as bad because they don't see anything wrong.
I'd often use something similar, not outright bitching but more like pointing out flaws with things such as a mandate to not use a framework that could have helped or being forbidden from improving code. The reason is it's a smell test: If the person interviewing me doesn't pick up on why that would cause a lot of issues, they're probably just as clueless and ignorant.
In short: You bitch about your current/ex job in the hopes the person interviewing will think "We'd never treat anyone so bad that they felt the need to bitch about it". If they think anything else, and especially anything against you for bitching, they have the same problems.
Sometimes it's just random bitching for stupid reasons e.g. They didn't give me dual monitors, etc. But usually the bitching is because the old company had major problems and the interviewee wants to make sure the new company doesn't.
The key to doing that, if you're going to risk that at all, is in your second paragraph: specific example(s). Anyone I interview that just says, "they suck, blah blah" gets nowhere with me. "Previous employer, Consulting Company A, required us to reuse MS Office keys to activate software for other customers as 'good customer service.' I have a real problem with that. After bringing up to management the idea that our customers should be buying their own keys and they shot it down, I decided to start looking for employment elsewhere." I can respect that [assuming by the end of the interview I have reason to believe (s)he's being truthful - not the best example, but it illustrates the idea that a complaint could be legitimate and indicate a positive attribute of the candidate].
The risk is giving the impression to the interviewer that you have a negative attitude and could be a problem. It's a fine line to walk.
And again, instead of people talking about the really important issue of crappy IT interview candidates polluting job pools everywhere, people just start posting code samples. I guess to "prove" they aren't the crappy candidates themselves? We can just trust you on that, ok?
+1
It's a pretty banal point at this point. Why rehash that shit over and over and over when we can have fun coming up with WTFs from trivial problems?
Dammit @boomzilla, you just had to come up with a
goodworthwhile reason?
-
Discussion about an article mentioning a programming problem turns into a code competition for solving said problem. Who would have thought?
And again, instead of people talking about the really important issue of crappy IT interview candidates polluting job pools everywhere, people just start posting code samples. I guess to "prove" they aren't the crappy candidates themselves? We can just trust you on that, ok?
'd
Filed under:
It's a pretty banal point at this point. Why rehash that shit over and over and over when we can have fun coming up with WTFs from trivial problems?
Because that can apply to lots of topics.
-
What about multiplication?AND Gate1 * 1 = 11 * 0 = 00 * 1 = 00 * 0 = 0
Logical operations are not mathematical operations.
Those people, if they have a degree, should fight to get at least a partial refund.
At least it's not your fault. ;-)
DissedCourse Toaster:
Consider replying to several posts at once
Shut up DissedCourse. Make it UN-clunky to do so first, then I'll consider it. I'm too tired to fight with you.
-
Logical operations are not mathematical operations.
I was being half-facetious, but I'll defend my statement anyway. At least one of us (hopefully) will learn something that way.
AND Gate
Note the word "Gate" a physical construct built out of transistors. At the lowest level (that I know of - the IC/Gate/CPU level, don't know what to call it), all mathematical operations are composed of logical ones. In this case, I was pointing out that a logical AND has the same properties as multiplication. At least for a single digit. I have no idea how to or even if it extends to multiple digits.
-
If you get the right output without mistakes in a reasonable amount of time we consider the answer correct.
TRWTF
There are some professions where the ability to get things right first time every time is critically important. Programming is not one of them.
-
```
#include <stdio.h>
#include <stdio_ext.h>int main(void)
{
for(int i = 0;\>you cannot write anything before `for(int i=0;`
-
I wanted to make it easy to copy if anyone wants to test the WTFery for themselves. It also includes a rather uncommon header for such a small bit of code so I wanted to make it clear for people.
Excuse me for trying to be helpful.
-
The point being that the question as written actually is impossible. The article author is a shit, and everyone in this thread is a double shit for swallowing that “80% of people can't perform this simple task” click bait.
-
From the article:
[Update: This is supposed to be a code snippet which already exists inside a function, so you can safely assume that inclusion of headers and declaration of the functions etc. is already done for you and you don't need to worry about that.]
I did, however, since the pure ery involved is too much for me to process without testing, so I needed to compile it and check. And when I already had that I just pasted it here for convenience.
To solve it as originally intended, as in not creating a C feature abuse abomination, I can do that in my head and would be able to do it in an interview.
-
Yeah, and so would every single human on earth (except the ones that don't know C (those lucky bastards!)). Look, if a self-help book author tells you about a simple test that 80% of people are unable to pass, you can be pretty sure that nothing they're going to tell you after that is going to be of any value. The book is third in its category on amazon right now, and you are an enabler for enabling that kind of viral marketing bullshit to work.
That article had zero informative content; the so-called data that was provided was no more valid than if it had been read out of the bottom of a teacup, and the fact that so many people went out and bought that book just to validate their own stupid preconceptions pisses me off (well, actually not really, but for the sake of conversation let's pretend it does).
-
you are an enabler for enabling that kind of viral marketing bullshit to work
Huh? This is the bit where I go , maybe even make it my daily one. I didn't create this thread nor post this link. I didn't share it on twatter, bookface, nor tumbleweed. My only comment on the subject which didn't involve horrible C was a self-deprecating sarcastic joke. After that I had some fun writing horrible code because I felt like it.
Did you respond to the wrong person perchance?
That article had zero informative content; the so-called data that was provided was no more valid than if it had been read out of the bottom of a teacup, and the fact that so many people went out and bought that book just to validate their own stupid preconceptions pisses me off
It was rather weak on content, yes. The dataset was small, the correlation is tenuous at best, and pretty much no one had any particularly insightful comments to make, so we decided to mess with code. Which some of us weirdos find fun. And fun is mostly the reason we come to this place. Connect the dots.
(well, actually not really, but for the sake of conversation let's pretend it does)
Oh. Carry on then.
-
Connect the dots.
Huh?
That's just a roboto and a flower-dog. What exactly are you trying to tell me?
-
That article had zero informative content; the so-called data that was provided was no more valid than if it had been read out of the bottom of a teacup
You have really high standards for blog posts.
-
What exactly are you trying to tell me?
I'm trying to discern why you attacked me to be an "enabler" of something when all I did was post some horrible C code as a joke.
-
I'm sure there is someone on the internet who formulated a law for that ...
-
An interesting problem…
for(int i=0; printf("%c%c%c%c",32+17*!i,32+(25+(i+1)/10)*(i>-91&&i)+16*!i,58+i%10-10*!(i%10),10)*--i;) while(!(i+'d'));
-
you can't use two loops.
And posts can't be empty.
-
I'm a little surprised nobody has posted this sort of thing yet:
Needs more Switch.
for (int i = 0; i < 100; i++) { switch (i) { case 0: System.out.println("100"); break; case 1: System.out.println("99"); break; case 2: System.out.println("98"); break; case 3: System.out.println("97"); break; case 4: System.out.println("96"); break; case 5: System.out.println("95"); break; case 6: System.out.println("94"); break; case 7: System.out.println("93"); break; case 8: System.out.println("92"); break; case 9: System.out.println("91"); break; case 10: System.out.println("90"); break; case 11: System.out.println("89"); break; case 12: System.out.println("88"); break; case 13: System.out.println("87"); break; case 14: System.out.println("86"); break; case 15: System.out.println("85"); break; case 16: System.out.println("84"); break; case 17: System.out.println("83"); break; case 18: System.out.println("82"); break; case 19: System.out.println("81"); break; case 20: System.out.println("80"); break; case 21: System.out.println("79"); break; case 22: System.out.println("78"); break; case 23: System.out.println("77"); break; case 24: System.out.println("76"); break; case 25: System.out.println("75"); break; case 26: System.out.println("74"); break; case 27: System.out.println("73"); break; case 28: System.out.println("72"); break; case 29: System.out.println("71"); break; case 30: System.out.println("70"); break; case 31: System.out.println("69"); break; case 32: System.out.println("68"); break; case 33: System.out.println("67"); break; case 34: System.out.println("66"); break; case 35: System.out.println("65"); break; case 36: System.out.println("64"); break; case 37: System.out.println("63"); break; case 38: System.out.println("62"); break; case 39: System.out.println("61"); break; case 40: System.out.println("60"); break; case 41: System.out.println("59"); break; case 42: System.out.println("58"); break; case 43: System.out.println("57"); break; case 44: System.out.println("56"); break; case 45: System.out.println("55"); break; case 46: System.out.println("54"); break; case 47: System.out.println("53"); break; case 48: System.out.println("52"); break; case 49: System.out.println("51"); break; case 50: System.out.println("50"); break; case 51: System.out.println("49"); break; case 52: System.out.println("48"); break; case 53: System.out.println("47"); break; case 54: System.out.println("46"); break; case 55: System.out.println("45"); break; case 56: System.out.println("44"); break; case 57: System.out.println("43"); break; case 58: System.out.println("42"); break; case 59: System.out.println("41"); break; case 60: System.out.println("40"); break; case 61: System.out.println("39"); break; case 62: System.out.println("38"); break; case 63: System.out.println("37"); break; case 64: System.out.println("36"); break; case 65: System.out.println("35"); break; case 66: System.out.println("34"); break; case 67: System.out.println("33"); break; case 68: System.out.println("32"); break; case 69: System.out.println("31"); break; case 70: System.out.println("30"); break; case 71: System.out.println("29"); break; case 72: System.out.println("28"); break; case 73: System.out.println("27"); break; case 74: System.out.println("26"); break; case 75: System.out.println("25"); break; case 76: System.out.println("24"); break; case 77: System.out.println("23"); break; case 78: System.out.println("22"); break; case 79: System.out.println("21"); break; case 80: System.out.println("20"); break; case 81: System.out.println("19"); break; case 82: System.out.println("18"); break; case 83: System.out.println("17"); break; case 84: System.out.println("16"); break; case 85: System.out.println("15"); break; case 86: System.out.println("14"); break; case 87: System.out.println("13"); break; case 88: System.out.println("12"); break; case 89: System.out.println("11"); break; case 90: System.out.println("10"); break; case 91: System.out.println("9"); break; case 92: System.out.println("8"); break; case 93: System.out.println("7"); break; case 94: System.out.println("6"); break; case 95: System.out.println("5"); break; case 96: System.out.println("4"); break; case 97: System.out.println("3"); break; case 98: System.out.println("2"); break; case 99: System.out.println("1"); break; } }
-
It's a TDWTF clbuttic!
-
>you can't use two loops.
And posts can't be empty.
Fixable. But it's not quite as awesome as @dkf's original:
for(int i=0; printf("%c%c%c%c",32+17*!i,32+(25+(i+1)/10)*(i>-91&&i)+16*!i,58+i%10-10*!(i%10),10)*('d'+--i););
-
And posts can't be empty.
Huh; missed that bit.
for(int i=0;!(i+'d')||printf("%c%c%c%c",' '+021*!i,' '+(031+(i+1)/'\n')*(i>-'['&&i)+020*!i,':'+i%'\n'-'\n'*!(i%'\n'),'\n'+'"'*!!((i-1)%'\n'));i-=!!(i+'d'));
I think I like this one more. It produces nice tabulated output. One should always strive to produce output in a neat format that people can understand at a glance.
-
Bonus points for using
printf
return value. Well, seemingly at least, I honestly don't even want to try and parse that :P
-
Bonus points for using
printf
return value. Well, seemingly at least, I honestly don't even want to try and parse that :PI was originally hoping to use the
printf
return value in a “useful” way, but that's really awkward without introducing an extra variable. So I decided to go with the crime against humanity instead; the character/integer duality is particularly “helpful” here.
-
I kinda managed to do it, but I did have to add an additional check in the increment section of the
for
, otherwise I'd get an extra line every time. And a0
too, IIRC.
-
Not really; it's always 4. Here's my analysis:
!(i+'d')
:'d'
is 100. This becomes true oncei
reaches -100' '+021*!i
:' '
is 32. 021 is 17. Together these make 49, the ascii code for1
, which is printed ifi=0
, else this prints a space' '+(031+(i+1)/'\n')*(i>-'['&&i)+020*!i
020*!i
with the space makes 48 (or'0'
) ifi=0
i>-'['&&i
:'['
is 91. This is 1 whilei
is between -1 and -90 inclusive, hence when we're writing 2-digit numbers031+(i+1)/'\n'
:'\n'
is 10, andi
is negative so we round down.' '+031
makes 57, or':'
, which comes after'9'
in ascii. Adding up this returns the appropriate character between'1'
and'9'
.
':'+i%'\n'-'\n'*!(i%'\n')
':'
comes after'9'
+i%'\n'
adds a number between 0 and -9'\n'*!(i%'\n')
equals 10 wheni%10=0
'\n'+'"'*!!((i-1)%'\n')
:'\n'
is 10.'"'
is 22. 32 is','
. This prints a newline if(i-1)%10=0
, else a comma.i-=!!(i+'d')
:'d'
is 100. This decrementsi
unlessi=100
The function decrements
i
, printingi+100
character by character right-aligned, comma-separated with 10 numbers per line. Oncei
reaches-100
, the code goes into an infinite loop without printing anything, as the||
short-circuits.EDIT: Missed description of the 4th character
-
I kinda managed to do it, but I did have to add an additional check in the increment section of the
for
, otherwise I'd get an extra line every time. And a0
too, IIRC.Here's a more conventional version, without the majority of UB:
for(int i=0;i-'d';i++){ static char *I="0987654321 \n,"; printf("%c%c%c%c",I[10-!i],I[(i-1)/10+!!i],I[i%10],I[11+!!((1+i)%10)]); }
-
Once i reaches -100, the code goes into an infinite loop without printing anything, as the || short-circuits.
I was particularly proud of that. The original description of the problem never called for the program to exit.
-
Here is my shot:
for(int i = 0; i < 10001; i += 100){ printf("%d ", i % 101); }
Prints a superfluous zero at the beginning, but that can be easily skipped with an if.
The program also requires a main function and an include of an i/o header so the requirement not to write anything before the 'for' seems too strict.
-
Well, I'm actually using that comparison to break out of the loop. But I'm intentionally overflowing the
unsigned int
so it makes sense. Well, in a WTF way, that is.I'm just ticked off that I couldn't fit the output buffer clearing in the loop itself.
-
If I could harass every person on the internet who ever liked anything ever, I would. But that would require effort, so I just do it one at a time, as the whim takes me. This time the hammer fell on you.
Please take a minute to fill out this survey:
- Aggravation /5
- Bloviation /5
- Consternation /5
- Quality of Service
-
- Aggravation /5
-
Bloviation /5
-
Consternation /5
-
Quality of Service
-
1/5 Because I had to check the dictionary for some of this bullshit you put in the poll!
-
4.5/5 if I am supposed to mark yours, 2/5 for mine because I was lazy
-
3/5 but only because this is TDWTF and I expect trolling. Marked down due to natural defences of the local inhabitants, not your skills
-
Needs more quote mining and goalpost moving. Would get trolled again, but not at full price.
-
-
IT is full of unqualified people (I'm one of them by the way, I stumbled my way through this career choice out of sheer will rather than actually going to university to learn). I doubt the article's statistical claims though. It is far, far too much, even for how easy it is to get into IT by bluffing sometimes.