Shop OBEX P1 Docs P2 Docs Learn Events
Spin Golf Challenge ... and an interesting exception to operator precedence. (S — Parallax Forums

Spin Golf Challenge ... and an interesting exception to operator precedence. (S

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2010-02-19 00:32 in Propeller 1
In another thread, Lucky said...
How about finding the middle number out of three values?
This sounds like a good Spin "golf" challenge for the remainder of the holiday weekend. Here are the rules:

1. The code has to be written in one line of native Spin (i.e. no method calls).

2. The input variables are a, b, and c, which can take on any 32-bit signed value, and two or all three can be equal. The output variable is z and has to be assigned the middle value of the input variables.

3. You can use as many additional variables as you like, but no assumptions can be made about their initial values.

4. Once you post your code here, everyone can see it, and anyone can submit a modified version.

5. Posts which are edited will be disqualified. To modify a post, submit a new one.

6. The first submitted code line that works for all cases of the input variables, with the least number of characters (including whitespace), before 12 Monday night (PST), wins.

Here's a pathetic example (107 characters) to get everyone started:

z:=a&(a=<b and a=>c or a=>b and a=<c)|b&(b=<a and b=>c or b=>a and b=< c|c&(c=<a and c=>b or c=>a and c=<b)




GO!

-Phil

Post Edited (Phil Pilgrim (PhiPi)) : 2/17/2010 6:09:27 AM GMT
«1

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-15 01:00
    28 characters

    z:=a+b+c-(a#>b#>c)-(a<#b<#c)

    ...although it also seems to work without the parenthesis, but just to be sure, I left them in.

    24 characters without the parenthesis

    z:=a+b+c-a#>b#>c-a<#b<#c

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-15 01:08
    Beau,

    Nice! Without the parens, what values did you test it with? According to the manual (p. 251), + and - are supposed to have precedence over <# amd #>, which would make the paren-free version equivalent to:

    z:=(a+b+c-a)#>b#>(c-a)<#b<#c

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-15 01:18
    Phil Pilgrim,

    yup, the parenthesis are required... my test values were fairly low and ascending... making them descending caused the solution without parenthesis to fail.

    Note: The solution with parenthesis should still work out even if the numbers added together cause a roll-over

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-15 02:40
    Beau,

    Your solution with the parens is going to be tough to beat. I really did not see that one coming!

    So ... anybody? Are we just going to let Beau walk away with it?

    -Phil
  • W9GFOW9GFO Posts: 4,010
    edited 2010-02-15 03:19
    z:=b smilewinkgrin.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-15 03:30
    W9GFO,

    LOL! And you'd be right a third of the time — with one seventh the characters that Beau's solution required!

    -Phil
  • W9GFOW9GFO Posts: 4,010
    edited 2010-02-15 03:43
    Well b is in the middle of a and c...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • rokickirokicki Posts: 1,000
    edited 2010-02-15 05:10
    Untested, but:
    z:=a#>b<#(a#>c<#(b#>c))
    
    


    23, which beats the previous record of 28, I believe.

    Post Edited (rokicki) : 2/15/2010 5:24:52 AM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-15 05:19
    Tom,

    You've got a couple typos. Try again, and I'll check it out.

    -Phil
  • rokickirokicki Posts: 1,000
    edited 2010-02-15 05:23
    Thanks, Phil!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-15 05:39
    Well, you're supposed to make a separate post, but that's okay. It's an informal contest, and no one came in after you. smile.gif

    Anyway, 'quite amazing! Here's how I interpret it:

    z:=min(max(a,b), max(a,c), max(b,c))

    'Looks right. Beau, I think Tom's got you beat — so far.

    -Phil
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-02-15 19:11
    Calhouncam,

    I might not be much good with SPIN but I am pretty good at spotting spam bots. Good bye.

    Graham
  • BeanBean Posts: 8,129
    edited 2010-02-15 19:48
    Graham,
    I delete the spam.

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.

    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

    March 2010 Nuts and Volts article·http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf
    ·
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-16 04:51
    rokicki,

    Nice! ... I keep thinking there is something that should be redundant and could cancel out with common terms like the 'c' by rearranging the order of the operation (basically the same equation), but nothing jumps out at the moment.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • rokickirokicki Posts: 1,000
    edited 2010-02-16 18:29
    Howdy!

    I've always enjoyed Golf challenges (although Perl is really where that shines; Spin is very simple).

    There always seems to be a way to cut another couple of characters off.

    I'm sure this thread will be resurrected by someone who comes up with the next trick.

    -tom
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-16 19:19
    Tom,

    Congrats! I've tried to think of ways to reduce your solution but, as yet, can't see any. No matter how the pieces are rearranged, I still end up with two sets of parens, viz:

    ····z:=a#>b<#(a#>c)<#(b#>c)

    I, too, have enjoyed Perl golf challenges, but it's been years since I've entered one. (And if Ton Hospel enters, it's nearly pointless to assume I have a chance of winning. Have you ever beaten him?)

    If anyone can think of a shorter solution than Tom's, feel free to post it here. smile.gif

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-16 21:25
    Simplifying Tom's version down to 20 Characters...

    z:=a#>b<#((a<#b)#>c)


    If you look at Tom's version...

    a#>b<#(a#>c)<#(b#>c)

    ... there is an implied set of parenthesis, but you don't need them since it's the first term. But it does help to see the equation...

    (a#>b)<#(a#>c)<#(b#>c)

    ... Now, if you treat this like regular multiplication simplification and substitute '#>' with '*' and substitute '<#' with '+' it MUCH easier to see the like terms I was referring to with 'c' that can be simplified...

    (a*b)+(a*c)+(b*c)

    ... you could also do it with 'a' or 'b', but visually 'c' is what I saw first so the equation above becomes...

    (a*b)+((a+b)*c)


    ... Substituting back in '*' with '#>' and substituting '+' with '<#' the equation is simplified to...


    z:=(a#>b)<#((a<#b)#>c)

    ...where the parenthesis in the first term can be removed with a result of...

    z:=a#>b<#((a<#b)#>c)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 2/16/2010 9:32:28 PM GMT
  • rokickirokicki Posts: 1,000
    edited 2010-02-16 21:25
    I don't think I've ever been *competitive* in the golf challenges, let alone to the point where I can approach
    Hospel. It's scary, what you can do with Perl.

    Here's another challenge for Spin: input is "a", output is "z"; return in z the number of bits set in a.
    Only non-whitespace characters count. You can destroy a. You can assume the use of whatever
    single-character variables you need, but none are initialized (except, of course, a). One valid solution
    might be something like:

    b := 1
    z := 0
    repeat while b
       if a & b
          z++
       b += b
    
    



    This would count as 32 characters (unless I've miscounted).
  • rokickirokicki Posts: 1,000
    edited 2010-02-16 21:32
    And further:

    z:=a#>b<#(a<#b#>c)
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-16 21:43
    Wow rokicki, I just set myself up for that one... smilewinkgrin.gif

    So with the '*' '+' analogy I made earlier, that looks like ...

    a*b+(a+b*c)

    ... nice!!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • rokickirokicki Posts: 1,000
    edited 2010-02-16 21:47
    Yeah, sorry for making such a trivial "contribution" to your surprising breakthrough!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-16 22:30
    Beau, Tom,

    Very astute insights! Nice!

    -Phil
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-16 22:47
    Regarding Tom's new challenge, I'll set the limbo stick a little lower with this algorthm by Tracy Allen:

    z := 0
    while a
       z++
       a := a - |< (>|  x - 1)
    
    
    


    Tracy's ahead with 27 characters. smile.gif

    Cripes! I missed Tom's evern shorter solution just above Tracy's!

    z := 0
    while a
       z++
       a &= a-1
    
    
    


    Tom streaks into the lead with 19! (Tom, were you sandbagging us, or did you just forget? smile.gif )

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 2/16/2010 10:54:46 PM GMT
  • rokickirokicki Posts: 1,000
    edited 2010-02-16 22:53
    Crud, I knew this was discussed a while ago, but not in a golf context. Okay, maybe we should cancel this one
    because it's already been done? Or can someone simplify what's there even more?
  • hover1hover1 Posts: 1,929
    edited 2010-02-16 23:30
    Beau,

    Gosh, (Nepolian Dynamie speak), can you just get back to the Prop 2 layout, and forget about these trival sidetracks. roll.gif
    Jim
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-02-17 00:01
    If all we care about is written characters, you could change that "z := 0" to "z~". It does the same thing, but it is slightly slower.
    By the way, it being slightly slower seems like a compiler issue to me. They should perform identical actions. And seeing as we can easily test which is faster, the compiler should be changed so it compiles "z~" to the same byte code as "z := 0"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

    Some of my objects:
    MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
    Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
    String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
    Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!

    Post Edited (Bobb Fwed) : 2/17/2010 12:16:12 AM GMT
  • kuronekokuroneko Posts: 3,623
    edited 2010-02-17 03:01
    z := 0
    while a
       z++
       a &= a-1
    


    FWIW, that doesn't even compile (while on its own isn't enough).
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-17 03:29
    Hmm, good point. repeat adds another six non-whitespace characters for a total of 25 — still beating Tracy's 27. (And Tracy doesn't even know he entered!)

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-02-17 04:16
          z~                          {2}
          repeat 32                   {8}
            z+=a&1                    {6}
            a>>=1                     {5} {21}
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-02-17 04:57
    Buidling on Beau's insight:

      z~                        {2}
      repeat 32                 {8}
        z += 1 & a ->= 1        {10} {20}
    
    
    


    This is a weird one. I wasn't sure it would even compile — much less work — until I tried it. It would appear to violate precedence, but I've verified that it's interpreted as:

      z~                       
      repeat 32                
        z += 1 & (a ->= 1)
    
    
    


    I can only surmise that it's compiled this way to preserve the lvalue of a in the expression, since (1 & a) ->= 1 is meaningless. It deserves a footnote in the manual's precedence table, though.

    -Phil
Sign In or Register to comment.