Condition

Given the number n. N minutes have passed since the beginning of the day. Determine how many hours and minutes the electronic clock will show at that moment. The program should display two numbers: the number of hours (from 0 to 23) and the number of minutes (from 0 to 59). Note that the number n can be more than the number of minutes in a day.

The essence of the competition is to solve this problem without using a semicolon (or other command delimiters)

Examples of input and corresponding output (result):

In [38]: [print('%s: %s %s'%(x, *divmod(x%1440,60))) ...: for x in [3, 150, 1441, 444, 180, 1439, 1440, 2000, 3456, 5678, 9876]] ...: 3: 0 3 150: 2 30 1441: 0 1 444: 7 24 180: 3 0 1439: 23 59 1440: 0 0 2000: 9 20 3456: 9 36 5678: 22 38 9876: 20 36 

PS lambda functions can be used!

PPS task was originally designed to solve in Python, so do not judge too ...

To create a table, please fill out the response header correctly.

Leaderboard:

 execute("ru.stackoverflow.com", "620564"); 
 .cssload-container,.cssload-cube{width:97px;height:97px;transform-style:preserve-3d}.cssload-container,.cssload-cube,.cssload-half1,.cssload-half2{transform-style:preserve-3d}.cssload-container{position:relative;margin:23px 84px;perspective:292px}.cssload-cube{animation:cube 11.5s forwards infinite;transform-origin:center 49px}.cssload-half1,.cssload-s1{top:0;transform-origin:50% 100%}.cssload-half1{height:39px;position:absolute;animation:half-fold 11.5s forwards infinite}.cssload-side{width:19px;height:19px;background:#ddd;position:absolute}.cssload-s1{left:39px;animation:s1ani 11.5s forwards infinite}.cssload-s2,.cssload-s3,.cssload-s4{left:39px;transform-origin:50% 0}.cssload-s2{top:19px;animation:s2ani 11.5s forwards infinite}.cssload-s3{top:39px;animation:s3ani 11.5s forwards infinite}.cssload-s4{top:58px;animation:s4ani 11.5s forwards infinite}.cssload-s5{left:19px;top:19px;transform-origin:100% 50%;animation:s5ani 11.5s forwards infinite}.cssload-s6{left:58px;top:39px;transform-origin:0 50%;animation:s6ani 11.5s forwards infinite}@keyframes cube{0%,30%{transform:rotateX(0)}40%{transform:rotateX(45deg) rotateY(0) rotate(45deg)}60%{transform:rotateX(60deg) rotateY(0) rotate(45deg)}65%,70%{transform:rotateX(60deg) rotate(45deg) rotate(180deg)}75%,80%{transform:rotateX(60deg) rotate(45deg) rotate(1turn)}90%{transform:rotateX(0) rotate(0) rotate(0)}}@keyframes s1ani{0%{opacity:1;transform:translateY(0);background:#ddd}40%{transform:rotateX(0);background:#ddd}50%{transform:rotateX(-90deg);background:#ddd}90%{transform:rotateX(-90deg)}}@keyframes s2ani{0%{opacity:0;transform:rotateX(-179deg)}10%{opacity:1;transform:rotateX(0)}40%{background:#ddd}45%,80%{background:#b4b4b4}65%{opacity:1;background:#b4b4b4}90%{opacity:1}to{opacity:0}}@keyframes s3ani{0%,10%{opacity:0;transform:rotateX(-179deg)}20%,90%{opacity:1;transform:rotateX(0)}40%{background:#ddd}45%{background:#969696}to{opacity:0}}@keyframes s4ani{0%,20%{opacity:0;transform:rotateX(-179deg)}10%,to{opacity:0}30%{opacity:1;transform:rotateX(0)}40%{transform:rotateX(0);background:#ddd}50%{transform:rotateX(90deg);background:#b4b4b4}80%{background:#b4b4b4}90%{opacity:1;transform:rotateX(90deg)}}@keyframes s5ani{0%,10%{opacity:0;transform:rotateY(-179deg)}20%{opacity:1;background:#ddd;transform:rotateY(0)}40%{transform:rotateY(0)}50%{transform:rotateY(90deg)}55%{background:#ddd}60%{background:#c8c8c8}90%{transform:rotateY(90deg);opacity:1}to{opacity:0}}@keyframes s6ani{0%,20%{opacity:0;transform:rotateY(179deg)}30%{opacity:1;transform:rotateY(0)}40%{transform:rotateY(0)}50%{transform:rotateY(-90deg);background:#ddd}60%,80%{background:#c8c8c8}90%{opacity:1;transform:rotateY(-90deg)}to{opacity:0}}@keyframes half-fold{0%,50%{transform:rotateX(0)}60%,90%{transform:rotateX(-90deg)}} 
 <script src="https://mayorovp.imtqy.com/codegolf/table-8c505e68f1349e4c69e7.js"></script> <div class=cssload-container><div class=cssload-cube><div class=cssload-half1><div class="cssload-side cssload-s1"></div><div class="cssload-side cssload-s2"></div><div class="cssload-side cssload-s5"></div></div><div class=cssload-half2><div class="cssload-side cssload-s3"></div><div class="cssload-side cssload-s4"></div><div class="cssload-side cssload-s6"></div></div></div></div> 

  • 2
    Is the "command" statement ( single_input or only small_stmt ) in Python? - jfs
  • @jfs, I don’t even know how to formulate correctly - MaxU
  • it is possible to confine with one expression (the fact that inside lambda can be placed ( test ), banning lambda themselves). Or the traditional condition: the smallest number of bytes (source code in utf-8) wins. - jfs
  • Input data counts? And in what form are they? input() is a python construction, in other languages ​​it is not. - user207618
  • 3
    It seems to me that in si-like languages ​​this is not solvable, since you must at least declare a variable, which means the separator must be required. - αλεχολυτ

13 replies 13

Python 2, 12 bytes

 n/60%24,n%60 

One expression for a given number of minutes n returns a tuple (часы, минуты) . Example:

 >>> n = 123400 >>> n/60%24,n%60 (16, 40) 

Two required numbers are present in the output. For check:

 >>> from datetime import timedelta >>> str(timedelta(minutes=n)) '85 days, 16:40:00' 

It can be seen that 16:40 is the correct result.

If n only can be used , then divmod(n%1440,60) (17 bytes). To print without REPL , you can add print . If the input is not in n , but in stdin, then to read: input() instead of n substitute:

Python 2, 29 bytes

 print divmod(input()%1440,60) 

Displays: (16, 40) for 123400 input. To print only numbers:

Python 3, 36 bytes

 print(*divmod(int(input())%1440,60)) 

Conclusion 16 40 .

Since the arrows go around in a circle, the task is ideal for modulo division:

  • n % 60 - find the position of the minute hand (minutes in the last hour)
  • n // 60 - turns the clock into hours, and % 24 then finds the position of the hour hand (the clock in the last 24 hours)
  • n % 1440 before the last day cuts: 1440 == (24 * 60)
  • divmod(m, 60) for integers combines m // 60, m % 60 :

     hours, minutes = divmod(m, 60) # == (m // 60, m % 60) 
  • mda, print(*divmod(int(input())%1440,60)) - it will be hard to beat it in Python 3 - MaxU
  • ">>> n = 3 >>> n / 60% 24, n% 60 (0, 3)" on which hours can you see this? Obviously, there must be something like “00:03”, or, at worst, “00 03”, but not raw “(0, 3)” - edem
  • @edem: read the question: "The program should print two numbers: the number of hours (from 0 to 23) and the number of minutes (from 0 to 59)" Look at least that the author of the question returns. If you do not agree, do not understand the question, then ask the author for clarification. - jfs
  • Tyuyu, for sure, you are right, then +1. - edem
  • @jfs, added to the question - MaxU

Python 3, 43 52 60 characters

My decision:

43 characters:

 print('%s %s'%divmod(int(input())%1440,60)) 

52 characters:

 print('{} {}'.format(*divmod(int(input())%1440,60))) 

Brief explanation:

The divmod (a, b) function returns as a result two values ​​(a tuple) - the result of integer division of a by b ( a // b ) and the remainder of dividing a by b ( a % b ):

 In [2]: divmod(65, 60) Out[2]: (1, 5) In [3]: divmod(1441%1440, 60) Out[3]: (0, 1) 
  • In golf, it is accepted: 1) To give explanations of how the competitive decision works and 2) Well ... you need to rank the results, probably. - user207618
  • @Other well, ranking by the number of characters probably not? And probably in vain you have removed the python tag, then the question will be what is one command in C ++, separated by commas for example. - pavel
  • Traditionally, a measure of measurement is assigned by the author. Like any restrictions. - user207618
  • Wow, I didn't know about divmod) - andreymal
  • Since this is golf, the language should not matter if it is not excluded in the conditions. So ... - user207618

