The Unofficial Macro Scheduler Puzzler of the Week 3

Anything Really. Just keep it clean!

Moderators: Dorian (MJT support), JRL

User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

The Unofficial Macro Scheduler Puzzler of the Week 3

Post by JRL » Fri Sep 27, 2013 2:58 pm

This weeks puzzler will be worth 20 rep points and bragging rights for the person who comes up with the shortest (fewest lines) program that accomplishes the puzzling requirements.


For this weeks puzzler we need a program that rounds to the nearest integer from a chosen decimal value.

For example, if you choose 0.75 as the decimal rounding value, 14.75 would round up to 15, 14.7499 would round down to 14

Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Fri Sep 27, 2013 3:17 pm

Since 0.5 is the normal 'break' point between going up or going down, we adjust for the difference from that amount.

Then we Truncate rather than Round from the adjusted initial value.

Code: Select all

Let>InitVal1=14.74999
Let>InitVal2=14.75
Let>RoundOffset=0.75

Let>RoundedVal1={Trunc(%InitVal1%+(%RoundOffset%-0.5))}

Let>RoundedVal2={Trunc(%InitVal2%+(%RoundOffset%-0.5))}

MDL>%InitVal1% rounds to %RoundedVal1% %CRLF% %InitVal2% rounds to %RoundedVal2%
Thanks,
Jerry

[email protected]

User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Sep 27, 2013 3:45 pm

Hmmm. re-reading the puzzler we may not have been clear enough.

We want the rounded answer to go up to the next integer when the decimal portion of the number being rounded is greater than or equal to the chosen decimal value, and to go down to the root integer if the decimal portion is less than the chosen decimal value.

As another example if our chosen decimal value is 0.25, 14.26 would round to 15 and 14.18 would round to 14.

Hopefully this is stated more clearly.

Jerry Thomas
Macro Veteran
Posts: 267
Joined: Mon Sep 27, 2010 8:57 pm
Location: Seattle, WA

Post by Jerry Thomas » Fri Sep 27, 2013 4:55 pm

Compare the decimal part of the initial value to the 'rounding value'

Code: Select all

Let>InitVal1=14.26
Let>RoundOffset=0.25

// TruncVal1 will = 14
Let>TruncVal1={Trunc(%InitVal1%)}

// DecimalPart will = 14.26 - 14 which = 0.26
Let>DecimalPart=InitVal1-TruncVal1

If>DecimalPart>RoundOffset
  // If 0.26 > 0.25, NewVal = 14 + 1
  Let>NewVal1=TruncVal1+1
else
  // If 0.26 not > 0.25, NewVal1 = 14
  Let>NewVal1=TruncVal1
Endif

MDL>%InitVal1%  %NewVal1%
Thanks,
Jerry

[email protected]

User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Sep 27, 2013 6:03 pm

Jerry you are so-o-o close
We want the rounded answer to go up to the next integer when the decimal portion of the number being rounded is greater than or equal to the chosen decimal value
Your last script rounds down when the decimal portion of the number being rounded is equal to the chosen decimal value.


Also, just as a point of reference. My script to accomplish this feat is 9 lines long. Jerry's last script is 7 lines. I'm not counting comment lines the message lines or the variable initialization lines. To win you will need to be the first to submit the shortest script that accomplishes the puzzler requirement. Beating my nine line script is not the goal though its likely the winning script will be shorter than nine lines.

I wonder how short a script might be that uses VBScript or RegEx?

One more thing to consider is decimal places. I know from making a script to accomplish this that too many decimal places will give odd results. Therefore let's limit the decimal accuracy to 14 decimal places. If a script is submitted that works properly at 14 decimal places but fails if the number has 15 decimal places it will be considered a good working script.

EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

I think I did it in 3 lines (5 with vars)

Post by EnderFFX » Fri Sep 27, 2013 6:48 pm

Code: Select all

Let>InitVal1=14.84
Let>RoundOffset=0.84

Let>FinalAnswer={Trunc(%InitVal1%)}
If>{(%InitVal1%-(%RoundOffset%+%FinalAnswer%)>0) or (%InitVal1%-(%RoundOffset%+%FinalAnswer%)=0)}
Add>FinalAnswer,1
EndIf

MessageModal>FinalAnswer

EnderFFX
Pro Scripter
Posts: 92
Joined: Mon Mar 08, 2004 6:17 am

Post by EnderFFX » Fri Sep 27, 2013 7:07 pm

4 lines, I think I got it, sorry about all the revisions :)

User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Sep 27, 2013 7:49 pm

EnderFFX,

Good job! I can't find anything wrong with the functionality so we wait and see if anyone can do it in 3 lines or fewer. I do see how you could shorten the code. I'll give 5 rep points immediately to the first person to shorten the four lines of code in EnderFFX's script. It will still be four lines.

hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

how about this?

Post by hoangvo81 » Wed Oct 02, 2013 7:09 pm

..truncating EnderFFX's code

Code: Select all

Let>InitVal1=14.84
Let>RoundOffset=0.84


If>{(%InitVal1%-(%RoundOffset%+trunc(%initVal1%))>0) or (%InitVal1%-(%RoundOffset%+trunc(%initval1%))=0)}
    let>FinalAnswer={trunc(%initval1%)+1}
EndIf

MessageModal>FinalAnswer


User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Oct 02, 2013 7:48 pm

hoangvo81,

Sorry, but I don't get a correct answer when the initial value of "InitVal1" is greater than 14 but less than 14 + the value of "RoundOffset" For example, using the posted initial "RoundOffset" value (0.84) plug in 14.83 for "InitVal1" and see the result.

User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Oct 02, 2013 8:33 pm

One more "rule" for this puzzler that I didn't consider in the beginning. We're not concerned about rounding negative numbers, only numbers greater than or equal to zero.

hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

Post by hoangvo81 » Wed Oct 02, 2013 8:48 pm

.. revised using EnderFFX still

Code: Select all

Let>InitVal1=13.85
Let>RoundOffset=0.84
let>a={(%initval1%-(%roundoffset%+trunc(%initval1%)))}
mdl>{trunc(trunc(%initval1%)+%a%+1)}

User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Oct 02, 2013 8:59 pm

hoangvo81,

Two lines... Since you're calculating in the mdl> line I'm going to have to count that one even though I said before I was ignoring the MDL line.

Looks very good. BUT.... I just took your code and turned it into a one-liner. Why don't you do that and sew this thing up?

hoangvo81
Pro Scripter
Posts: 69
Joined: Tue Feb 07, 2012 8:02 pm

Post by hoangvo81 » Wed Oct 02, 2013 9:05 pm

Code: Select all

Let>InitVal1=13.85
Let>RoundOffset=0.84
mdl>{trunc(trunc(%initval1%)+((%initval1%)-(%roundoffset%+trunc(%initval1%))))+1}
Thanks JRL, i just realize i left
Let>a
and used %a% in the MDL line
so i pulled it into 1

User avatar
JRL
Automation Wizard
Posts: 3500
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Oct 02, 2013 9:19 pm

Excellent!

I'd say this puzzler's been solved and hoangvo81 has 20 rep points coming his way.

Thank you to everyone who participated and hopefully I'll have a new puzzler ready to go by Friday.

P.S.
Until the next puzzler is posted, I still have five rep points for anyone who shortens one line of EnderFFX's last posted code without harming the code's functionality.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts