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

Pages: 1-4041-8081-120121-

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: Anonymous 2012-09-27 15:17

No

Name: Anonymous 2012-09-27 15:52

You're code is not ``clever''
Nor is your ironic misuse of the English language.

Name: Anonymous 2012-09-27 17:30

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.
I don't even know what that means!

Name: Anonymous 2013-05-09 5:29

>>null
nice dubs bro

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-09 8:33

This issue largely comes from traditional CS curriculum (SICP included). They start off by saying that complexity and abstraction are good things (only in moderation), "premature optimisation is evil" (it's never premature), and use those as arguments to convince the students that all software needs to be multi-layered elephantine systems broken up into hundreds of classes, packages, design patterns, and other cruft.

Quoting from the first SICP lecture:
"And these techniques for controlling complexity are what
this course is really about. And in some sense that's really
what computer science is about. Now that may seem like a
very strange thing to say, because after all a lot of people
besides computer scientists deal with controlling complexity. A large airliner is an extremely complex system. And the aeronautical engineers who design that air, you know, are dealing with the men's complexity. But there's a difference between that kind of complexity and what we deal with in computer science. And that is that computer science in some sense isn't real. You see, when an engineer is designing a physical system that's made out of real parts, the engineers who worry about that have to address problems of tolerance and approximation and noise in the system. So, for example, as an electrical engineer I can go off and easily build a one-stage amplifier or a two-stage amplifier, and I can imagine cascading a lot of them to build a million-stage amplifier, but it's ridiculous to build such a thing, because by the --- long before the millionth stage the thermal noise in those components way at the beginning is gonna get amplified and make the whole thing meaningless. Computer science deals with idealized components. We know as much as we want about these little programming data pieces that we're fitting things together. So there's... We don't have to worry about tolerance and that means that in building a large program there's not all that much difference between what I can build and what I can imagine. Because the parts of these abstract entities that I know as much as I want. I know about them as precisely as I'd like. So as opposed to other kinds of engineering where the constraints on what you can build are the constraints of physical systems, the constraints of physics and noise and approximation, the constraints imposed... in building large software systems are the limitations of our own minds."

Rubbish. CS is as real as any other science. Computers are physical entities in this universe like any other, and need to obey the laws of physics too. Thinking that abstraction and complexity has no cost is stupid.

(I enjoy some SICP myself but that's just mental masturbation.)

Name: Anonymous 2013-05-09 10:45

>>6
Computers are physical entities in this universe like any other, and need to obey the laws of physics too. Thinking that abstraction and complexity has no cost is stupid.
B-B-But, think of the purity !

Name: Anonymous 2013-05-09 11:20

>>6
All the codes go in main()!

Nice try, Cudder-sama. It won't be that easy to make me stop liking you.

Name: Anonymous 2013-05-09 11:28

>>6
A computer is a layer of abstraction itself. Circuitry down the hood is enough to cast magic spells and create Touhou games.

Name: Sagist 2013-05-09 16:40

I'd just like to bump all the actual /prog/ threads.

Name: Anonymous 2013-05-09 16:44

ABSTRACT MY ANUS

Name: Anonymous 2013-05-09 17:28

I'd just like to bump all the actual /anus/ threads.

Name: Anonymous 2013-05-09 17:39

>>12
Same here, man.

Name: Anonymous 2013-05-09 22:25

I love Cudder-様.

Name: Anonymous 2013-05-09 22:29

LLLLLLLLEEEEEEEEEEEELLLLLLLLLLLLLLLLLLLLLLLLLLL
>POUNDING A PILLOW WITH A FLESHLIGHT
>OMG MFW LLEELLLLL EGGIINNNNN!!!!!

Name: Anonymous 2013-05-10 23:16

>>6

Indeed. Programming only feels limited by the mind when your mind stays within the boundary of what's feasible.

Name: Anonymous 2013-05-11 0:33

>>16
When I program, I feel limited by hardware and preexisting software quite often.

For instance, when I program in C, I want for a more natural dynamic array type.

Not to mention primitive types greater than 2^64, easy-to-implement concurrent execution of loops, a sane way to provide syntactically-sweet language extensions (C++ classes/operator overloading are NOT sane), the list goes on...

Name: Anonymous 2013-05-11 0:52

>>17
LISP

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-11 7:49

>>9
The cost of the sequential machine abstraction is far less significant than the flexibility it provides... at least for most tasks. But for everything else... have you heard of FPGAs?

>>17
When I program, I feel limited by hardware and preexisting software quite often.
That's why CS is engineering just like any other type of engineering; you have to work within the constraints of the system you're building.
For instance, when I program in C, I want for a more natural dynamic array type.
If you're irritated by that then move to C++ and use std::vector, although then you have not much control over resizing/allocation.

Not to mention primitive types greater than 2^64, easy-to-implement concurrent execution of loops, a sane way to provide syntactically-sweet language extensions (C++ classes/operator overloading are NOT sane), the list goes on...
All those things have additional costs, and making them easier to use encourages overuse where they're absolutely NOT needed. "Let's make all integers arbitrary-precision, then we'll never have to worry about overflow!" >>18 is an example of that. Now your code is a few orders of magnitude larger than it really needs to be, and correspondingly slower. Dynamic arrays are a perfect example of this, and IMHO not having them by default in C makes you think more about whether you actually need them in the first place, possibly making you reconsider and come up with a simpler algorithm (static allocation, in-place streaming) that does not require them.

One of my favourite interview questions is something like this:
"Write a program, in your language of choice, that adds line numbers to a file by prefixing each line with a decimal integer followed by one space. You may assume files have no more than 2^32-1 lines." No hints (you should try this youself) but I can tell you that if you thought of needing an array of some sort, you're not getting the job.

Name: Anonymous 2013-05-11 8:38

>>19
Are you kidding me? That's question requires almost no effort at all.
perl -pe 's/^/$. /'

Name: Anonymous 2013-05-11 8:47

>>20
hahhaha cudderfag doesnt know what a regexp is

although GC is still shit

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-11 8:57

>>21
Wrong, but it's completely overkill for this.

>>20
Sorry, you failed. That code uses an array.

Name: Anonymous 2013-05-11 9:09

>>22
Ok so an iterative method is what you want, correct?
I don't see why anyone would use an array for this, unless they're a very inexperienced programmer.

Name: Anonymous 2013-05-11 9:38

I don't get it. There's got to be a buffer SOMEWHERE to do this, even if it holds only one element/char (a la code]char tmp;[/code]), so how would it be possible without? Or is this what you meant?

Name: Anonymous 2013-05-11 9:40

Also,
BBcode failure

Name: Anonymous 2013-05-11 9:53

>>20 already numbered his files while you fuck around creating an optimized solution.

Name: Anonymous 2013-05-11 10:05

hahahahaha I just saw this and laughed my ass off at OP, then I checked the date and realized I was the one that posted it all those years ago

Name: Anonymous 2013-05-11 10:38

>>22
Where does that use an array? Unless you're counting the implied $_ as a character array, though perl considers it a scalar.

Name: Anonymous 2013-05-11 13:03

>>19

Allow me to rephrase >>18.
TURING COMPLETE MACRO SYSTEM TO PROVIDE ANY ABSTRACTION YOU WISH TO HAVE ON TOP OF YOUR MEMORY INSECURE STATICALLY TYPED STACK BOY SCOPED NOOB LANGUAGE

Using a static array is an optimization over using a dynamic array. It can be performed if the compiler is aware of an upper bound to the length of the array. This should be built in to the language: an array with an upper bound provided by the programmer. The upper bound can then be statically verified (to the fullest extent possible), tested dynamically with assertions, and taken for granted in the release.

In place streaming is one way to evaluate a lazy map/reduce. So you're favorite lazy functional language can do this too, although you typically wont have control over the buffer size.

Name: Anonymous 2013-05-11 15:16

did..did I get the job Cudder-sama?


(define (number in)
 (letrec ((start-line (lambda (in line-number)
           (append (string->list (number->string line-number)) (list #\space) (loop in line-number))))
          (loop (lambda (in line-number)
           (cond ((null? in) '())
                 ((eq? (car in) #\newline) (cons (car in) (start-line (cdr in) (+ line-number 1))))
                 (else (cons (car in) (loop (cdr in) line-number)))))))
  (start-line in 1)))

Name: Anonymous 2013-05-11 18:15

>>30
No.

The most important thing to know about strings in Common Lisp is probably that they are arrays[sub][1][/sup]

1. http://cl-cookbook.sourceforge.net/strings.html

Name: Anonymous 2013-05-11 20:14

>>30
yo! pimped ur sexps

ㅑdefine ㅑnumber inㅕ
 ㅑletrec ㅑㅑstart-line ㅑlambda ㅑin line-numberㅕ
           ㅑappend ㅑstring->list ㅑnumber->string line-numberㅕㅕ ㅑlist #\spaceㅕ ㅑloop in line-numberㅕㅕㅕㅕ
          ㅑloop ㅑlambda ㅑin line-numberㅕ
           ㅑcond ㅑㅑnull? inㅕ 'ㅑㅕㅕ
                 ㅑㅑeq? ㅑcar inㅕ #\newlineㅕ ㅑcons ㅑcar inㅕ ㅑstart-line ㅑcdr inㅕ ㅑ+ line-number 1ㅕㅕㅕㅕ
                 ㅑelse ㅑcons ㅑcar inㅕ ㅑloop ㅑcdr inㅕ line-numberㅕㅕㅕㅕㅕㅕㅕ
  ㅑstart-line in 1ㅕㅕㅕ

Name: Anonymous 2013-05-11 20:35

>>32
Shalom! JEWED your goyish sexps
✡define ✡number in✡
 ✡letrec ✡✡start-line ✡lambda ✡in line-number✡
           ✡append ✡string->list ✡number->string line-number✡✡ ✡list #\space✡ ✡loop in line-number✡✡✡✡
          ✡loop ✡lambda ✡in line-number✡
           ✡cond ✡✡null? in✡ '✡✡✡
                 ✡✡eq? ✡car in✡ #\newline✡ ✡cons ✡car in✡ ✡start-line ✡cdr in✡ ✡+ line-number 1✡✡✡✡
                 ✡else ✡cons ✡car in✡ ✡loop ✡cdr in✡ line-number✡✡✡✡✡✡✡
  ✡start-line in 1✡✡✡

Name: Anonymous 2013-05-12 1:32

>>19
Please employ me to fuck you until one of us die. I want you so much, Cudder氏. Please.

#include <stdio.h>

void print(unsigned int x)
{
    if (x > 9) {
        print(x/10);
    }
    putchar('0' + (x%10));
}

int main()
{
    unsigned int i = 0;
    char c;
    while (EOF != (c = getchar())) {
        print(i++);
        putchar(' ');
        do {
            putchar(c);
            if (c == '\n') break;
        } while (EOF != (c = getchar()));
    }
    return 0;
}

Name: Anonymous 2013-05-12 1:41

>>34
Relying on the integer encodings being sequential 0-based?  Fool!  Don't you know Cudder's company targets machines based on Baudot-Murray?

Name: Anonymous 2013-05-12 2:29

>>35
Fuck you, I am a free man and character encoding is the least of my problems.
Sorry for the delay, I was masturbating to Cudder氏. ;__;

Name: Anonymous 2013-05-12 3:49

>>34
putchargetchar
I/O buffers detected.  You're fired.

Name: Anonymous 2013-05-12 4:05

>>37
so, read/write instead ?
I'm interested in your solutions, also.

Name: Anonymous 2013-05-12 5:03

char c;
while (EOF != (c = getchar())) {

Your fired

Name: Anonymous 2013-05-12 7:14

>>39
THIS IS NOT LISPPPPPPPPPPPPPPP

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.

Name: >>80 2013-05-13 12:43

Of course, assuming the average x86 desktop and an ANSI compiler.

Name: Anonymous 2013-05-13 12:43

it looks like your real problem is files with individual lines stretching beyond 4GiB, or 16EiB
Then it's a retarded problem to begin with, reading a file that size one byte at a time is going to take f o r e v e r.

Name: Anonymous 2013-05-13 13:31

>>82
Let it never be said that I didn't think this problem was completely retarded to begin with. Especially the smarm of `a decimal integer,' and then not even bothering to say that it must be the correct integer. What, am I going to be a smartass and display the line number in hex, then be considered inferior to the guy who just puts `17' for each line? Because that's how the problem works as written.

Name: Anonymous 2013-05-13 13:51

>>83
And does it have to be an ASCII decimal integer?

Name: Anonymous 2013-05-13 14:03

>>80
No, with your program, the final line (number 4294967296 -- you're counting from 1, remember?) would make i overflow and be printed as 0.

Name: Anonymous 2013-05-13 14:16

>>85

That's a trivial fix though - he can just use long instead of int and change that %u to %lu.

Name: Anonymous 2013-05-13 14:19

>>64
Come the fuck on, just what in the fuck do you want?

Name: Anonymous 2013-05-13 14:23

>>87
Maybe if you read the problem, preferably the entire problem, and then think about the problem space, after having read the problem, the problem space will allow you to think (preferably twice) about the answer which is, after all, in the space of the space of answers answering the problem space, which you should think of having read, hmm?  Also, please see the BIG HINTS at >>88 and >>thinkabouttheproblemspace!

Name: Anonymous 2013-05-13 14:31

Is Cudder-様 telling us to prepend the line numbers to a file that we can't even read, write, touch or think about?

Name: Anonymous 2013-05-13 15:03

Well, this is interesting. Not sure where the newline went, something weird from using null I guess. But it didn't blow up.

$ dd bs=1024 count=4194305 if=/dev/zero of=test
4194305+0 records in
4194305+0 records out
4294968320 bytes transferred in 104.261202 secs (41194310 bytes/sec)
$ gecho -e "\nline2\nline3" >>test
$ perl -pe 's/^/$. /' test
1 2 line2
3 line3

Name: Anonymous 2013-05-13 15:54

>>85,86
Read the statement again: "You may assume files have no more than 2^32-1 lines".
The maximum line would have number 2³² - 1 = UINT_MAX, and that IS NOT FSCKING ZERO.

Name: Anonymous 2013-05-13 16:26

OP is a fag

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-14 7:55

I think you'll all just make me facepalm more if I let you continue, so I'll explain it all now.

>>67 was the closest "why".
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.'
Not quite. It'll die as soon as you have a line bigger than available memory. which may be several MB. Everything else you say isn't the main issue here. The main issue with the first few solutions is YOU ARE USING O(N) MEMORY WHEN YOU SHOULD ONLY BE USING O(1). That's why I said it doesn't matter whether you buffer or not. You can read in 64K at atime to buffer, no problem. But the moment you think of reading an "entire line" into memory at a time, you've FAILED.

>>70
>>34 is not correct because of getchar's return type. And it uses recursion and reinvents printf%u unnecessarily. Nothing else.

>>80
This is a pass, maybe overly complicated but if this was written on the first try it's a pass.

>>73
being meguca is suffering

Name: Anonymous 2013-05-14 8:44

Cudder please grace us with your O(1) memory usage C solution.

Name: Anonymous 2013-05-14 8:57

>>93
YHBTL

Name: tdavis 2013-05-14 9:22

>>93
really punish

Name: Anonymous 2013-05-14 9:28

>>93
if you thought of needing an array of some sort, you're not getting the job.
Based on your initial statement, you should be the one without the job. Attempting to process files with lines of the sort of length you're describing without using an array to handle the data in reasonably sized chunks is ludicrous.

Name: Anonymous 2013-05-14 9:31

>>93
which may be several MB
What is swapping?

Name: Anonymous 2013-05-14 10:36

>>100
nice dubs gro

Name: Anonymous 2013-05-14 10:55

>>99
Thanks, you too.

Name: DALMATIANS 2013-05-14 11:03

DALMATIANS

Name: Anonymous 2013-05-14 11:38

>>101
nice dogs bru

Name: Anonymous 2013-05-14 12:47

woof woof bark auuuuuu LeChuck arf arf arf

Name: Anonymous 2013-05-14 16:27

>>82-84
Yes, I have noticed that, but assumed the obvious.

Not forever, but RUBY AS FUCK I agree. So, taste this:

#include <stdio.h>

int main()
{
    char buf[4096];
    int line = 1;
    while(!feof(stdin)) {
        size_t i = 0;
        size_t j = 0;
        size_t n = fread(buf, 1, sizeof(buf), stdin);
        while (i < n) {
            if (j < n) fprintf(stdout, "%u ", line++);
            do {
                j++;
            } while ((j < n) && (buf[j-1] != '\n'));
            fwrite(&buf[i], 1, j-i, stdout);
            i = j;
        }
    }
    return 0;
}


:3

Name: >>104 2013-05-14 16:30

There is a bug, but that's okay! ^_^

Name: Anonymous 2013-05-14 21:04

Name: Anonymous 2013-05-14 21:54

>>1
You say ``fuck'' too much.

Name: Anonymous 2013-05-14 23:24

>>96
heheh, golden calf

Name: Anonymous 2013-05-14 23:34

le moses game

Name: Anonymous 2013-05-15 0:00

rule 110 get.

Name: 111 2013-05-15 0:13

Rollin for trips. If I gets, OP has to kill himself.

Name: Anonymous 2013-05-15 7:37

>>111
Fuck off, spammer.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-15 7:37

>>94
Something like this.

>>97
stdio includes buffering.

>>98
A "safety vent" to prevent hard crashes. You should never be running a system at that load level, because it'll be unacceptably slow. Some of the systems I work on don't even have secondary storage.

>>104
getchar and putchar will do buffering for you automatically, and they're inlined because they're macros in any good C library, so you're introducing additional overhead by buffering twice. MSVCRT's default buffer is 4K, glibc is also 4K.

>>106
This *might* be faster but you should then turn OFF stdio buffering. Either way I'm not interested in absolute speed with this question, and using 1000x the memory for <1000x speed increase is inefficient (see also: 10KB --- yes, ten thousand bytes --- implementations of memcpy() that achieve maybe a few % speedup in careful benchmarks over rep movsd and probably much less on Nehalem and Sandy Bridge).

Name: Anonymous 2013-05-15 7:47

>>113
stdio includes buffering.
And using it means violating the criteria given for this problem, resulting in horribly inefficient execution.

Name: Anonymous 2013-05-15 15:37

Don't you need O(n) memory for the case when you only have an array of '\n'.

Name: Anonymous 2013-05-15 16:27

>>115
No.  Why would you? '\n' is just a character.  It causes no more memory to be required than if it were 'a'.

Name: Anonymous 2013-05-15 17:13

>>116
Overwriting characters with the line numbers while updating the file.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-16 2:21

>>114
No it doesn't.

>>117
In case you haven't noticed, the problem was about writing a filter, not an in-place algorithm.

Name: Anonymous 2013-05-16 7:45

>>118
So a buffer isn't an array?

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-16 7:47

>>119
It's a fixed-size one.

Name: Anonymous 2013-05-16 9:03

>>120
Yes, and you originally said to not even think of using an array, while I maintain that if you don't think of using an array then you aren't approaching the problem correctly.

Name: Anonymous 2013-05-16 12:06

>>118
Well I completely missed it and now the problem doesn't seem as bad.

Name: Anonymous 2013-05-17 2:00

Well, since you don't like >>30, how about this?


(defpackage #:cudders-secret-box-of-treats
  (:use #:cl)
  (:export #:lines)
  (:shadow car cdr caar cadr cdar cddr))


(in-package #:cudders-secret-box-of-treats)


(defclass promise ()
  ((cached-value :initarg :value)
   (calculator :initarg :calculator)
   (evaluated :initform nil)))


(defmethod force ((self promise))
  (with-slots (cached-value calculator evaluated) self
    (if evaluated
      cached-value
      (progn (setf cached-value (funcall calculator))
             (setf evaluated t)
             cached-value))))

(defun make-promise (calculator)
  (make-instance 'promise :calculator calculator))

(defmacro promise (&rest body)
  `(make-promise (lambda () ,@body)))





(defmethod car ((self list))
  (cl:car self))

(defmethod cdr ((self list))
  (cl:cdr self))

;; need to redefine these so they use the new generic car and cdr.
(defun caar (list)
  (car (car list)))

(defun cadr (list)
  (car (cdr list)))

(defun cdar (list)
  (cdr (car list)))

(defun cddr (list)
  (cdr (cdr list)))



(defclass stream-cons ()
  ((car :initarg :car)
   (cdr :initarg :cdr)))

(defun stream-cons-f (car cdr)
  (make-instance 'stream-cons :car car :cdr cdr))

(defmacro stream-cons (car cdr)
  `(stream-cons-f ,car (promise ,cdr)))

(defmethod car ((self stream-cons))
  (with-slots (car) self
    car))

(defmethod cdr ((self stream-cons))
  (with-slots (cdr) self
    (force cdr)))

(defun stream-append (&rest lists)
  (cond ((null lists) nil)
        ((null (car lists)) (apply #'stream-append (cdr lists)))
        (t (stream-cons (caar lists) (apply #'stream-append (cons (cdar lists) (cdr lists)))))))

(defun for-each (fn list)
  (if (null list)
    nil
    (progn (funcall fn (car list))
           (for-each fn (cdr list)))))


(defun number-lines-stream-list (list)
  (labels ((start-line (line-number list)
             (stream-append (coerce (write-to-string line-number) 'list)
                            (stream-cons #\space
                                         (non-start-line line-number list))))
           (non-start-line (line-number list)
             (cond ((null list) nil)
                   ((eq (car list) #\newline) (stream-cons #\newline (start-line (1+ line-number) (cdr list))))
                   (t (stream-cons (car list) (non-start-line line-number (cdr list)))))))
    (start-line 1 list)))

(defun stream->char-stream-cons (stream)
  (let ((c (read-char stream nil 'eof)))
    (if (eq c 'eof)
      nil
      (stream-cons c (stream->char-stream-cons stream)))))




(defun main (file-name)
  (with-open-file (input file-name)
    (for-each #'write-char (number-lines-stream-list (stream->char-stream-cons input)))))

(main "data.tt")

Name: >>123 2013-05-17 2:03

ignore that export. also 123 get

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-17 3:01

>>121
You do not need to use an array to solve this problem, thus you should not think of using one.

>>123
Congratulations, you have managed to write a Lisp solution that takes more lines than the C one. FAIL.

Name: Anonymous 2013-05-17 3:18

>>125
FAIL

>LELLELELELEEEEEEEEEEEEEEEEEEELL
>LE FAIL FOR DA E/G/IN /G/IN /G/RO
>LELELLE LE EGIN  FAIL MEME
>LELELEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE GRO

Name: Anonymous 2013-05-17 4:12

>>121
Don't think, feel, and you will be tanasinn.

Name: Anonymous 2013-05-17 4:18

>>127
[aa]
      ____
     /∵∴∵∴\
    /∵∴/∴∵\\
   /∵∴●∴∴.● |   
   |∵∵∵/ ●\∵|    only yahew can great tanasinn
   |∵∵ /三 | 三| |  
   |∵∵ |\_|_/| | 
    \∵ | \__ノ .|/ 
   / \|___/\
[/a]

Name: Anonymous 2013-05-17 4:53

Name: Anonymous 2013-05-17 5:22

>>129
What the fuck are you doing? Go back to whatever shit board you came from. Your not wanted here goyim. I knew Judas should of killed Jesus earlier, so Christian scum like this one could never be born.

Name: Anonymous 2013-05-17 7:08

>>125
Bullshit. Good I/O uses buffers, therefore if I think about using good I/O I think about using arrays. If you want to say "Don't use realloc or critical section malloc" or whatever, say it. Don't pretend arrays are something they aren't.

Name: Anonymous 2013-05-17 10:57

>>130
You can go with him; you're not welcome here either.

Name: Anonymous 2013-05-17 15:50

>>113
MSVCRT's default buffer is 4K
not my msvcrt!

Name: Anonymous 2013-05-17 16:04

None of you are fucking welcome here because you keep posting bad programming that keeps making the e/a/in /a/roski imagefaggot post here.
Fucking stop.

Name: Anonymous 2013-05-17 16:44

>>134
LLLLLLLLLLEEEEEEEEEEEEELLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
>LE EGIN BUTTHURT FACE

Name: Anonymous 2013-05-17 20:11

Yes, let the lel flow through you.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-19 7:53

>>131
Buffers of fixed-length, buffers that you shouldn't need to think about.

Name: Anonymous 2013-05-19 14:01

>>137
Bullshit. Now you're trying to claim that people don't think about buffers when writing I/O.  Just admit that your 'no thinking about arrays' statement was poorly worded.

Name: Anonymous 2013-05-19 17:52

>>134
Who's the e/a/in /a/roski here? I only see lel-cunt, Cudder, and that guy who's in love with Cudder.

Name: Anonymous 2013-05-19 22:12

#include <stdio.h>

int main()
{
    unsigned char s;
    char c;
    unsigned long n;

    s = 1;
    n = 1;

    while ((c = getchar()) != EOF) {
        if (s)
            printf("%lu\t", n++);

        putchar(c);

        s = (c == '\n');
    }

    return fflush(stdout) || ferror(stdin);
}

Name: Anonymous 2013-05-19 22:24

>>139
der shudderspace is the e/a/in /a/roski, if you don't know that then you've been here less than two weeks.

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