ANyways, so... I was reading The Pragmatic Programmer [1] and it occurred to me that we should probably boil these principles down for the novices amongst us. ITT things you want to carve into your colleagues' faces.
* KEEP IT FUCKING SIMPLE, MOTHERFUCKER!
* You're code is not ``clever'', it is autistic.
* Code is /not/ poetry or art. Go away! Fuck your OCD.
* This shit has been solved a thousand times over.
* You do not need to design with the latest and greatest in gang-of-four approved OOP design patterns, using infinitely scalable NoSQL solutions in hip new languages that compile down to JavaScript (srsly WTF?!), just to create a CRUD application.
* Not everyone gets off on code, some of us just want to make a living doing the least amount of effort that is required to deliver a consistent quality for an extended period of time.
I swear by god if I see one more AbstractControllerFactoryInterface
Hint #3: When I gave the "no array" hint, I was thinking more of what you're thinking to do with the array than whether or not there is one. (Memory is a big array. Another hint there...)
>>34 is the closest so far, but screwed up severely at >>39. Recursion is not necessary for this problem either, so even if you didn't have >>39 you wouldn't have passed.
[Why is this reminding me of FizzBuzz? It shouldn't be that much harder, but some of you are providing solutions that would be the equivalent of writing a FizzBuzz that doesn't work for numbers greater than 15 (another hint).]
I'll leave you with a final advice: Read the problem statement carefully, preferably 2 or more times. Does your solution work for all of the problem space? Think about it.
void print(unsigned int x)
{
unsigned int q, z;
for (q = 1000000000; q > 1; q /= 10) {
z = x / q;
if (z) putchar('0' + z % 10);
}
putchar('0' + (x % 10));
}
int main()
{
unsigned int i = 0;
int c;
while (EOF != (c = getchar())) {
print(i++);
putchar(' ');
do {
putchar(c);
} while ((c != '\n') && (EOF != (c = getchar())));
}
return 0;
}
I did not know C but I have tried to do something pausible, and now you are digging requirements out of nowhere. What is it now? I have not got time for that. Adios!
I knew Cudder氏 would not give up for anything but her own (which is impossible for me). I'd better stick to a dumb physics, chemistry or architecture undergrad. ;__;
Name:
Anonymous2013-05-12 13:27
Have some slow as fuck x86 code. But hey, no arrays. Though I don't get the comment about memory being an array, you wouldn't be able to make syscalls without it.
Hello, I'm a beginner programmer so I don't really know how files work. Can you just use the end of the file as a queue? You need to append to the file at one point anyway because of the line numbering increases its size. I'm thinking of something like:
keep line counter i
loop: read character and enqueue it
if the character is '\n' add "i " into the queue and increment i
if the character is EOF organize the queue into an array and we're done
print next character in queue to replace the read one and loop
Please tell me how I'm wrong.
Name:
Anonymous2013-05-12 14:32
#include <stdio.h>
#include <stdlib.h>
int main(void) {
unsigned line = 0;
char c;
while((c = getchar()) != EOF) {
printf("%u ", line++);
do
putchar(c);
while((c = getchar()) != EOF && c != '\n');
putchar(c);
}
return EXIT_SUCCESS;
}
amirite?
Name:
Anonymous2013-05-12 14:58
From Cudder's broad description of an array it sounds like the task is the equivalent of programing a Turing machine without any tape.
>>49
I have not read K&R because I do not have one. But stdio.h says int putchar(int) and int getchar(), that is why they are being pedantic I guess. Anyway I do not know how could char be wrong, since char is signed, ASCII uses only 127 values and EOF == (char)EOF.
>>51
Fuck you copycat, murderer, you tried to be ``clever'' but just introduced a bug and ruined my code.
>>42
Nothing. As I said above, it's more to do with how you use it than whether or not there is one.
So now we have another C solution that's much closer to passing, another broken C solution, Asm that's not much better than what a compiler could do... but not a single remark on why attempts like >>20 and >>30 don't work for all of the problem space! All you did was get hung up on one comment about the use of arrays; I intended that as a hint to get you thinking about the problem in the right direction, not as a "thou shalt not".
Keep on trying. Not trying as in writing more possibly broken code, but as in actually thinking about the problem...
Name:
Anonymous2013-05-13 8:21
You got a pdf brah? All I can find are shitty CHM converts of that book
>>64
So I've read through all of the answers, and it looks like your real problem is files with individual lines stretching beyond 4GiB, or 16EiB, or whatever, which would mean that your catch is `you better not store each line in an array as you read it in.' Okay, fine. That gets rid of >>20.
Another possible issue you're having is that the trivial file '' is not numbered correctly. I.e.
foo
bar
baz
quux
should become
1 foo
2 bar
3 baz
4 quux
5
? I don't agree with this myself for religious reasons, but I think it's something you might do, as it eliminates pretty much everyone and fits with your desire to say `the PROBLEM SPACE!' as you point out, smugly, that an empty sequence of 0 bytes is a valid line. Then the exiting of >>34 would also be caused by the specific (EOF != (c = getchar())) segment, though the problem with it is `it exits too fast' instead of `it uses in-comparison =' or `it assigns char/int wrong' as everyone has been assuming.
It hasn't been you specifically who complains about I/O buffers being arrays, so I'll assume getchar and putchar are fine. That means that if I don't do anything stupid like readline and make sure to number correctly, this should fit your (poorly-communicated) requirements:
#include <stdio.h>
int main(void)
{
unsigned int ln = 2;
int i;
printf("1 ");
i = getchar();
while (i != EOF) {
putchar(i);
if (i == '\n') printf("%u ", ln++);
i = getchar();
}
return 0;
}
As a final note, you should rephrase the question to point out that the line numbers must be correct, as 0 is a perfectly valid decimal integer that can be prepended to all lines. You should also clarify that this need not be done in place, if that is truly your intention.
>>70
No way she is so dumb!
There were TM jokes, maybe she wants these sort of jump tables people use for emulators:
#include <stdio.h>
void print(unsigned int x)
{
unsigned int d = 1000000000;
while (d > 1 && x / d == 0) d /= 10;
while (d > 1) {
putchar('0' + x / d % 10);
d /= 10;
}
putchar('0' + x % 10);
putchar(' ');
}
int main()
{
unsigned int i = 1;
int s = 1, c = getchar();
while (c != EOF) {
switch (s) {
case 1:
print(i++);
putchar(c);
s = 0;
break;
case 0:
putchar(c);
s = (c == '\n');
break;
}
c = getchar();
}
return 0;
}
or with a fall-through:
switch (s) {
case 1:
print(i++);
s = 0;
case 0:
putchar(c);
s = (c == '\n');
}
Name:
722013-05-13 10:53
Oh, I do not even need the s = 0; in the fall-through switch...
So, last submission:
#include <stdio.h>
void print(unsigned int x)
{
unsigned int d = 1000000000;
while (d > 1 && x / d == 0) d /= 10;
while (d > 1) {
putchar('0' + x / d % 10);
d /= 10;
}
putchar('0' + x % 10);
putchar(' ');
}
int main()
{
unsigned int i = 1;
int s = 1, c = getchar();
while (c != EOF) {
switch (s) {
case 1:
print(i++);
case 0:
putchar(c);
s = (c == '\n');
}
c = getchar();
}
return 0;
}
An idea: most of these C solutions don't work for the (231−1)th line number. Maybe Cudder is hinting at something like this, with the whole "problem space" thing:
#include <stdio.h>
int main()
{
unsigned int i = 0;
int c;
while (EOF != (c = getchar())) {
if (i == 0xffffffffUL)
printf("4294967296 ");
else
printf("%u ", ++i);
do {
putchar(c);
} while ((c != '\n') && (EOF != (c = getchar())));
}
return 0;
}
PS: The only reason anyone is having trouble solving this is because you're shit at explaining the problem. There's no reason to act smug about this, and you should really cut it out.
Name:
Anonymous2013-05-13 11:59
>>76
Is it really you? Is it destiny we are coming back to this cesspool. I have also recently started visiting this board again. Check out my ☆★Prog challenge [Physiks]★☆, just like the good ole days except no Physics ;) I actually moved to Physics, after CS degree is complete. How's life treating you old friend.
>>77
Nope, Xarn never posts onymously without a tripcode. I thought my post felt kinda Xarn-y, though, so I figured it'd be a nice homage to put that in the name field.
(If the real Xarn reads this: I hope you appreciate having become a symbol of non-shitposting.)
Name:
Anonymous2013-05-13 12:26
>>78
>Xarn
>non shitposting
guess how many you should pick
Name:
Anonymous2013-05-13 12:38
>>75
I am wrong, okay? As you wish: #include <stdio.h>
int main()
{
unsigned int i = 1;
int s = 1, c = getchar();
while (c != EOF) {
switch (s) {
case 1:
printf("%u ", i++);
case 0:
putchar(c);
s = (c == '\n');
}
c = getchar();
}
return 0;
}
>>76 Fuck you, do you really think I have not tested unsigned int bounds?
Well, 2³²-1 equals UINT_MAX, so as long as the file has no more than 2³²-1 lines (4294967295), the program is right.