Bash, 24 characters

(I don’t know how much this corresponds to tag, but let it be)

 date -d"0+$n min" +%H:%M 

Check:

 $ for n in 3 150 1441 444 180 1439 1440 2000 3456 5678 9876 > do > date -d"0+$n min" +%H:%M > done 00:03 02:30 00:01 07:24 03:00 23:59 00:00 09:20 09:36 22:38 20:36 
  • Normally, consistent. Code golf, which is also a competition , implies the use of any language, unless the restrictions are explicitly indicated. - Nick Volynkin

PHP, 25 characters

 echo gmdate('H:i',$n*60); 

Sandbox

  • And if echo replaced with = , then even less)))) - AK
  • @jfs, then you need to add 3 more characters. - Visman

C #, 24 with crutches and 51 characters if correct

You can give direct access to the Console class program:

 using static System.Console; 

and immediately on the screen in the time format you need (51 characters)

 Write(TimeSpan.FromMinutes(n).ToString("hh\\:mm")); 

IMPORTANT: The code itself will say what it does. Even a person who is far from programming.


More shortly but through the backside (24 characters):

 Write(n/60%24+":"+n%60); 

The code is badly readable. Even an experienced programmer will have to think for a while to understand what is going on here.


Conclusion: This is a great example showing that in programming "shorter" does not always mean "better."

  • "And in Ruby and Swift there are no dividing commands" - in Ruby everything is there, so what. Both zapyatochiye, and the character of line feed usually working instead of it (about what not everyone knows and occasionally get into interesting situations ). - D-side
  • hmm, yes, you are right. The carriage .... Well, it's definitely not the case in the swift) - Andrew
  • And in the swift, too, the same :) There, too, the "carriage" works instead of the cubicle most of the time. - D-side
  • I remembered how the code got messed up when I translated a string in the wrong place .. You are absolutely right! Thanks for editing. - Andrew
  • TimeSpan.FromMinutes (n) is a full-fledged version, it prints to the console when launched from interactive (the options for ruby ​​and python are also calculated for interactive). So 23 characters in total. - PashaPash

