I have been recently told to read an entire physics textbook by the new year (true story, unfortunately). I need your help to determine what chapters I should read each day. This is where you come in.
Input
- Two dates, in any format. The second date will always be later than the first.
- A list of chapter numbers. This comma-separated list can contain single chapters (
12) or inclusive ranges (1-3). Ex.1-3,5,6,10-13. - A list of weekdays (represented by the first two letters of the name:
Monday -> Mo) to exclude from the schedule. Ex.Mo,Tu,Fr.
Output
Output will be a newline-separated list of dates and chapter numbers (see format below). The chapters should be evenly distributed over all days in the range, excluding the weekdays provided. If the chapters do not distribute evenly, have the days with lower amounts of chapters at the end of the period of time. Dates in output can be in a different format than input. Days with no chapters can be ommited, or just have no chapters with it.
Example:
Input: 9/17/2015 9/27/2015 1-15 Tu
Output:
9/17/2015: 1 2
9/18/2015: 3 4
9/19/2015: 5 6
9/20/2015: 7 8
9/21/2015: 9 10
9/23/2015: 11
9/24/2015: 12
9/25/2015: 13
9/26/2015: 14
9/27/2015: 15
-
\$\begingroup\$ The input in the example should be `9/17/2015 9/27/2015 1-15 Tu' because 9/22 is a Tuesday. \$\endgroup\$DavidC– DavidC2015年11月17日 21:23:09 +00:00Commented Nov 17, 2015 at 21:23
-
\$\begingroup\$ @DavidCarraher you are right, when I made that sample input I was thinking of November for some reason. \$\endgroup\$GamrCorps– GamrCorps2015年11月17日 21:36:58 +00:00Commented Nov 17, 2015 at 21:36
-
8\$\begingroup\$ If it was me the last date would have all the chapters :) \$\endgroup\$MickyT– MickyT2015年11月17日 23:29:01 +00:00Commented Nov 17, 2015 at 23:29
-
\$\begingroup\$ @MickyT precisely my inspiration for this challenge. \$\endgroup\$GamrCorps– GamrCorps2015年11月18日 03:05:48 +00:00Commented Nov 18, 2015 at 3:05
-
\$\begingroup\$ You'll soon discover how much amazing physics is. You're lucky actually. \$\endgroup\$Fabrizio Calderan– Fabrizio Calderan2015年11月23日 15:21:56 +00:00Commented Nov 23, 2015 at 15:21
3 Answers 3
JavaScript (ES6), (削除) 317 (削除ここまで) (削除) 310 (削除ここまで) 291 bytes
(a,b,c,d)=>{u=0;c.split`,`.map(m=>{p=m[s]`-`;for(q=n=p[0];n<=(p[1]||q);r=++u)c+=","+n++},c="");c=c.split`,`;x=d.map(p=>"SuMoTuWeThFrSa".search(p)/2);for(g=[];a<b;a.setTime(+a+864e5))x.indexOf(a.getDay())<0&&(t=y=g.push(a+" "));return g.map(w=>w+c.slice(u-r+1,u-(r-=r/y--+.99|0)+1)).join`
`}
Usage
f(new Date("2015-09-17"),new Date("2015-09-27"),"5,1-4,6,10-13,20-27",["Su","Tu"])
=> "Thu Sep 17 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 5,1,2
Fri Sep 18 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 3,4,6
Sat Sep 19 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 10,11
Mon Sep 21 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 12,13
Wed Sep 23 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 20,21
Thu Sep 24 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 22,23
Fri Sep 25 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 24,25
Sat Sep 26 2015 10:00:00 GMT+1000 (AUS Eastern Standard Time) 26,27"
Explanation
(a,b,c,d)=>{
u=0; // u = total chapters
c.split`,`.map(m=>{ // c = array of each chapter
p=m[s]`-`;
for(q=n=p[0];n<=(p[1]||q);r=++u) // get each chapter from ranges
c+=","+n++
},c="");
c=c.split`,`;
x=d.map(p=>"SuMoTuWeThFrSa".search(p)/2); // x = days to skip
for(g=[];a<b;a.setTime(+a+864e5)) // for each day between a and b
x.indexOf(a.getDay())<0&& // if this day is not skipped
(t=y=g.push(a+" ")); // add it to the list of days
// t = total days
// y = days remaining
return g.map(w=>w+
c.slice(u-r+1,u-(r-=r/y--+.99|0)+1) // add the chapters of the day
).join`
`
}
PowerShell v4, (削除) 367 (削除ここまで) (削除) 357 (削除ここまで) (削除) 323 (削除ここまで) (削除) 313 (削除ここまで) (削除) 308 (削除ここまで) (削除) 307 (削除ここまで) (削除) 305 (削除ここまで) 277 Bytes
param($a,$b,$c,$d)$e=@();$c=-split('('+($c-replace'-','..'-replace',','),(')+')'|iex|%{$_-join' '});while($a-le$b){if(-join"$($a.DayOfWeek)"[0,1]-notin$d){$e+=$a;$z++}$a=$a.AddDays(1)}$g=,0*$z;$c|%{$g[$c.IndexOf($_)%$z]++};1..$z|%{"$($e[$_-1]): "+$c[$i..($i+=$g[$_-1]-1)];$i++}
Edit - golfed 28 bytes by using explicit input formatting.
Explained:
param($a,$b,$c,$d) # Parameters, takes our four inputs
$e=@() # This is our array of valid output dates
$c=-split('('+($c-replace'-','..'-replace',','),(')+')'|iex|%{$_-join' '})
# Ridiculously complex way to turn the input chapters into an int array
# The first part changes "1,5-9,12" into a "(1),(5..9),(12)" format that
# PowerShell understands, then executes that with iex, which creates an
# array of arrays. Then iterate through each inner array and joins them all
# together with spaces, then finally splits on spaces to create a 1D array
while($a-le$b){ # Until we reach the end day
if(-join"$($a.DayOfWeek)"[0,1]-notin$d){
# Not an excluded day of the week
$e+=$a # Add it to our list of days
$z++ # Increment our count of total days
}
$a=$a.AddDays(1) # Move to the next day in the range
}
$g=,0*$z # Populate a new array with zeroes, same length as $e
$c|%{$g[$c.IndexOf($_)%$z]++}
# This populates $g for how many chapters we need each day
1..$z|%{"$($e[$_-1]): "+$c[$i..($i+=$g[$_-1]-1)];$i++}
# Goes through the days in $e, prints them, and slices $c based on $g
Usage
Expects the dates to be in .NET DateTime format. Expects the "skipped" days to be in an array (PowerShell equivalent of a list).
PS C:\Tools\Scripts\golfing> .\help-me-manage-my-time.ps1 (Get-Date '9/17/2015') (Get-Date '9/27/2015') '5,1-3,6,10-13,20-27' @('Su','Tu')
09/17/2015 00:00:00: 5 1 2
09/18/2015 00:00:00: 3 6
09/19/2015 00:00:00: 10 11
09/21/2015 00:00:00: 12 13
09/23/2015 00:00:00: 20 21
09/24/2015 00:00:00: 22 23
09/25/2015 00:00:00: 24 25
09/26/2015 00:00:00: 26 27
-
3\$\begingroup\$ There are so many dollar signs here...This must be expensive! :D \$\endgroup\$kirbyfan64sos– kirbyfan64sos2015年11月18日 21:01:19 +00:00Commented Nov 18, 2015 at 21:01
-
\$\begingroup\$ @kirbyfan64sos Only 12% of the characters here are
$... That's actually pretty average for PowerShell golfs, which seems to be around 10% to 15% (based on my own informal calculations of answers I've posted). \$\endgroup\$AdmBorkBork– AdmBorkBork2015年11月18日 21:11:55 +00:00Commented Nov 18, 2015 at 21:11 -
\$\begingroup\$ Over to you again :-) \$\endgroup\$Willem– Willem2015年11月21日 10:16:14 +00:00Commented Nov 21, 2015 at 10:16
-
\$\begingroup\$ Hmm 308 for mine as well ... \$\endgroup\$Willem– Willem2015年11月23日 20:52:12 +00:00Commented Nov 23, 2015 at 20:52
-
\$\begingroup\$ Good job on 305! 300 now :-) \$\endgroup\$Willem– Willem2015年11月24日 17:57:40 +00:00Commented Nov 24, 2015 at 17:57
Python 2 - (削除) 338 (削除ここまで) (削除) 317 (削除ここまで) (削除) 308 (削除ここまで) (削除) 304 (削除ここまで) 300
Here we go to get the ball rolling ...
def f(a,b,c,d):
from pandas import*;import numpy as n
s=str.split;e=n.array([])
for g in s(c,','):h=s(g,'-');e=n.append(e,range(int(h[0]),int(h[-1])+1))
k=[t for t in date_range(a,b) if s('Mo Tu We Th Fr Sa Su')[t.weekday()]not in d];j=len(k);e=array_split(e,j)
for u in range(j):print k[u],e[u]
Example input:
f('9/17/2015','9/27/2015','5,1-3,6,10-13,20-27',['Su','Tu'])
Example Output:
2015年09月17日 00:00:00 [ 5. 1. 2.]
2015年09月18日 00:00:00 [ 3. 6.]
2015年09月19日 00:00:00 [ 10. 11.]
2015年09月21日 00:00:00 [ 12. 13.]
2015年09月23日 00:00:00 [ 20. 21.]
2015年09月24日 00:00:00 [ 22. 23.]
2015年09月25日 00:00:00 [ 24. 25.]
2015年09月26日 00:00:00 [ 26. 27.]