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

/prog/ challenge: ``proper quotes''

Name: Anonymous 2008-08-12 20:08

Write a program that takes a text file and converts adirectional quotes into `` and '', i.e.

INPUT:
I turned my head around, with that casual smile all over my face.
"The stuff you said in your intro, how much of it was serious?"
With her arms crossed in front of her chest, lips sealed tight,
Suzumiya Haruhi kept her stern posture, staring right into my eyes.
"What 'stuff in my intro?'"
"The stuff about the aliens and all that."
"Are you an alien?"


OUTPUT:
I turned my head around, with that casual smile all over my face.
``The stuff you said in your intro, how much of it was serious?''
With her arms crossed in front of her chest, lips sealed tight,
Suzumiya Haruhi kept her stern posture, staring right into my eyes.
``What 'stuff in my intro?'''
``The stuff about the aliens and all that.''
``Are you an alien?''


Use whatever language you want. Bonus points for creativity and efficiency.

I'll start:
#include <stdio.h>

char *q[]= {"''", "``"};
main() {
 int c, i=0;
 while((c=getchar())!=EOF) {
  if(c=='"')
   printf("%s",q[i^=1]);
  else
   putchar(c);
 }
}

Name: Anonymous 2008-08-12 20:25

State var = 1. Scan the file. substitute " with `` and change state var to 1 if state var == 0; substitute " with '' and change state var to 0 if state var == 1.

What I did here is a pseudocode, one level higher.

Name: Anonymous 2008-08-12 20:29

%%%%%%%%%
%%%%%%%%%%%%%%%%"
%%%%%%``
%%%%%%%''
%%%%
%

%
%%%
%%%%
%%%%%

Name: Anonymous 2008-08-12 20:31

my $c = 0;
sub t {
  ($c++%2)?'``':"''"
}
while(<>){
  s/"/&t()/g;
  print $_;
}

Name: Anonymous 2008-08-12 20:33

      ******************************************************************

      *                                                                *

      *        C A L C U L A T E   D A T E   D A Y   N U M B E R       *

      *                                                                *

      *                                                                *

      *    RETURNS A NUMBER WHICH IS ONE GREATER FOR EACH SUCCESSIVE   *

      *    DATE.                                                       *

      *                                                                *

      *    USAGE:  MOVE <FIRST DATE>  TO DW-WORK-YYYYMMDD.             *

      *            PERFORM 001100-DATE-DAYS                            *

      *               THRU 001100-EXIT.                                *

      *                                                                *

      *    RESULT: DW-DAYS = DATE DAY NUMBER                           *

      *                                                                *

      *                                                                *

      *    THIS ROUTINE USES A VARIATION OF ZELLER'S CONGRUENCE.       *

      *    THE FORMULA IS:                                             *

      *                                                                *

      *        <DATE DAY NBR> = (     (YEAR * 365)                     *

      *                          + INT(YEAR / 4)                       *

      *                          - INT(YEAR / 100)                     *

      *                          + INT(YEAR / 400)                     *

      *                          + INT(MONTH * 30.6001)                *

      *                          +     DAY) )                          *

      *                                                                *

      *    WHERE: DAY     = DAY OF THE MONTH                           *

      *                                                                *

      *           MONTH   = MONTH + 13  (JAN & FEB)                    *

      *                     MONTH + 1   (MAR - DEC)                    *

      *                                                                *

      *           YEAR    = YEAR - 1    (JAN & FEB)                    *

      *                     YEAR        (MAR - DEC)                    *

      *                                                                *

      *           INT(...) MEANS TAKE THE INTEGER PART (NO ROUNDING)   *

      *                                                                *

      *                                                                *

      *    THE DATE-DAY-NUMBER CAN BE USED TO DETERMINE THE NUMBER OF  *

      *    DAYS BETWEEN TWO DATES BY COMPUTING THE DAY NUMBER OF EACH  *

      *    DATE AND SUBTRACTING, LIKE THIS:                            *

      *                                                                *

      *        MOVE <FIRST DATE>  TO DW-WORK-YYYYMMDD.                 *

      *        PERFORM 001100-DATE-DAYS                                *

      *           THRU 001100-EXIT.                                    *

      *        MOVE DW-DAYS TO <HOLD DAY>.                             *

      *        MOVE <SECOND DATE> TO DW-WORK-YYYYMMDD.                 *

      *        PERFORM 001100-DATE-DAYS                                *

      *           THRU 001100-EXIT.                                    *

      *        SUBTRACT DW-DAYS FROM <HOLD DAY>                        *

      *            GIVING <DAYS BETWEEN DATES>.                        *

      *                                                                *

      ******************************************************************

      *

       001100-DATE-DAYS.

      *

      *  ** ADJUST YEAR AND MONTH **

      *

           MOVE DW-WORK-YYYY TO DW-TEMP-YYYY.

           MOVE DW-WORK-MM   TO DW-TEMP-MM.

           IF (DW-WORK-MM < 03)

               ADD 13 TO DW-TEMP-MM

               SUBTRACT 1 FROM DW-TEMP-YYYY

           ELSE

               ADD 1 TO DW-TEMP-MM.

      *

           MULTIPLY DW-TEMP-YYYY BY 365 GIVING DW-DAYS.

      *

           DIVIDE DW-TEMP-YYYY BY 4 GIVING DW-WORK1.

           ADD DW-WORK1 TO DW-DAYS.

      *

           DIVIDE DW-TEMP-YYYY BY 100 GIVING DW-WORK1.

           SUBTRACT DW-WORK1 FROM DW-DAYS.

      *

           DIVIDE DW-TEMP-YYYY BY 400 GIVING DW-WORK1.

           ADD DW-WORK1 TO DW-DAYS.

      *

           MULTIPLY DW-TEMP-MM BY 30.6001 GIVING DW-WORK1.

           ADD DW-WORK1 TO DW-DAYS.

      *

           ADD DW-WORK-DD TO DW-DAYS.

      *

       001100-EXIT.

           EXIT.

      *