Java, 37 with crutches and 54 characters if correct

If you add a static import

 import static java.lang.System.out; import static java.time.LocalTime.MIDNIGHT; 

not to write the name of the class, you can write like this:

out.println(MIDNIGHT.plusMinutes(n));

54 character full answer

 System.out.println(LocalTime.MIDNIGHT.plusMinutes(n)); 

Also available in 90 characters

 System.out.println(DateTime.now().withTimeAtStartOfDay().plusMinutes(n).toString("HH:mm")); 

    JS, 38 characters

    Function:

     m=>console.log(`${m/60%24|0}:${m%60}`) 

    Example output:

     (m=>console.log(`${m/60%24|0}:${m%60}`))(5) // 0:5 

      Perl, 43 characters (for a complicated version of the problem)

      In the current form, the task is a simple derivation of the result of dividing numbers by module, and the differences for programming languages ​​when solving it, only in the way of outputting these same results, are no more. The method of calculation itself is relatively standard, as can be seen in most answers: $n/60%24 for hours and for minutes $n%60 .

      In the light of the above, I provide a solution for a complicated version of this task, as if the output of the program were identical to a real electronic clock, for example: "00:13", "23:48", etc.

       ((($m/60%24).':'.$m%60)=~s/(\b\d\b)/0$1/gr) 

      Check:

       print((($_/60%24).':'.$_%60)=~s/(\b\d\b)/0$1/gr) for (3, 150, 1441, 444, 180, 1439, 1440, 2000, 3456, 5678, 9876); 00:03 02:30 00:01 07:24 03:00 23:59 00:00 09:20 09:36 22:38 20:36 

      Option without zero for hours, 45 characters

       ((($m/60%24).':'.$m%60)=~s/:(\b\d\b)/:0$1/gr) 

      Check:

       perl -E 'say((($_/60%24).":".$_%60)=~s/:(\b\d\b)/:0$1/gr) for (3, 150, 1441, 444, 180, 1439, 1440, 2000, 3456, 5678, 9876)' 0:03 2:30 0:01 7:24 3:00 23:59 0:00 9:20 9:36 22:38 20:36 
      • 1- Show how to run the code (which flags to specify). 2- real electronic clock other numbers use . For example: watch -n1 "date +%T | figlet -k -f clr4x6" - jfs
      • @jfs added ... - edem
      • one
        I did not quite understand why such difficulties, if there is *printf , and shorter: sprintf'%02d:%02d',$m/60%24,$m%60 - 32 bytes (we get the string) or printf'%02d:%02d',$m/60%24,$m%60 (immediately output ) - PinkTux
      • @PinkTux did not want to use the formatting features. - edem

      PHP, 23 characters

       echo$n/60%24,':',$n%60; 

      PS Yes, you can write without a space -> sandbox

      PPS Formula pulled from other answers: P

        Ruby, 15 characters

         Time.gm(0)+n*60 
        • Fixed DST bug with jfs guideline ( new -> utc )
        • 1 character is cut off, also according to a tip from jfs ( utc -> gm )

        Because if we want to show the time , why not take advantage of the class that is responsible for the time?

        Two things are used, hack and not so hack:

        • Hack: Time.gm(0) is a call to the Time constructor with a zero year in the GMT timezone (to avoid summer / winter time special effects or DST). For unspecified components, the default values ​​are taken: for hours and minutes these are zeros. Without arguments, it is impossible, since without them there will be a current time object.
        • Not a very hack: you can add a number to the object of the class Time , and it will be perceived as the number of seconds, therefore it should be multiplied by 60.

        Session from irb with demonstration:

         $ irb 2.3.1 :001 > n = 123400 => 123400 2.3.1 :002 > Time.gm(0)+n*60 => 0000-03-26 16:40:00 +0300 
        • @jfs is a great find! Fixed, the account is the same, I will correct the description a little later. - D-side
        • You can save a symbol: Time.gm(0)+1060354206*60 -> 02:06 - jfs
        • @jfs screwed, thanks. - D-side

        C / C ++, 29 characters

         void f(int n){ printf("%d %d",n/60%24,n%60); } ^ 

        Without this semicolon, neither in C nor in C ++ is simply possible. Grammar will not allow.

        But you can just one output statement (formally passes):

         printf("%d %d",n/60%24,n%60); 
        • And where is the input? I have already tried to post it like this (but with input), but deleted it (you should see). - αλεχολυτ
        • And we are not told that it should be considered. We are told - literally - Given the number n . Those. the variable is already there, declared, the value is assigned. The solution could even be considered printf("%d %d",n/60%24,n%60) ... - Harry
        • Please make the title in a format in which the results table can understand it :) - Pavel Mayorov
        • It is written only "To create a table, please fill out the title of the answer." , but how exactly is it not written ... - Harry
        • judging by the code: /<h[12]>\s*?([^<,]+),\s*?(\d+)/ , the first-second level header ( # , ## ) of the form is enough: <Язык>, <число> - jfs

        Python, 56 characters

        Though I am still weak in python, I also want to stick my nose:

         n=int(input()); ch=(n//60); min=(n-ch*60); print(ch,min) 

        Let not even one team, but for beginners will go to figure out what is at stake.

        • But you yourself wrote in your question that this decision is wrong - andreymal
        • This is not one team. - user207618
        • @Other: in Python this is one command ( single_input ) - jfs
        • "without a semicolon (or other command delimiters)" - Andrew

        49 characters PowerShell

        [DateTime]::Today.addminutes(1).ToString("HH:mm")

        [DateTime]::Today - returns us time at 00:00 addminutes() - adds to the time minutes ToString("HH:mm") - output in the format, you can still ToShortTimeString()

         PS C:\> @(3, 150, 1441, 444, 180, 1439, 1440, 2000, 3456, 5678, 9876) | foreach{[DateTime]::Today.addminutes($_).ToString("HH:mm")} 00:03 02:30 00:01 07:24 03:00 23:59 00:00 09:20 09:36 22:38 20:36