Worthwhile task shaving: Difference between revisions

(Added completed solution in python)
Line 157:
6 HOURS | 2 MONTHS 2 WEEKS 1 DAY
1 DAY | 9 MONTHS 9 WEEKS 5 DAYS</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
'''Works with jaq, the Rust implementation of jq'''
<syntaxhighlight lang="jq">
### Formatting
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
def rpad($len): tostring | ($len - length) as $l | . + (" " * $l);
 
def center($width):
tostring
| (($width-length)/2|floor) * " " + . | rpad($width);
 
### Task Shaving
 
def shaved: # time shaved off in seconds
[1, 5, 30, 60, 300, 1800, 3600, 21600, 86400];
 
def columns:
["1 SECOND", "5 SECONDS", "30 SECONDS", "1 MINUTE", "5 MINUTES",
"30 MINUTES", "1 HOUR", "6 HOURS", "1 DAY"];
 
def parameters:
{ diy: 365.25, minute: 60}
| .hour = .minute * 60
| .day = .hour * 24
| .week = .day * 7
| .month = .day * .diy / 12
| .year = .day * .diy
| .freq = [50 * .diy, 5 * .diy, .diy, .diy/7, 12, 1] # frequency per year
| .mult = 5 # multiplier for table
;
 
# Total field width is 13
def fmtTime($interval):
if . == 0 then 13*" "
else floor
| (if . == 1 then " " else "S" end) as $pl
| "\(lpad(2)) \($interval + $pl | rpad(10))"
end;
 
# Format one row
def row:
map(lpad(13)) | "\(.[0]) | \(.[1:]|join(""))";
 
"*** HOW OFTEN YOU DO THE TASK ***" | center(90),
(["SHAVED OFF", "50/DAY", "5/DAY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY"]
| map(center(13)) | row),
("-" * 90),
(parameters as $p
| range(0;9) as $y
| [columns[$y],
(range(0; 6) as $x
| ($p.freq[$x] * shaved[$y] * $p.mult)
| if . < $p.minute then . | fmtTime("SECOND")
elif . < $p.hour then ./$p.minute | fmtTime("MINUTE")
elif . < $p.day then ./$p.hour | fmtTime("HOUR")
elif . < 14 * $p.day then ./$p.day | fmtTime("DAY")
elif . < 9 * $p.week then ./$p.week | fmtTime("WEEK")
elif . < $p.year then ./$p.month | fmtTime("MONTH")
else 0 | fmtTime("")
end) ] | row)
</syntaxhighlight>
{{output}}
<pre>
*** HOW OFTEN YOU DO THE TASK ***
SHAVED OFF | 50/DAY 5/DAY DAILY WEEKLY MONTHLY YEARLY
------------------------------------------------------------------------------------------
1 SECOND | 1 DAY 2 HOURS 30 MINUTES 4 MINUTES 1 MINUTE 5 SECONDS
5 SECONDS | 5 DAYS 12 HOURS 2 HOURS 21 MINUTES 5 MINUTES 25 SECONDS
30 SECONDS | 4 WEEKS 3 DAYS 15 HOURS 2 HOURS 30 MINUTES 2 MINUTES
1 MINUTE | 2 MONTHS 6 DAYS 1 DAY 4 HOURS 1 HOUR 5 MINUTES
5 MINUTES | 10 MONTHS 4 WEEKS 6 DAYS 21 HOURS 5 HOURS 25 MINUTES
30 MINUTES | 6 MONTHS 5 WEEKS 5 DAYS 1 DAY 2 HOURS
1 HOUR | 2 MONTHS 10 DAYS 2 DAYS 5 HOURS
6 HOURS | 2 MONTHS 2 WEEKS 1 DAY
1 DAY | 8 MONTHS 8 WEEKS 5 DAYS
</pre>
 
=={{header|Julia}}==
2,467

edits