Spin Golf Challenge ... and an interesting exception to operator precedence. (S
Phil Pilgrim (PhiPi)
Posts: 23,514
This sounds like a good Spin "golf" challenge for the remainder of the holiday weekend. Here are the rules:In another thread, Lucky said...
How about finding the middle number out of three values?
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
Comments
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.
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
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.
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
LOL! And you'd be right a third of the time — with one seventh the characters that Beau's solution required!
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
23, which beats the previous record of 28, I believe.
Post Edited (rokicki) : 2/15/2010 5:24:52 AM GMT
You've got a couple typos. Try again, and I'll check it out.
-Phil
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
I might not be much good with SPIN but I am pretty good at spotting spam bots. Good bye.
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
·
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.
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
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.
-Phil
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
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:
This would count as 32 characters (unless I've miscounted).
z:=a#>b<#(a<#b#>c)
So with the '*' '+' analogy I made earlier, that looks like ...
a*b+(a+b*c)
... nice!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Very astute insights! Nice!
-Phil
Tracy's ahead with 27 characters.
Cripes! I missed Tom's evern shorter solution just above Tracy's!
Tom streaks into the lead with 19! (Tom, were you sandbagging us, or did you just forget? )
-Phil
Post Edited (Phil Pilgrim (PhiPi)) : 2/16/2010 10:54:46 PM GMT
because it's already been done? Or can someone simplify what's there even more?
Gosh, (Nepolian Dynamie speak), can you just get back to the Prop 2 layout, and forget about these trival sidetracks.
Jim
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
FWIW, that doesn't even compile (while on its own isn't enough).
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
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:
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