Averages/Mean time of day: Difference between revisions
m
→{{header|Uiua}}: small tweak
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
m (→{{header|Uiua}}: small tweak) |
||
(7 intermediate revisions by 7 users not shown) | |||
Line 228:
<pre>
% ./mean_time_of_day 23:00:17 23:40:20 00:12:45 00:17:19
23:47:43
</pre>
=={{header|ALGOL 68}}==
Uses code from the Averages/Mean angle task, included here for convenience.
<syntaxhighlight lang="algol68">
BEGIN # Mean time of day mapping time to angles #
# code from the Averages/Mean angle task - angles are in degrees #
PROC mean angle = ([]REAL angles)REAL:
(
INT size = UPB angles - LWB angles + 1;
REAL y part := 0, x part := 0;
FOR i FROM LWB angles TO UPB angles DO
x part +:= cos (angles[i] * pi / 180);
y part +:= sin (angles[i] * pi / 180)
OD;
arc tan2 (y part / size, x part / size) * 180 / pi
);
# end code from the Averages/Mean angle task #
MODE TIME = STRUCT( INT hh, mm, ss );
OP TOANGLE = ( TIME t )REAL: ( ( ( ( ( ss OF t / 60 ) + mm OF t ) / 60 ) + hh OF t ) * 360 ) / 24;
OP TOTIME = ( REAL a )TIME:
BEGIN
REAL t := ( a * 24 ) / 360;
WHILE t < 0 DO t +:= 24 OD;
WHILE t > 24 DO t -:= 24 OD;
INT hh = ENTIER t;
t -:= hh *:= 60;
INT mm = ENTIER t;
INT ss = ENTIER ( ( t - mm ) * 60 );
( hh, mm, ss )
END # TOTIME # ;
PROC mean time = ( []TIME times )TIME:
BEGIN
[ LWB times : UPB times ]REAL angles;
FOR i FROM LWB times TO UPB times DO angles[ i ] := TOANGLE times[ i ] OD;
TOTIME mean angle( angles )
END # mean time # ;
OP SHOW = ( TIME t )VOID:
BEGIN
PROC d2 = ( INT n )STRING: IF n < 10 THEN "0" ELSE "" FI + whole( n, 0 );
print( ( d2( hh OF t ), ":", d2( mm OF t ), ":", d2( ss OF t ) ) )
END # show time # ;
SHOW mean time( ( ( 23,00,17 ), ( 23,40,20 ), ( 00,12,45 ), ( 00,17,19 ) ) )
END
</syntaxhighlight>
{{out}}
<pre>
23:47:43
</pre>
Line 407 ⟶ 462:
using System.Linq;
using System.Text;
using static System.Math;
namespace RosettaCode;
class Program
{
private const int SecondsPerDay = 60 * 60 * 24;
static void Main()
{
var digitimes = new List<TimeSpan>();
while
string input
if (string.IsNullOrWhiteSpace(input)) break;
if (TimeSpan.TryParse(input, out var digitime))
digitimes.Add(digitime);
} else {
Console.WriteLine("Seems this is wrong input: ignoring time");
}
}
if(digitimes.Count() > 0)
Console.WriteLine($"The mean time is : {MeanTime(digitimes)}");
}
public static TimeSpan MeanTime(IEnumerable<TimeSpan> ts) => FromDegrees(MeanAngle(ts.Select(ToDegrees)));
public static double ToDegrees(TimeSpan ts) => ts.TotalSeconds * 360d / SecondsPerDay;
public static TimeSpan FromDegrees(double degrees) => TimeSpan.FromSeconds((int)(degrees * SecondsPerDay / 360));
public static double MeanAngle(IEnumerable<double> angles)
var x = angles.Average(a
var y = angles.Average(a => Sin(a * PI
return (Atan2(y, x) * 180 / PI +
}
}
Line 692 ⟶ 733:
readln;
end.</syntaxhighlight>
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang=easylang>
func tm2deg t$ .
t[] = number strsplit t$ ":"
return 360 * t[1] / 24.0 + 360 * t[2] / (24 * 60.0) + 360 * t[3] / (24 * 3600.0)
.
func$ deg2tm deg .
len t[] 3
h = floor (24 * 60 * 60 * deg / 360)
t[3] = h mod 60
h = h div 60
t[2] = h mod 60
t[1] = h div 60
for h in t[]
if h < 10
s$ &= 0
.
s$ &= h
s$ &= ":"
.
return substr s$ 1 8
.
func mean ang[] .
for ang in ang[]
x += cos ang
y += sin ang
.
return atan2 (y / len ang[]) (x / len ang[])
.
in$ = "23:00:17 23:40:20 00:12:45 00:17:19"
for s$ in strsplit in$ " "
ar[] &= tm2deg s$
.
print deg2tm (360 + mean ar[])
</syntaxhighlight>
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
Line 716 ⟶ 794:
→ "23:47:43"
</syntaxhighlight>
=={{header|Erlang}}==
Line 2,216 ⟶ 2,293:
{{out}}
<pre>23:47:43</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ → angles
≪ 0 1 angles SIZE '''FOR''' j
1 angles j GET R→C P→R + '''NEXT'''
angles SIZE / ARG
≫ ≫ ''''MEANG'''' STO
≪ → times
≪ { } 1 times SIZE '''FOR''' j
1 times j GET HMS→ 15 * + '''NEXT'''
'''MEANG''' 15 / 24 MOD →HMS 4 FIX RND STD
≫ ≫ ''''MTIME'''' STO
{23.0017 23.4020 0.1245 0.1719 } '''MTIME'''
{{out}}
<pre>
1: 23.4743
</pre>
=={{header|Ruby}}==
Line 2,584 ⟶ 2,681:
{{out}}
23:47:43
=={{header|Uiua}}==
Not much native support for trig in Uiua, so much of the code is just setting that up.
<syntaxhighlight lang="uiua">
# Find mean time of day using 'mean angle'
ParseTS ← setinv(/(+×60)⊜⋕≠@:.|/(◇⊂◇⊂)@:≡(□↙¯2⊂"00"°⋕)⌊[⍥(⊃÷◿60)2])
RpS ← ×π÷180÷240 1 # Radians per second
SpD ← ×× 24 60 60 # Seconds per day
Cos ← setinv(∿+η|-:η°∿)
# Polar to (complex) cartesian, and inverse.
PtoC ← setinv(
ℂ⍜⊟×⊃∿Cos:°⊟
| √+∩(×.),⟜:°ℂ
⍤("undefined for r=0")≠0.
⊟⟜(⍥¯<0:°Cos÷)
)
Ts ← "23:00:17, 23:40:20, 00:12:45, 00:17:19"
⊜(PtoC ⊟1 ×RpS ParseTS)¬⦷", ".Ts # Get TSs as unit complex numbers.
⁅÷RpS⊡1°PtoC÷⊃(⧻|/+) # Average them and convert back to seconds.
°ParseTS+×SpD<0. # Ensure its >0, format as TS.
</syntaxhighlight >
{{out}}
<pre>
"23:47:43"
</pre>
=={{header|VBA}}==
Line 2,654 ⟶ 2,779:
The mean time is : 23:47:43</pre>
=={{header|V (Vlang)}}==
{{trans|Wren}}
<syntaxhighlight lang="v (vlang)">import math
const inputs = ["23:00:17", "23:40:20", "00:12:45", "00:17:19"]
Line 2,703 ⟶ 2,828:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var timeToDegs = Fn.new { |time|
Line 2,720 ⟶ 2,845:
s = m % 60
m = (m / 60).floor
return
}
|