假日头痛


14

警告:请勿从此帖子中寻求医疗建议。如果需要医疗建议,请咨询合格的专业人员。

我头疼。我需要头痛药。

我会告诉你我最近服用的几剂,然后你告诉我何时可以服用我的下一剂而不过量。

我给你这个字符串: P: 00:00, I: 02:00, P: 04:00, I: 06:00

你会给我这个: Next P: 08:00, I: 10:00

输入:

字符串,代表每次服药的时间,格式如下:

P: 00:00, I: 02:00, P: 04:00, I: 06:00

这意味着扑热息痛在00:00和04:00服用,布洛芬在02:00和06:00服用

输出(更新):

可以输入每次服药下次服用时间的字符串,格式如下:

Next P: 08:00, I: 10:00
  • 输出顺序应与服药顺序相同。-如果布洛芬在09:35和扑热息痛以及10:22服用,则输出应为Next I: 09:35, P: 10:22
  • 如果每种药物下一剂的时间相同,则输出顺序无关紧要:Next P: 08:00, I: 08:00或者Next I: 08:00, P: 08:00
  • 如果仅服用一种药物(在输入字符串中),则仅该药物应在输出字符串中: Next P: 02:00

规则:

  • 永远只有两种药物,扑热息痛“ P”和布洛芬“ I”。
  • 扑热息痛每4小时可以服用一次,在24小时内最多可以服用4次。
  • 布洛芬也可以每4小时服用一次,在24小时内最多服用4次。
  • 扑热息痛和布洛芬可以一起服用,也可以分开服用。一个不计入另一个的剂量。
  • 输入字符串中的时间将始终是连续的,但可能会持续到午夜(23:00-> 03:00)
  • 输入字符串中的时间不会超过24小时
  • 每种药物最多4次(总共8次)
  • 输入内容始终为非空,并且至少包含一种药物和一种药物

例子:

每两小时间隔两次

"P: 00:00, I: 02:00, P: 04:00, I: 06:00" -> "Next P: 08:00, I: 10:00"

单剂量扑热息痛

"P: 22:00" -> "Next P: 02:00"

24小时内对乙酰氨基酚的最大剂量,单次布洛芬剂量

"P: 04:05, P: 08:10, P: 12:15, I: 12:30, P: 16:25" -> "Next I: 16:30, P: 04:05"

测试用例:

"I: 06:00" -> "Next I: 10:00"
"P: 22:00" -> "Next P: 02:00"
"P: 22:00, P: 02:00, I: 06:00" -> "Next P: 06:00, I: 10:00"
"P: 00:00, I: 02:00, P: 04:00, I: 06:00" -> "Next P: 08:00, I: 10:00"
"P: 04:05, P: 08:10, P: 12:15, I: 12:30, P: 16:25" -> "Next I: 16:30, P: 04:05"
"I: 06:32, P: 08:15, I: 10:44, P: 13:03" -> "Next I: 14:44, P: 17:03"
"P: 07:30, I: 07:30, P: 11:30, I: 11:30, P: 15:30, I: 15:30, I: 19:30" -> "Next P: 19:30, I: 07:30"
"I: 07:30, P: 11:30, I: 11:30, P: 15:30, I: 15:30, P: 19:30, I: 19:30" -> "Next P: 23:30, I: 07:30"
"P: 07:30, I: 07:30, P: 11:30, I: 11:30, P: 15:30, I: 15:30, P: 19:30, I: 19:30" -> "Next P: 07:30, I: 07:30" OR "Next I: 07:30, P: 07:30"

这是代码高尔夫球,因此最短的答案int字节获胜。

更新:

现在的输出可以是扑热息痛和布洛芬的缩写;PI


我很高兴在输入和输出格式上有一些影响-meta post
Gurupad Mamadapur

@GurupadMamadapur可能输出,但是从输入中提取时间和药物类型只是挑战的一半
Erresen

我建议你让人们缩写扑热息痛和布洛芬输出,因为他们增加不必要的长度,以提交
Cyoce

@Cyoce是的,我同意,我自己尝试解决方案,实际上有点棘手-更新了规则以允许缩写输出
Erresen

@Lynn同意并更新
Erresen,2016年

Answers:


4

的JavaScript(ES6),367个 362 354 358字节

高尔夫球版:

A=i=>i>9?""+i:"0"+i,B=(s,a=":")=>s.split(a),C=(a,b,c,d)=>[...[s,t]=B((b>3?c:d)||":"),a+` ${A(s=b>3?+s:(+s+4)%24)}:`+A(t=+t)],F=s=>{a=B(s,m=", ");for(b=c=d=e=f=p=q=0;f<a.length;g=="P:"?(b++,d=d?h:p=h):(c++,e=e?h:q=h))[g,h]=B(a[f++]," ");[i,j,k]=C("P",b,p,d),[n,o,l]=C("I",c,q,e),r=B(h)[0];return"Next "+c?b?n*60+(n<r)*1440+j<i*60+(i<r)*1440+o?l+m+k:k+m+l:l:k}

取消留言/评论:

// Returns a zero-padded string of the argument.
A=i=>i>9?""+i:"0"+i,

// Since we do a lot of splitting, alias it. Making the
// second argument optional (and defaulting to ':') saved
// 3 bytes
B=(s,a=":")=>s.split(a),

// Constructs a string for output, along with the time
// of the next dose, in the format [hour, minute, string].
// Arguments:               type
// a -> type (P/I)          String
// b -> amount of doses     Number
//      taken
// c -> first dose taken    String
// d -> last dose taken     String
//
// The first two values are split from the string, but
// before the array is returned, they are converted to
// integers (during the string construction).
C=(a,b,c,d)=>[...[s,t]=B((b>3?c:d)||":"),a+` ${A(s=b>3?+s:(+s+4)%24)}:`+A(t=+t)],

// Main function. Returns the time(s) for the next dose.
// Argument:                type
// s -> list of times of    String
//      and types of 
//      doses taken
F=s=>{
    a=B(s,m=", "); // Split the input by comma + space,
                   // and save that string, since we
                   // need it later when constructing
                   // the output string.
    // For loop has been restructured. Original:
    // for(b=c=f=0;f<a.length;g=="P:"?(b++,d=d?h:p=h):(c++,e=e?h:q=h))
    //     [g,h]=B(a[f++]," ");
    b = 0; // P-dose counter
    c = 0; // I-dose counter
    d = 0; // Last P-dose
    e = 0; // Last I-dose
    p = 0; // First P-dose
    q = 0; // First I-dose
    for (f = 0; f < a.length; f++) {
        [g, h] = B(a[f], " ");  // g contains the type,
                                // h contains the time
        if (g == "P:") {
            b++;                // increase the counter

            if (d == 0) {   // store h in p if this is
                p = h;      // the first dose of this
            }               // type
            d = h;
        } else {
            // See the above code block for comments
            c++;

            if (e == 0) {
                q = h;
            }
            e = h;
        }
    }
    // End of restructured for loop.

    // Construct the output strings, and get the times.
    // See comments at C function.
    [i, j, k] = C("P", b, p, d);
    [n, o, l] = C("I", c, q, e);

    // Get the amount of hours of the dose taken last.
    // We use this to get the correct order of the two
    // times.
    r = B(h)[0];

    // Return statement has been restructured. Original:
    // return "Next "+c?b?n*60+(n<r)*1440+j<i*60+(i<r)*1440+o?l+m+k:k+m+l:l:k
    //==================================================
    // Start creating the output string.
    output = "Next "
    // Use the following checks to figure out what needs
    // to be part of the output and in what order.
    if (c > 0) {
        if (b > 0) {
            // Compare the times of next doses
            // P_time = (i + (i < r) * 24) * 60
            // I'm using implicit conversion of
            // booleans to numbers. If the next
            // dose is past midnight, add 1 * 24
            // to the time, so it is compared
            // correctly.
            // Then add the minutes to the number.
            P_time = i*60+(i<r)*1440+o;
            I_time = n*60+(n<r)*1440+j;

            if (I_time < P_time) {
                output += l + m + k; // I first
            } else {
                output += k + m + l; // P first
            }
        } else {
            output += l; // Just I
        }
    } else {
        output += k; // Just P
    }

    // Finally, return the output
    return output;
}

要使用它,请使用字符串作为参数调用F,如下所示:

F("P: 04:00, I: 06:00")

很好,但是我有几个问题。如果输入中只有一种药丸,例如F("P: 22:00")-> ,这似乎会失败ReferenceError: q is not defined。此输入将运行,如果P&我先前已经提及,但与老细节I.
克里斯-中号

谢谢!我刚刚测试过,您对参考错误的看法是正确的。我猜q变量没有重置,并且在测试过程中我没有给予足够的重视。感谢您通知我,稍后再解决。
路加福音

原来是一个简单的修复程序,但是花了我4个字节。
路加福音

1

Python 3-437字节

a=input();i=p=l=-1;j=q=0
for x in a.split(", ")[::-1]:
    for y, z in [x.split(": ")]:
        s=lambda q,r,t:[t,sum([a*b for a,b in zip([60,1],map(int,q.split(':')))])][r%4<2]+[0,240][r<2]
        if y=="I":j+=1;i=s(z,j,i)
        else:q+=1;p=s(z,q,p)
        l=[l,p+i-239][j+q<2]