Name: Anonymous 2008-08-12 20:34

Unicode has both kinds of quotes and ascii defines ' as being down so it is ambiguous.

Name: Anonymous 2008-08-12 20:46

#!/usr/bin/perl
$/ = undef;
$_ = <>;
s/"(.*)"/``$1''/go;
print;

Name: Anonymous 2008-08-12 20:52

>>7
#!/usr/bin/perl
$/ = undef;
$_ = <>;
s/"(.*)"/``$1''/sgo;
print;

Name: Anonymous 2008-08-12 21:00

>>7
I'm not so familiar with the FORCED OBFUSCATION OF CODE, but shouldn’t that be s/"(.*?)"/``$1''/go;?

Name: Anonymous 2008-08-12 21:12

:%s/"\(\(.\|\n\)\{-\}\)"/``\1''/g

Name: Anonymous 2008-08-12 21:43

>>9
.* is "any character, repeated zero or more times"

.*? is "any character, repeated zero or more times, zero or one time" which would be redundant.

Maybe I'm wrong, but that's what I remember... last time I used complex regex like this was 2-3 years ago.

Name: Anonymous 2008-08-12 21:57

main =
   interact begin
   where
      begin ('"': text) = "``" ++ end text
      begin (c: text) = c: begin text

      end ('"': text) = "''" ++ begin text
      end (c: text) = c: end text

Name: Anonymous 2008-08-12 22:37

>>11
lrn2perl

Name: Anonymous 2008-08-12 23:10

>>11

Non greedy quantifiers, motherfucker --- do you speak it?

Name: Anonymous 2008-08-12 23:59

I can make it shorter with my magic functional perl! (look at that fabulous map subroutine)

#!/usr/bin/perl
print map{s/"(.*?)"/``$1''/g;$_} <>;


I wish we had perl6 with it lazy <== operator so that my program would actually be better than >>8. It would be something like this: print <== map{s/"(.*?)"/``$1''/g;$_} <== <>, and print, map, and <> will be executed in parallel.

Name: Anonymous 2008-08-13 4:09

>>15
doesn't work for quotes that begin on one line and end on another.
#!/usr/bin/perl
$/ = undef;
map{s/"(.*?)"/``$1''/sgo;print}<>

Name: Anonymous 2008-08-13 4:19

>>16
Oh, that is correct.
Still I'd prefer join "",<> insted of $/ = undef;;
It uses less special variables and if you use $/ without local in subroutine it may fuck up the rest of your program.

Name: Anonymous 2008-08-13 6:18

>>12
You forgot the empty list case. Also, I can't figure out how to fix the error that ghc complains about.

Name: Anonymous 2008-08-13 6:53


<?php

PREG

MATCH

FUCKING

ALL

?>

Name: Anonymous 2008-08-13 7:50

>>19
preg is /prog/'s evil cousin

Name: Anonymous 2008-08-13 10:41

>>18

main =
   interact $ fixQuotes "``" "''"

fixQuotes open close = fixer
   where
      fixer ('"': text) = open ++ (fixQuotes close open) text
      fixer (c: text) = c: fixer text
      fixer [] = []

Name: Anonymous 2008-08-13 11:44

>>21
Another Haskell version, just for the hell of it.


