Thread: need a Brilliant C Programmer to help me solve that Enigma

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    2

    Question need a Brilliant C Programmer to help me solve that Enigma

    * GUIDELINES:
    * - Look at main(): it calls various functions.
    * - You are asked to implement two functions: str_cpy() and str_cat(). No need
    * to implement str_printf() and str_free()
    * - Reading main() carefully will allow to understand str_cpy() and str_cat()
    * signature and usage.
    * - The code you write needs to be "library quality"; as good as you would
    * expect a good libc to implement such functions.
    * - At the top of the page, you see 4 includes - indicating the functions that
    * can be used to implement str_cpy() and str_cat().
    *
    * FYI: it is possible to implement str_cpy() and str_cat() efficiently in no
    * more than 7 lines of code per function, and in less than 5 minutes.
    *
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>
    
    int main(int argc, char *argv[])
    {
        char *s = NULL;
        str_cpy(&s, "Hola Hola");
        str_cpy(&s, s+5);
        str_cat(&s, " Mundo");
        str_printf(&s, "%s!", s);
        puts(s); /* result: "Hola Mundo!" */
        str_free(&s);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Ha Ha Ha.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Source: hola! Coding Challenge

    Help me try to cheat my way into a job!
    Last edited by rags_to_riches; 08-08-2012 at 07:20 AM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The problem is that you do not know what str_cpy and str_cat has to do or you have figured out what their goal is,but you can not implement them?

    P.S. you don't need a Brilliant C Programmer to help me solve that <<Enigma>>

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by rags_to_riches View Post
    Source: hola! Coding Challenge

    Help me try to cheat my way into a job!
    Looks like we have a Detective here.Thank you Mr.Monk

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    2
    i don't know how to implement it to be perfect function, to cover all cases

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Show some attempted code if you're looking for guidance.
    Or post in the "job recruitment" forum if you're looking for a "brilliant" C programmer to do the work for you (hint: a "brilliant" programmer likely won't write your code for you for free).

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Ha Ha Ha.

    Tim S.
    You mean this is a serious post; I still think it is a joke.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > * more than 7 lines of code per function, and in less than 5 minutes.
    Does the maxim "fast, cheap, good - pick any two" apply here, when you've already chosen "fast"?

    > * - The code you write needs to be "library quality"; as good as you would
    > * expect a good libc to implement such functions.
    Where is it documented whether these function return a status, or void ?

    The example main() is far too crappy to make any judgement call on this. Either there are no errors allowed, or it just ignores the possibility.
    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
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I'm with Salem.

    This isn't a joke. It's just stupid.

    To make this work requires making dangerous guesses, using pointless global state, or implementing awful hacks where I'd dismiss anyone with the gall of claiming it was "library quality" when the implied concept is trivial to do safely without any of that mess.

    [Edit]
    Gah!

    I'm so dumb!

    The obvious "challenge" here is spotting the stupidity of the thing.

    I got it the second time around... where is my 1000?
    [/Edit]

    Soma
    Last edited by phantomotap; 08-08-2012 at 08:50 AM.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by cdevdev View Post
    * GUIDELINES:
    * - Look at main(): it calls various functions.
    * - You are asked to implement two functions: str_cpy() and str_cat(). No need
    * to implement str_printf() and str_free()
    * - Reading main() carefully will allow to understand str_cpy() and str_cat()
    * signature and usage.
    * - The code you write needs to be "library quality"; as good as you would
    * expect a good libc to implement such functions.
    * - At the top of the page, you see 4 includes - indicating the functions that
    * can be used to implement str_cpy() and str_cat().
    *
    * FYI: it is possible to implement str_cpy() and str_cat() efficiently in no
    * more than 7 lines of code per function, and in less than 5 minutes.
    *
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>
    
    int main(int argc, char *argv[])
    {
        char *s = NULL;
        str_cpy(&s, "Hola Hola");
        str_cpy(&s, s+5);
        str_cat(&s, " Mundo");
        str_printf(&s, "%s!", s);
        puts(s); /* result: "Hola Mundo!" */
        str_free(&s);
        return 0;
    }
    A stupid question deserves a stupid solution:

    Make str_cpy and str_cat do absolutely nothing and just return 0. (that's two lines of code)
    Make str_printf malloc memory for s and initialize it to Hola Mundo. (that's another 2 lines of code)
    Make str_free free s. (that's 1 line of code) Hell, you could even forget about it and leak the f*** hoping it will blow up in the interviewer's/grader's face.

    It's efficient it's 5 lines of code in TOTAL and it takes about 60 seconds to write. It also shows what a total dumb challenge this is to begin with.

    EDIT: Forgot to add it's also "library quality" as far as the person asking you to do this is concerned.
    Last edited by claudiu; 08-09-2012 at 04:40 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    I studied main carefully, as the directions say. This should pass all the test cases provided.

    Code:
    /* The spec says that the result of this function is never used, so it's a NOP */
    void str_cpy(char **s, const char *dummy)
    {
    }
    
    /* The spec says that this function should allocate space for and copy the string
     * "Hola Mundo" in s. */
    void str_cat(char **s, const char *dummy)
    {
      *s = strdup("Hola Mundo");
    }
    What do I win?
    Last edited by KCfromNC; 08-09-2012 at 07:31 AM.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by KCfromNC View Post
    What do I win?
    The tile of the <<Brilliant C Programmer>>

    (as stated in 1st post )

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by std10093 View Post
    The tile of the <<Brilliant C Programmer>>

    (as stated in 1st post )
    How many cereal box tops do I need to send in to get this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help solve this enigma, please
    By falcon758 in forum C Programming
    Replies: 25
    Last Post: 09-24-2011, 02:18 AM
  2. For my next brilliant question...
    By phosphorousgobu in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 02:49 PM
  3. brilliant code
    By Waldo2k2 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-26-2002, 10:32 AM
  4. Brilliant discoveries
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-16-2002, 04:33 AM
  5. Another do-while enigma..for me
    By bugeye in forum C Programming
    Replies: 9
    Last Post: 01-26-2002, 09:24 AM