Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

The /prog/matic programmer

Name: Anonymous 2012-09-27 15:16

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

[1] http://pragprog.com/the-pragmatic-programmer, easily found online.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-12 7:44

>>24,26,28
Hint #1: It's not a solution if it doesn't work for all of the problem space.

(Big) hint #2: >>29

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.

Name: Anonymous 2013-05-12 9:24

What's wrong with having a buffer?

Name: Anonymous 2013-05-12 10:30

>>44
whata fuck man how 2 get dubs??????????????????1

Name: Anonymous 2013-05-12 11:02

You bastard

Name: Anonymous 2013-05-12 11:15

>>44
Really strong dubs. Nice, very nice!

Name: Anonymous 2013-05-12 11:18

>>19
The fuck is in-place streaming?

Name: Anonymous 2013-05-12 13:02

>>34
Fuck you, I will not implement libc.

>>39,41
Fuck you two.


#include <stdio.h>

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: Anonymous 2013-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.

%define    STDIN          0
%define    STDOUT         1
%define    SYSEXIT        1
%define    SYSREAD        3
%define    SYSWRITE       4
%define LF             0x0A
%define SPACE          0x20
%define EOF            0

%macro sys 1
    mov        eax,    %1
    push       eax
    int        80h
    add        esp,    16
%endmacro

%macro sys.exit 1
    push       dword %1
    sys        SYSEXIT
%endmacro

%macro sys.read 0
    push       dword 1
    push       dword bufs
    push       dword STDIN
    sys        SYSREAD
%endmacro

%macro sys.write 1
    push       dword 1
    push       dword %1
    push       dword STDOUT
    sys        SYSWRITE
%endmacro

%macro ascii.bufl 0
    or         eax,    0x30
    mov        [bufl], al
%endmacro

%macro div.32 2
    mov        eax,    %1
    mov        ebx,    %2
    xor        edx,    edx
    div        ebx
%endmacro

section .bss
    bufs:      resb 1          ; character buffer for STDIN
    bufl:      resb 1          ; character buffer for line count
    rem:       resd 1          ; division remainder
    dig:       resd 1          ; ASCII conversion divisor

section .data
    lnc        dd   0          ; line count

section .text
global main

main:
    sys.read
    cmp        eax,    dword EOF
    jne        num
    sys.exit   0
num:
    inc        dword [lnc]
    mov        [dig],  dword 1000000000
    div.32     [lnc],  [dig]
    mov        [rem],  edx
    ascii.bufl
    sys.write  bufl
numl:
    div.32     [dig],  10
    mov        [dig],  eax
    cmp        [dig],  dword 1
    je         done
    div.32     [rem],  [dig]
    mov        [rem],  edx
    ascii.bufl
    sys.write  bufl
    jmp        numl
done:
    mov        eax,    [rem]
    ascii.bufl
    sys.write  bufl
    mov        [bufl], byte SPACE   
    sys.write  bufl
line:
    sys.write  bufs
    cmp        [bufs], byte LF
    je         main
    sys.read
    cmp        eax,    dword EOF
    jne        line
    sys.exit   0

Name: Anonymous 2013-05-12 13:32

What is wrong with while (EOF != (c = getchar()))?

That's what they do in K&R

Name: Anonymous 2013-05-12 13:42

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: Anonymous 2013-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: Anonymous 2013-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.

Name: Anonymous 2013-05-12 15:45

>>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.

Name: Anonymous 2013-05-12 16:01

in-place streaming
iterative process?

Name: Anonymous 2013-05-12 16:17

>>53
Ahh, I did miss the mismatched type. Does the compiler even give a warning about the lack of a cast?

Name: Anonymous 2013-05-12 16:42

>>56
No, it does not. If EOF is a constant in char range, there is nothing wrong with "char c = EOF;".

Name: Anonymous 2013-05-12 16:55

I mean, do people use ^? these days?

Name: Anonymous 2013-05-12 17:11

>>56
The issues is c = getchar() is mismatched.

Name: Anonymous 2013-05-12 17:22

>>58
Like if getchar() would return anything outside the char range... >.>

Name: Anonymous 2013-05-12 18:15

What the fuck is 66^?99?

Name: Anonymous 2013-05-12 18:43

>>60
Yes, those things from the PC charset like ☻, ╟ and ÿ.

Name: Anonymous 2013-05-12 23:19

Name: Anonymous 2013-05-13 7:38

UVB-76, UVB-76, 93, 882, NAIMINA, 74, 14, 35, 74, 9, 3, 8, 8, 2, Nikolai, Anna, Ivan, Mikhail, Ivan, Nikolai, Anna, 7, 4, 1, 4, 3, 5, 7, 4.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-13 8:16

>>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: Anonymous 2013-05-13 8:21

You got a pdf brah? All I can find are shitty CHM converts of that book

Name: Anonymous 2013-05-13 9:32

NIGGERS
☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻
☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻

Name: Anonymous 2013-05-13 10:27

>>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.

Name: Anonymous 2013-05-13 10:27

>>64
What are you saying? My solution works. u.u

Name: Anonymous 2013-05-13 10:32

>>67
No way! So an empty file has one line?

Name: Anonymous 2013-05-13 10:35

>>69
Based on Cudder's `PROBLEM SPACE' comments, I suspect it might be, especially as it fits with the reason he gave for >>34 not being correct.

Name: Anonymous 2013-05-13 10:49

>>70
Then Cudder is wrong. An empty file has zero lines.

coax@neko:~$ touch empty
coax@neko:~$ ls -l empty
-rw-r--r--   1 coax     staff          0 May 13 09:48 empty
coax@neko:~$ wc -l empty
       0 empty

Name: Anonymous 2013-05-13 10:50

>>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: 72 2013-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;
}


I hope you enjoyed my suffering. ;__;

Name: Anonymous 2013-05-13 11:14

And I know I should use something like d = pow(10, floor(log(x))). Ain't got time for that.

Name: Anonymous 2013-05-13 11:19

>>74
Out of curiosity, what is your logic for not using goddamn printf?  The implicit array required by the format specifier?

Name: Xarn 2013-05-13 11:51

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: Anonymous 2013-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.

Name: Xarn 2013-05-13 12:04

>>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: Anonymous 2013-05-13 12:26

>>78
>Xarn
>non shitposting
guess how many you should pick

Name: Anonymous 2013-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.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List