Thread: C++ GOLF (unjustified words)

  1. #1
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704

    C++ GOLF (unjustified words)

    First was a PERL golf, then a PHP golf (when I found and partook in it). Since this section seems to be dead and going no where I'll host a game of golf.

    Remember you need to be able to explain your solution to others so dont waste coding time googling for the perl solutions.

    ALL ENTRIES MUST BE IN BY MONDAY MAY/31/2004 12:pm central time
    PM me your answer function.

    Challenge
    There are two columns of words. Left-justify the left column, and right-justify the right column, then squeeze as many spaces as possible out from between them while keeping at least one space on every line and keeping the justifications.

    So with the function input:
    Code:
    waf zrsea
    abdef sedrgf
    a warfhjik
    Your function should return:
    Code:
    waf    zrsea
    abdef sedrgf
    a   warfhjik

    Challenge Rules
    Input will be in a single string, no more than 10 lines, with two words separated by one space. Maximum word length is 10. Output should be the same but with "(space)+" instead of "(space)".

    Challenge Specific Requirements
    1) Function will take one parameter, a string
    2) Function will return a string


    C++ Golf Rules
    It's pretty simple if you haven't played code golf before; shortest solution in characters that solves the challenge and meets the challenge rules wins.

    1. Solutions in C/C++ only. Free to use classes, but it will likely increase your code. ++ has been added simply for header librarys
    2. You can only use the standard C/C++ headers
    3. Because C++ often has one shortest solution, only post your golf score until the round is declared over.
    2. Your solution must be contained in a single function and your solution must be contained within the scope of the function (i.e. defining vars outside your function and using 'global a,b,c;' would not be ok.)
    3. You golf score includes the function definition and all spaces. i.e.
    #234567890123456
    string j(string s) {} # golf score of 15


    HAVE FUN - Even if you dont think you'll win have a go and see how you shape up.
    Last edited by dbgt goten; 05-19-2004 at 06:53 PM. Reason: time constraint
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So in C, you would have
    Code:
    char *input = "waf zrsea\n"
                  "abdef sedrgf\n"
                  "a warfhjik\n";
    That is, one string containing pairs of words separated by a space, and delimited with a newline?
    Is there always a newline at the end of the last line?

    > 3. Because C++ often has one shortest solution, only post your golf score until the round is declared over.
    If someone posts a better score, can you still work on the problem up to the deadline to see if you can still beat your previous best?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Quote Originally Posted by Salem
    So in C, you would have
    Code:
    char *input = "waf zrsea\n"
                  "abdef sedrgf\n"
                  "a warfhjik\n";
    That is, one string containing pairs of words separated by a space, and delimited with a newline?
    Is there always a newline at the end of the last line?
    No. There is not a /n at the end of the string, just the middle.
    > 3. Because C++ often has one shortest solution, only post your golf score until the round is declared over.
    If someone posts a better score, can you still work on the problem up to the deadline to see if you can still beat your previous best?
    Yes, until the deadline you may revise your solution until you are happy.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Hmpf, not really a proper line is it then, the last one?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    tis the rules.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1) Function will take one parameter, a string
    2) Function will return a string
    And what are the acceptable definitions of string in C?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    So the idea is to create a source file to do this with the smallest footprint?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    When is the deadline?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Deadline was
    > ALL ENTRIES MUST BE IN BY MONDAY MAY/31/2004 12m central time
    I submitted a score of 249, but never heard of anything as the deadline past...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    oh, sorry. I was scanning this for deadlines, and I didn't see results, so I figured it was still going. I guess I didn't see them because they were in big uppercase letters right in front of my face

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up :)

    This looks like a really cool contest.... when are you guys going to do this again??!

    I'd really like to get in on the next round of c++ golf..
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well, here's my entry I submitted long ago..
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void g(char*i,char*o){
    int m=0,l=0,x;char *p=i,*q;
    while(x=strcspn(p,"\n")){m=x>m?x:m;p+=x+(p[x]!=0);l++;}
    memset(o,' ',l*++m);o[l*m]='\0';p=i;
    while(q=strchr(p,' ')){x=strcspn(q,"\n");
    strncpy(o,p,q-p);strncpy(o+m-x,q+1,x);o+=m;p=q+x+(p[x]!=0);}
    }
    
    
    int main ( ) {
        char *i1 = "WAF zrsea\n"
                   "ABDEF sedrgf\n"
                   "A warfhjik\n";
        char *i2 = "waf zrsea\n"
                   "a z\n"
                   "aaaaaaaaa a\n"
                   "z zzzzzzzzz\n"
                   "a warfhjik";
        char buff[1000] = { 0 };
        g(i1,buff);
        printf( "%s--\n%s", i1, buff );
        printf("====\n");
        g(i2,buff);
        printf( "%s\n--\n%s\n", i2, buff );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM