ผลรวมของเวลา 24 ชั่วโมง


21

กำหนดจำนวนเต็มตั้งแต่ 0 ถึง 141 (รวม) ให้แสดงรายการเวลา 24 ชั่วโมงทั้งหมดที่หน่วยชั่วโมงนาทีและวินาทีเพิ่มไปยังจำนวนเต็มนั้น

กฎของการเพิ่ม

ตัวเลขจะถูกเพิ่มตามหน่วยเวลาไม่ใช่ตัวเลขหลักเดียว

ตัวอย่างเช่นใช้ 17:43:59

17 + 43 + 59 = 119

จำไว้ว่าเป็นตัวอย่างของตัวเลขที่เพิ่มเข้ามา ในความเป็นจริงคุณจะเข้าสู่ 119 และ 17:43:59 จะเป็นหนึ่งในผลลัพธ์ ควรให้เอาต์พุตเป็น HH: MM: SS หรือ H: MM: SS

โปรดจำไว้ว่าจำนวนสูงสุดที่เป็นไปได้คือ 141 คือ 23:59:59 นี่คือรหัสกอล์ฟดังนั้นจำนวนที่น้อยที่สุดชนะ อนุญาตให้ทดลองใช้และข้อผิดพลาดได้ แต่อาจมีวิธีที่ดีกว่าในการทำเช่นนี้

แก้ไข: โปรดระบุตำแหน่งที่จะป้อนค่าในรหัสของคุณ


3
ยินดีต้อนรับสู่ Programming Puzzles & Code Golf! หากคุณเสียบปลั๊กหมายความว่าคุณได้เป็นส่วนหนึ่งของซอร์สโค้ดซึ่งมักจะไม่ได้รับอนุญาต โดยทั่วไปคุณควรยึดติดกับค่าเริ่มต้นเหล่านั้น เราต้องแสดงผลลัพธ์เป็นสตริงหรือไม่? ถ้าเป็นเช่นนั้นรูปแบบใดบ้างที่อนุญาต
เดนนิส

หมายเลขอินพุตรับประกันว่าจะเป็นค่าบวกหรือไม่ จะมีทางออกอย่างน้อยหนึ่งวิธีหรือไม่
xnor

ฉันได้แก้ไขคำถามเล็กน้อยเพื่อชี้แจง / ตอบบางสิ่ง หากความตั้งใจของคุณแตกต่างจากการเปลี่ยนแปลงของฉันอย่าลังเลที่จะแก้ไขเพื่อให้ตรงกับที่
Geobits

1
ฉันทำอย่างนั้นเพราะมันเป็นวิธีปกติที่ฉันเห็นเวลาที่กำหนด (ในโลกแห่งความเป็นจริง) ไม่มีใครเคยพูดว่ามันเป็น 13: 4: 7 แต่ 5:10:30 เกือบจะยอมรับได้เสมอ ฉันไม่มีปัญหากับการเปลี่ยนแปลง
Geobits

3
"กรุณาระบุที่ป้อนในรหัสของคุณ" - การประชุมเกี่ยวกับ PPCG สำหรับการป้อนข้อมูลใช้การขัดแย้งเช่นเดียวกับตัวเลือกอื่น ๆ ดูค่าเริ่มต้นสำหรับ Code Golf: วิธีการอินพุต / เอาต์พุตบน Meta
user2428118

คำตอบ:


8

เยลลี่ , 16 30 29 20 ไบต์

ขณะนี้มีรูปแบบผลลัพธ์ที่ถูกต้อง! ขอบคุณเดนนิสมากสำหรับความช่วยเหลือในการดีบั๊กคำตอบนี้ ยินดีต้อนรับคำแนะนำการเล่นกอล์ฟ ลองออนไลน์!

แก้ไข: +14 ไบต์จากการใช้รูปแบบผลลัพธ์ที่ถูกต้อง -1 ไบต์จากการลบพื้นที่พิเศษ -3 จากการเปลี่ยนจากการ24,60,60 “ð<<‘-6 ไบต์จากการเปลี่ยนไป+100DḊ€€d⁵

“ð<<‘Œp’S=¥Ðfd⁵j€”:Y

คำอธิบาย

“ð<<‘Œp’S=¥Ðfd⁵j€”:Y  Main link. Argument: n

“ð<<‘                 Jelly ord() the string `ð<<` to get [24, 60, 60]. Call this list z.
     Œp               Cartesian product of z's items. 
                        Since each item of z is a literal,
                        Jelly takes the range [1 ... item] for each item.
       ’              Decrements every number in the Cartesian product 
                        to get lowered ranges [0 ... item-1].
        S=¥           Create a dyadic link of `sum is equal to (implicit n)`.
           Ðf         Filter the Cartesian product for items with sum equal to n.
             d⁵       By taking divmod 10 of every number in each item,
                        we get zero padding for single-digit numbers
                        and every double-digit number just turns into a list of its digits.
               j€”:   Join every number with a ':'.
                   Y  Join all of the times with linefeeds for easier reading.

8

Bash, 71

  • บันทึก 8 ไบต์ด้วย @hvd
for t in {0..23}+{00..59}+{00..59};{((${t//+0/+}-$1))||echo ${t//+/:};}

ลองมันออนไลน์


1
printfมีราคาแพงที่นี่ โดยการtเข้าใกล้รูปแบบที่ถูกต้องและแก้ไขเพื่อให้((t-$1))งานคุณสามารถลงไปที่ 71:for t in {0..23}+{00..59}+{00..59};{((${t//+0/+}-$1))||echo ${t//+/:};}
hvd

@hvd กอล์ฟที่ดี - ขอบคุณ!
Digital Trauma

6

Perl 6 , 62 56 ไบต์

{map *.fmt('%02d',':'),grep $_==*.sum,(^24 X ^60 X ^60)}

เพียงตรวจสอบชุดค่าผสมที่เป็นไปได้ทั้งหมดในผลิตภัณฑ์ครอสของทุกชั่วโมงนาทีและวินาที


4

Python 3 , 91 ไบต์

def f(n):
 for k in range(86400):t=k//3600,k//60%60,k%60;sum(t)==n!=print('%d:%02d:%02d'%t)

มีวิธีแก้ปัญหาที่สั้นกว่าโดยใช้ exec (Python 2) หรือการเรียกซ้ำ (Python 3) แต่ทั้งคู่ต้องการหน่วยความจำในปริมาณที่ไม่สมเหตุสมผล

ลองออนไลน์!


4

PowerShell , 87 77 ไบต์

บันทึก 10 ไบต์ขอบคุณJohn L. Bevan

$d=date;0..86399|%{$d+=1e7l;"$d".Split()[1]}|?{("{0:H+m+s}"-f$d|iex)-in$args}

ลองออนไลน์! (นี่จะหมดเวลามันช้ามาก)

คำอธิบาย

[datetime]ค่อนข้างง่ายเริ่มต้นด้วยกระแสเพิ่ม 1 วินาที 86,399 ครั้งจัดรูปแบบเป็นสตริงจากนั้นเก็บเฉพาะค่าที่รวมกัน


FYI: คุณสามารถแทนที่10000000ด้วย1e7lการบันทึก 4 ไบต์ ... หรือแม้กระทั่ง1e7สำหรับไบต์พิเศษ (ฉันคิดว่าฉันต้องรวมLเพื่อประโยชน์ของพารามิเตอร์ แต่สงสัยว่าวิธีหลีกเลี่ยงความต้องการของคุณ
JohnLBevan

1
@ JohnLBevan ขอบคุณ! ฉันพยายาม1e7อย่างน้อย 30 นาทีและมันเป็นLpostfix ที่ฉันพลาดไป ฉันลืมเรื่องนี้และไม่สามารถหาวิธีที่จะทำให้มันเข้าสู่ int ที่สั้นกว่าค่าคงที่ ใครเป็นคนตัดสินใจว่านั่นเป็นการ[timespan]ตีความ[int]ว่าเป็นเห็บและ[double]เป็นวันหรือไม่? iexบิตจะสดใสสวยแม้ว่ามันจะทำให้สิ่งที่ทั้งนี้สุดฤทธิ์ช้าลง
ต้มตุ๋น

1
ไม่ต้องห่วง; ฉันมีความช่วยเหลือเช่นกัน;): stackoverflow.com/q/41408902/361842
JohnLBevan

1
@JohnLBevan แท้จริงฉันเพิ่งเห็นคำถามนี้ก่อนความคิดเห็นที่คุณเชื่อมโยงมัน! ดี
ต้มตุ๋น

1
นอกจากนี้ยังมีiexเคล็ดลับที่ดัดแปลงมาจากเคล็ดลับที่นี่: codegolf.stackexchange.com/a/746/6776
JohnLBevan


2

Haskell, 90 ไบต์

p x=['0'|x<10]++show x
i=[0..59]
f x=[p h++':':p m++':':p s|h<-[0..23],m<-i,s<-i,h+m+s==x]

กลับรายการของ HH A: MM: SS สตริงเช่น->f 140["22:59:59","23:58:59","23:59:58"]

สามลูปง่ายๆผ่านชั่วโมงนาทีและวินาที xเก็บและจัดรูปแบบค่าทั้งหมดที่รวมเป็นจำนวนการป้อนข้อมูล




2

Batch, 168 bytes

@for /l %%t in (0,1,86399)do @call:c %1 %%t
@exit/b
:c
@set/ah=%2/3600,m=%2/60%%60,s=%2%%60,n=%1-h-m-s
@set m=0%m%
@set s=0%s%
@if %n%==0 echo %h%:%m:~-2%:%s:~-2%

Outputs single-digit hours.




1

QBIC, 82 72 bytes

:[0,23|[0,59|[0,59|~b+c+d=a|?!b$+@:`+right$(@0`+!c$,2)+A+right$(B+!d$,2)

This hits an unfortunate spot in QBasic, with casting to number, trimming and prepending a 0 when necessary is really costly.

Sample output:

Command line: 119
1:59:59
2:58:59
2:59:58
3:57:59
[... SNIP 270 lines ...]
23:58:38
23:59:37

Explanation I wrote a novel about it:

:           Get N, call it 'a'
[0,23|      Loop through the hours; this FOR loop is initialised with 2 parameters
            using a comma to separate FROM and TO, and a '|' to delimit the argument list
[0,59|      Same for the minutes
[0,59|      And the seconds
            QBIC automatically creates variables to use as loop-counters: 
            b, c, d (a was already taken by ':')
~b+c+d=a    IF a == b+c+d
|           THEN
 ?          PRINT
  !         CAST
   b        'b'
    $       To String; casting num to str in QBasic adds a space, this is trimmed in QBIC
+@:`        Create string A$, containing ":"
+right$      This is a QBasic function, but since it's all lowercase (and '$' is 
            not a function in QBIC) it remains unaltered in the resulting QBasic.
(@0`+!c$,2) Pad the minutes by prepending a 0, then taking the rightmost 2 characters.
+A          Remember that semicolon in A$? Add it again
+right$     Same for the seconds
(B+!d$,2)   Reusing the 0-string saves 2 bytes :-)

QBIC looks interesting. Did you create it just for #code-golf!? :)
wasatchwizard

@wasatchwizard Yup :-)
steenbergh

1

PowerShell, 67 79 Bytes (nasty version)

Since the rules say nothing about completing in a certain time (or at all), and nothing about no duplicates, here's a horrific solution:

for(){if(("{0:H+m+s}"-f($d=date)|iex)-in$args){"{0:H:mm:ss}"-f$d}}

1
I can't find the relevant meta post, but i'm pretty sure a submission have to halt to be valid, unless specified by the challenge
Sefa

Thanks @Sefa... if that's the case I can't find a nice way to get my nasty version to work in less characters than Briantist's clean version... Tempted to delete this answer, but I'm kind of proud of how bad it is ;)
JohnLBevan

0

Racket 39 bytes

(for*/sum((h 24)(m 60)(s 60))(+ h m s))

Ungolfed:

(for*/sum      ; loop for all combinations; return sum of values for each loop
   ((h 24)     ; h from 0 to 23
    (m 60)     ; m from 0 to 59
    (s 60))    ; s from 0 to 59
  (+ h m s))   ; sum of all 3 variables

0

MATL, 29 bytes

24:q60:qt&Z*t!si=Y)'%i:'8:)&V

Try it online!

Explanation

24:q     % Push [0 1 ... 23]
60:q     % Push [0 1 ... 59]
t        % Duplicate
&Z*      % Cartesian product of the three arrays. This gives a matrix with each
         % on a different row Cartesian tuple
t!       % Push a transposed copy
s        % Sum of each column
i=       % Logical mask of values that equal the input
Y)       % Select rows based on that mask
'%i:'    % Push this string
8:)      % Index (modularly) with [1 2 ... 8]: gives string '%i:%i:%i'
&V       % Convert to string with that format specification. Implicitly display

0

JavaScript, 122 120 bytes

Takes one additional empty string as input, which I presume doesn't count towards the size. Updated bytecount (including historical) to add two bytes for the string's initialization.

console.log((
//Submission starts at the next line
i=>o=>{for(h=24;h--;)for(m=60;m--;)for(s=60;s--;)if(h+m+s==i)o+=`${h}:0${m}:0${s} `;return o.replace(/0\d{2}/g,d=>+d)}
//End submission
)(prompt("Number:",""))(""))


If you need to initialize a string to empty, the initialization must be counted
edc65

@edc65 Done.···
user2428118

0

JavaScript (ES6), 110

v=>eval("o=[];z=x=>':'+`0${x}`.slice(-2);for(m=60;m--;)for(s=60;s--;h>=0&h<24&&o.push(h+z(m)+z(s)))h=v-m-s;o")

Less golfed

v=>{
  o=[];
  z=x=>':' + `0${x}`.slice(-2);
  for(m = 60; m--;)
    for(s = 60; s--; )
      h = v - m - s,
      h >= 0 & h < 24 && o.push(h + z(m) + z(s))
  return o
}

Test

F=
v=>eval("o=[];z=x=>':'+`0${x}`.slice(-2);for(m=60;m--;)for(s=60;s--;h>=0&h<24&&o.push(h+z(m)+z(s)))h=v-m-s;o")

function update() {
  O.textContent=F(+I.value).join`\n`
}

update()
<input id='I' value=119 type=number min=0 max=141 oninput='update()'><pre id=O></pre>


0

JavaScript, 96 bytes

v=>{for (i=86399;i;[a,b,c]=[i/3600|0,i%3600/60|0,i--%60]){v-a-b-c?0:console.log(a+":"+b+":"+c)}}

Expanded view:

v => {
    for (i = 86399; i;
        [a, b, c] = [i / 3600 | 0, i % 3600 / 60 | 0, i-- % 60]) {
        v - a - b - c ? 0 : console.log(a + ":" + b + ":" + c)
    }
}

Loop through all possible times by looping 86399 to 1,

  • convert the integer to time by dividing by 3600 to get the first digit
  • the 2nd digit by taking the integer mod 3600 and then dividing by 60
  • and the last digit is the integer mod 60

Subtract all 3 numbers from the input value to return a falsey value if the three numbers add up to the input value. If the value is falsey, output the value.


0

bash, 78 bytes (using a BSD utility) or 79 bytes (non-BSD also)

This is a little longer than @DigitalTrauma and @hvd's nice 71-byte bash solution, but I kind of liked the idea here of using numbers in base 60; I'm curious if anybody can golf this down a little more.

With the BSD-standard jot utility:

jot '-wx=`dc<<<60do3^%d+n`;((`dc<<<$x++'$1'-n`))||tr \  :<<<${x:3}' 86400 0|sh

With the more universally available seq utility:

seq '-fx=`dc<<<60do3^%.f+n`;((`dc<<<$x++'$1'-n`))||tr \  :<<<${x:3}' 0 86399|sh

The idea is to generate the numbers from 0 to 83699 and use dc to convert them into base 60. The "digits" in dc's base-60 output are 2-digit numbers from 00 to 59, with spaces separating the "digits", so this lists all the desired times from 00 00 00 to 23 59 59 in almost the format needed.

If you literally carry that out, though, numbers below 60^2 aren't 3-digit numbers in base 60, so the initial 00 or 00 00 is missing. For that reason, I'm actually generating the numbers from 60^3 to 60^3+83699; this ensures that all the numbers generated are exactly 4 digits long in base 60. This is OK as long as I eventually throw away the extra first digit (01) that isn't needed.

So, once the desired times are generated, I just take each quadruple from 01 00 00 00 to 01 23 59 59, add the last three numbers and subtract the argument $1. If that's 0, I then take everything in the quadruple from the 3rd character on (throwing away the "01 "), use tr to convert spaces to colons, and print the result.


0

PowerShell, 91 97 bytes (including input)

param($x)1..864e3|%{($d=date($_*1e7l))}|?{("{0:H+m+s}"-f$_|iex)-eq$x}|%{"{0:H:mm:ss}"-f$_}

param($x)0..23|%{$h=$_;0..59|%{$m=$_;0..59|?{$h+$m+$_-eq$x}|%{"{0:0}:{1:00}:{2:00}"-f$h,$m,$_}}}

or

param($x)0..23|%{$h=$_;0..59|?{($s=$x-$h-$_)-le59-and$s-ge0}|%{"{0:0}:{1:00}:{2:00}"-f$h,$_,$s}} <\s>

Expanded & commented

param($x)
#loop through the hours
0..23 | %{
    $h=$_
    #loop through the minutes
    0..59 | %{
        $m=$_
        #loop through the seconds
        0..59 | ?{ #filter for those where the sum matches the target
            $h + $m + $_ -eq $x
        } | %{
            #format the result
            "{0:#0}:{1:00}:{2:00}" -f $h, $m, $_
        }
    }
}

NB: Surpassed by @Briantist's version: /codegolf//a/105163/6776

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.