The Unofficial Macro Scheduler Puzzler of the Week 3
Moderators: Dorian (MJT support), JRL
The Unofficial Macro Scheduler Puzzler of the Week 3
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
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
-
- Macro Veteran
- Posts: 267
- Joined: Mon Sep 27, 2010 8:57 pm
- Location: Seattle, WA
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.
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%
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.
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.
-
- Macro Veteran
- Posts: 267
- Joined: Mon Sep 27, 2010 8:57 pm
- Location: Seattle, WA
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%
Jerry you are so-o-o close
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.
Your last script rounds down when the decimal portion of the number being rounded is equal to the chosen decimal value.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
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.
I think I did it in 3 lines (5 with vars)
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,
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.
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.
how about this?
..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
.. 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)}
Code: Select all
Let>InitVal1=13.85
Let>RoundOffset=0.84
mdl>{trunc(trunc(%initval1%)+((%initval1%)-(%roundoffset%+trunc(%initval1%))))+1}
Let>a
and used %a% in the MDL line
so i pulled it into 1
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.
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.