r=lambda d,e:("","%s: %02d:%02d, "%(d,(e/60)%24,e%60))[e>-1];p+=[1440,0][p>=l];i+=[1440,0][i>=l];print("Next "+[r("I",i)+r("P",p),r("P",p)+r("I",i)][p<i][:-2])

说明:

a=input();i=p=l=-1;j=q=0
for x in a.split(", ")[::-1]: #Read in reverse order, a="P: 01:00"
    for y, z in [x.split(": ")]:#Y="P", Z="00:00"
        s=
        lambda q,r,t:[t,sum([a*b for a,b in zip([60,1],map(int,q.split(':')))])]#Convert "01:01" to 61
        [r%4<2]#In case it's the first or fourth string calculate a new value, otherwise: return the original value
        +[0,240][r<2]#In case it's the last string: add 4 hours. Otherwise, leave it.
        if y=="I":j+=1;i=s(z,j,i)#Calculate for i
        else:q+=1;p=s(z,q,p)#Calculate for p
        l=[l,p+i-239][j+q<2]#Sets the last record. Since we read in reverse order, this should be the first one. We've added 4 hours though so remove those again
r=lambda d,e:("","%s: %02d:%02d, "%(d,(e/60)%24,e%60))[e>-1];#Print function, don't print anything when we have no value
p+=[1440,0][p>=l];i+=[1440,0][i>=l];    #Add a day if record is before the last record so we can correctly calculate the order
print("Next "+[r("I",i)+r("P",p),r("P",p)+r("I",i)][p<i][:-2])#print it and remove the last ","

1

PHP,228个 241 239 227 226字节

需要PHP 7

Next<?foreach(explode(", ",$argv[1])as$d){[$m,$h,$i]=explode(":",$d);$x[$m][++$$m]=24+$h+$i/60;}foreach($x as$m=>$d)$r[$m]=$d[$$m-3]?:$d[$$m]-20;sort($r);foreach($r as$m=>$t)$o[]=" $m: ".date("i:s",$t%24*60);echo join(",",$o);

分解

Next<?                              // print "Next"
foreach(explode(", ",$argv[1])as$d) // loop through string split by comma+space
{
    [$m,$h,$i]=explode(":",$d);         // separate drug, hours and minutes
    $x[$m][++$$m]=24+$h+$i/60;          // append time to array, track count in ${$m}
}                                       // (i.e. $P for drug "P" etc.)
foreach($x as$m=>$d)                // loop through drugs
    $r[$m]=                             // add time to result
        $d[$$m-3]                           // if more than 3 medications, use $$m-3
            ??$d[$$m]-20                    // else use last medication - 20 hours
    ;
sort($r);                           // sort results by time
foreach($r as$m=>$t)$o[]=" $m: "    // prepare for output: drug name and formatted time:
    .date("i:s",$t%24*60)           // use hrs as mins and mins as secs to avoid TZ problems
;
echo join(",",$o);                  // print

0

JavaScript(ES6),246个字节

s=>s.split`, `.map(s=>(m[s[0]].unshift(t=s.replace(/\d+/,h=>(h=(1+h)%24)>9?h:`0`+h),s),l=l||t.slice(1)),l=0,m={I:[],P:[]})&&`Next `+[].concat(m.I[7]||m.I[0]||[],m.P[7]||m.P[0]||[]).sort((i,p)=>((i=i.slice(1))<l)-((p=p.slice(1))<l)||i>p).join`, `

说明:

遍历每个剂量,将IP剂量分成两个阵列。每个剂量还增加了4个小时,这些时间也被节省了。反向填充阵列,使检测8个条目更加容易。第一次给药后4小时的时间也被保存下来,以供分选时使用。此时,每个数组可以处于以下三种状态之一:

  • 8次输入,在这种情况下,最后一次输入是第一剂,下一剂必须在该剂后24小时(即明天的同一时间)
  • 2、4或6次进入,在这种情况下,第一次进入是在最后一剂之后4小时,因此是下一剂的时间
  • 0个条目,在这种情况下,我们concatentate [],它被展平,因此从结果中排除

从两个阵列中提取了下一个剂量时间后,仍然需要对它们进行排序。这是通过将它们与第一次给药后4小时的时间进行比较来完成的。如果两次在此时间之前之一,则必须指明天,并且该剂量最后一次。否则,将直接比较时间。(不便的是,该药早于时间,所以我必须将其剥离以进行适当比较。)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.