properQuotes str = properQuotes' str False
  where
    properQuotes' []       _     = []
    properQuotes' ('"':cs) False = "``" ++ properQuotes' cs True
    properQuotes' ('"':cs) True  = "''" ++ properQuotes' cs False
    properQuotes' (c:cs)   quote = c : properQuotes' cs quote

main = interact $ properQuotes

Name: Anonymous 2008-08-13 12:02

main = interact $ fixQuotes "``" "''"
   where
      fixQuotes _ _ [] = []
      fixQuotes open close ('"': text) = open ++ fixQuotes close open text
      fixQuotes open close (c: text) = c: fixQuotes open close text

Name: Anonymous 2008-08-13 12:53

>>22,23
NON-SCALABLE TURK SOLUTION

Name: Anonymous 2008-08-13 14:00

Microsoft Word

Name: Anonymous 2008-08-13 14:54

>>24
Huh?

Name: HMA MEME FAN 2008-08-13 15:46

Now with file parsing, more side-effects, and helpful errors!

#include <iostream>
#include <fstream>
using namespace std;

int main( int argc, const char* argv[] )
{
    if( argc != 3) {
        cout << "Call like this: parser input.txt output.txt";
        cin.get();
        return -1;
    }

    // Ready for parsing
    ifstream input;
    input.open( argv[1] );
    ofstream output;
    output.open( argv[2] );
    char current;
    bool insideAQuote = false;

    if( !input.is_open() ) {
        cout << "Could not read input file (have you read your SICP today?)";
        cin.get();
        return -1;
    }

    while( !input.eof() && input.get(current) )
    {
        if( current == '"' ) {
            if( !insideAQuote) {
                output << "``";
                insideAQuote = true;
            }
            else {
                output << "''";
                insideAQuote = false;
            }
        }
        else
            output << current;
    }
    return 0;
}

Name: HMA MEME FAN 2008-08-13 15:59

(On second thought, I think the !input.eof() is superfluous)

Name: Anonymous 2008-08-13 16:01

Shut up, namefag.

Name: Anonymous 2008-08-13 16:10

>>29
Hax my anus, faggot.

Name: Anonymous 2008-08-13 16:21

>>30
A namefag is a namefag, dumbfag.

Name: Anonymous 2008-08-13 16:24

>>31's  a n u s  f e e l s  k i n d  o f  h a x e d  n o w :(

Name: Anonymous 2008-08-13 16:33

>>32
I could say the same about you.

Name: Anonymous 2008-08-13 16:46

(define (quote-filter)
  (define (helper start-char rchar)
    (if (eof-object? rchar) #t
        (begin
          (if (char=? \#" rchar)
              (display (if (start-char) "``" "''"))
              (display rchar))
          (helper (not start-char) (read-char)))))
  (helper #t (read-char)))
(quote-filter)

Name: Anonymous 2008-08-13 17:24

#include <stdio.h>

int main(void) {
        int c, i = 0;
        while((c = getchar()) !=EOF) {
                if(c=='"')
                        printf("%c%c", c = "'`"[i ^= 1], c);
                else
                        putchar(c);
        }
        return 0;
}


De-faggotized >>1.

Name: Anonymous 2008-08-13 18:32

>>35
printf("%c%c", c = "'`"[i ^= 1], c);
The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

Enjoy your `"idiot quotes'".

Name: Anonymous 2008-08-13 19:00

map{s/"(.*?)"/``$1''/sgo{print}join$/,<>

Name: Anonymous 2008-08-13 21:07

Right, so we have the "state machine", the "regexp", and the "retarded Haskell LOL FUNCTOINAL!!!1". That should be everything, thread over. Right?

Name: Anonymous 2008-08-13 22:29

>>34
About time that showed up

Also, OMG OPTIMIZED version of >>1
#include <stdio.h>
main() {
 int c, i='\'';
 while((c=getchar())!=EOF) {
  if(c=='"')
   putchar(i^='G'),putchar(i);
  else
   putchar(c);
 }
}


>>38
No, we're missing Brainfuck.

Name: Anonymous 2008-08-13 22:36

fix_quotes(_,_,[],[]).

fix_quotes(Open, Close, [34|Text], ReFixed) :-
   fix_quotes(Close, Open, Text, Fixed),
   append(Open, Fixed, ReFixed), !.
  
fix_quotes(Open, Close, [C|Text], [C|Fixed]) :-
   fix_quotes(Open, Close, Text, Fixed).

get_text([C|Text]) :- get_code(C), C =\= -1, !, get_text(Text).
get_text([]).

put_text([C|Text]) :- put_code(C), put_text(Text).
put_text([]).

main :- get_text(Text), fix_quotes("``", "''", Text, Fixed),
    put_text(Fixed).

initialization(main).

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