Announcement

Collapse
No announcement yet.

Thought this was interesting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Thought this was interesting

    Some of you old coders may already know about this....

    I was browsing through some tutorials for 'C' and I came across this:

    #include <stdio.h>

    main(t,_,a)
    char *a;
    {return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
    main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a
    )&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,
    t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
    ,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
    +k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){n\
    l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
    n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
    ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
    #'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
    :t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
    +1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
    i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}



    Well, I was gonna let you all compile it yourself but thought I'd just better post the result. so here it is. If you don't believe me, compile it yourself

    On the first day of Christmas my true love gave to me
    a partridge in a pear tree.

    On the second day of Christmas my true love gave to me
    two turtle doves
    and a partridge in a pear tree.

    On the third day of Christmas my true love gave to me
    three french hens, two turtle doves
    and a partridge in a pear tree.

    On the fourth day of Christmas my true love gave to me
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the fifth day of Christmas my true love gave to me
    five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the sixth day of Christmas my true love gave to me
    six geese a-laying, five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the seventh day of Christmas my true love gave to me
    seven swans a-swimming,
    six geese a-laying, five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the eighth day of Christmas my true love gave to me
    eight maids a-milking, seven swans a-swimming,
    six geese a-laying, five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the ninth day of Christmas my true love gave to me
    nine ladies dancing, eight maids a-milking, seven swans a-swimming,
    six geese a-laying, five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the tenth day of Christmas my true love gave to me
    ten lords a-leaping,
    nine ladies dancing, eight maids a-milking, seven swans a-swimming,
    six geese a-laying, five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the eleventh day of Christmas my true love gave to me
    eleven pipers piping, ten lords a-leaping,
    nine ladies dancing, eight maids a-milking, seven swans a-swimming,
    six geese a-laying, five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.

    On the twelfth day of Christmas my true love gave to me
    twelve drummers drumming, eleven pipers piping, ten lords a-leaping,
    nine ladies dancing, eight maids a-milking, seven swans a-swimming,
    six geese a-laying, five gold rings;
    four calling birds, three french hens, two turtle doves
    and a partridge in a pear tree.


    Dave
    Ladies and gentlemen, take my advice, pull down your pants and slide on the ice.

  • #2
    Um, no major C training here (though I've done a little), but exactly how is it going from that complex but short prog to the 12 days of christmas? My first guess, just glancing at the code, is by using functions to pick characters from the ASCII set?
    "..so much for subtlety.."

    System specs:
    Gainward Ti4600
    AMD Athlon XP2100+ (o.c. to 1845MHz)

    Comment


    • #3
      Let me guess... it's basicly just the last paragraph compressed. When ran it starts at the first segment and adds one at a time till it's finished.

      Comment


      • #4
        I simply uses a recursive function like the one used in the halo game (not the game named HALO on the X-BOX). It uses the ASCII table to display the caracter from the numerical value. The function recalls itself until the condition has been meet and then it closes. It the same process that we use to find something in a tree (binary or not). If u want other example of that do a search on binary trees.

        IMHO recursive function should be avoided at all cost.... they pump too much ram and there's always a risk that it won't run at all if it has not enough ram or that it won't ever shut down due to a programming error which will cause the function to eat all the system ressources.

        Spazm
        Last edited by spazm_1999; 14 August 2001, 17:35.
        P3-667@810 retail, Asus CUSL2-C, 2*128 mb PC-133(generic), G400DH 16mb, SBLive value, HollyWood+, 1*Realtek 8029(AS) and 1*Realtek 8039C, Quantum 30g, Pioneer DVD-115f

        Comment


        • #5
          I love this program!
          It won "Least Likely to Compile" award in the 1988 Obfuscated C contest.
          See: http://home.swbell.net/cjolley/die.html

          Basically it's a zip file.
          Output is through the lone putchar()
          The first long string is the data and the second shorter string is the key (directory?)

          Also, the entire thing is done in the return statement, so it runs backwards off the stack.
          Try giving a debugger a look at it

          10 points for knowing what the "char *a;" means in this code segment:
          main(t,_,a)
          char *a;
          {

          I tried untangling the “?” operators once but got stuck about 2/3 of the way through.
          Anyone have a beautifier clever enough to straighten it out?

          chuck
          Chuck
          秋音的爸爸

          Comment


          • #6
            Recursion has its uses. Some things can ONLY be done recursively, and some are just cleaner/faster/smaller if done through recursion. Normally, I prefer iterative stuff too, but it's certainly not "to be avoided at all costs." And often when people write an iterative solution to avoid recursive coding, they end up with something that is more likely to break or run out of resources than the recursive approach.
            Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

            Comment


            • #7
              the ? operator means nothing by itself but if u add the ":" it's better. That operator is an alternative to the if function.... here's an example of how it works.

              (condition) ? True : False.

              a>10 ? a-- : a++;

              IMHO if makes code lots more readable in most cases so the ?: operator isn't too much of use.

              And i did'nt say that i did'nt like it, just that i don't like the way it processes even tho in that case the recursive function is better.

              Spazm
              P3-667@810 retail, Asus CUSL2-C, 2*128 mb PC-133(generic), G400DH 16mb, SBLive value, HollyWood+, 1*Realtek 8029(AS) and 1*Realtek 8039C, Quantum 30g, Pioneer DVD-115f

              Comment


              • #8
                Originally posted by Wombat
                Recursion has its uses. Some things can ONLY be done recursively, and some are just cleaner/faster/smaller if done through recursion. Normally, I prefer iterative stuff too, but it's certainly not "to be avoided at all costs." And often when people write an iterative solution to avoid recursive coding, they end up with something that is more likely to break or run out of resources than the recursive approach.
                Excuse me, but writing a find function in a tree is lots better iteratively since u'll have lots of chances of taking all resources. Iterative function take almost no ressources since it's only created once. Don't forget that a recursive function has as much int/float/procedure call stacked that the number of nodes in the tree which makes a lot.

                Spazm
                P3-667@810 retail, Asus CUSL2-C, 2*128 mb PC-133(generic), G400DH 16mb, SBLive value, HollyWood+, 1*Realtek 8029(AS) and 1*Realtek 8039C, Quantum 30g, Pioneer DVD-115f

                Comment


                • #9
                  Stacks don't have to be very large.

                  And I'd still like to see you write an efficient, effective, closest-pair implementation.
                  Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                  Comment


                  • #10
                    recursion is sometimes natural

                    There are some problems that you will find are very natural to solve using recursion rather than illeteration. Two problems I have found recursion useful for are:

                    1) Processing of brackets in in-fix equations, eg

                    User enters: 1 + (2 * 3) + 4

                    This equation is sent to the decoder function evalfunc(). The decoder parses until it reaches the bracket, then gathers the equation inside the bracket and sends it the evalfunc() again.

                    2) processing of * in unix pattern matching. since * is greedy, you attempt to match the substring after the * in the pattern recursively with whatever is unmatched in the string to match.

                    User enters: funtimes
                    Pattern to match with f*es

                    so the function receives both of these strings. and matches the f and then reaches the *. Because the * is greedy, we recurse by calling ourself with:

                    pattern: es string: s
                    then
                    pattern: es string: es

                    since they match, the pattern is said to match the string.

                    Both of these examples would be MUCH harder, and much more unnatural to do illiteratively.
                    80% of people think I should be in a Mental Institute

                    Comment


                    • #11
                      I know that but it can get problematic in some situation. Of course that depend on what type of machine you'll have to run the code. If u run that on a DUAL XEON with 512mb of ram the chances that u'll ever have a prob with recursive code is almost a miracle but if u code for old pentium that run with 32mb of ram there are many cases u can get a not enough memory errors.

                      And I doubt that u'll ever see a closest-pair implementation written by me since i don't work on those type of functions.

                      Spazm
                      P3-667@810 retail, Asus CUSL2-C, 2*128 mb PC-133(generic), G400DH 16mb, SBLive value, HollyWood+, 1*Realtek 8029(AS) and 1*Realtek 8039C, Quantum 30g, Pioneer DVD-115f

                      Comment


                      • #12
                        Then what DO you write? Also, if you have the Dual proc setup, then you'd probably favor the recursive solution even more, since it is naturally given to parallel execution.
                        Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                        Comment


                        • #13
                          I usuually write tax income software, inventory softwares and thing like those.

                          Anyway i don't have a dual proc. I said that 'cuz most of my client runs old 386/486/pentium with win98 and 32mb of ram...

                          Hell that's not like i said that recursive functions were useless it's just that if i have the choise between iterative and recusive the iterative would be my choise.... unless it's forced by my boss.

                          Spazm
                          P3-667@810 retail, Asus CUSL2-C, 2*128 mb PC-133(generic), G400DH 16mb, SBLive value, HollyWood+, 1*Realtek 8029(AS) and 1*Realtek 8039C, Quantum 30g, Pioneer DVD-115f

                          Comment


                          • #14
                            /rant on/
                            Originally posted by spazm_1999
                            the ? operator means nothing by itself but if u add the ":" it's better. That operator is an alternative to the if function.... here's an example of how it works.

                            (condition) ? True : False.

                            a>10 ? a-- : a++;

                            IMHO if makes code lots more readable in most cases so the ?: operator isn't too much of use.

                            And i did'nt say that i did'nt like it, just that i don't like the way it processes even tho in that case the recursive function is better.

                            Spazm
                            Umm.....?

                            Why the hostility?
                            1, I know what the "?:" is. I successfuly converted 2/3 of this program to "if"s. You try it.
                            2, I have never used a "?" in a C program because they are hard to read and have no compilation benifit.
                            3, I wasn't comparing my feelings about the code to yours. I just said that I thought it was clever & fun code.
                            /rant off/

                            This is a fun program, not a lesson in the pros & cons of recursive production code.

                            BTW: Wombat, I've never heard of an algorithm that has to be written recursively.

                            chuck
                            Chuck
                            秋音的爸爸

                            Comment


                            • #15
                              Cjolley, i'm sorry if u feel hostility in my post but i don't feel anything in that... i don't feel that i'm normal at all so how can i even know if i write something offensive to people. Well for the
                              #1 it's just a misread of my part i thought u wanted to know what the ?: operator means.... 95% of my classmates never even heard of it so i just thought it was your case.
                              For #2 were on the same track i don't use 'em for the same reason.
                              For #3, neither do i... i was just stating my opinion even if i do say it in a way that no one understand it.

                              Spazm
                              P3-667@810 retail, Asus CUSL2-C, 2*128 mb PC-133(generic), G400DH 16mb, SBLive value, HollyWood+, 1*Realtek 8029(AS) and 1*Realtek 8039C, Quantum 30g, Pioneer DVD-115f

                              Comment

                              Working...
                              X