Averages/Mean time of day: Difference between revisions

Added Algol 68
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Added Algol 68)
 
(4 intermediate revisions by 4 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;
class Program
 
static void Main()
{
var digitimes = new List<TimeSpan>();
static void Main(string[] args)
{
Func<TimeSpan, double> TimeToDegrees = (time) =>
360 * time.Hours / 24.0 +
360 * time.Minutes / (24 * 60.0) +
360 * time.Seconds / (24 * 3600.0);
Func<List<double>, double> MeanAngle = (angles) =>
{
double y_part = 0.0d, x_part = 0.0d;
int numItems = angles.Count;
 
for Console.WriteLine(int"Enter itimes, =end 0;with ino <input: numItems"); i++)
while (true) {
string input x_part += MathConsole.CosReadLine(angles[i] * Math.PI / 180);
if (string.IsNullOrWhiteSpace(input)) break;
y_part += Math.Sin(angles[i] * Math.PI / 180);
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)));
return Math.Atan2(y_part / numItems, x_part / numItems) * 180 / Math.PI;
public static double ToDegrees(TimeSpan ts) => ts.TotalSeconds * 360d / SecondsPerDay;
};
public static TimeSpan FromDegrees(double degrees) => TimeSpan.FromSeconds((int)(degrees * SecondsPerDay / 360));
Func<double, TimeSpan> TimeFromDegrees = (angle) =>
new TimeSpan(
(int)(24 * 60 * 60 * angle / 360) / 3600,
((int)(24 * 60 * 60 * angle / 360) % 3600 - (int)(24 * 60 * 60 * angle / 360) % 60) / 60,
(int)(24 * 60 * 60 * angle / 360) % 60);
List<double> digitimes = new List<double>();
TimeSpan digitime;
string input;
 
public static double MeanAngle(IEnumerable<double> angles)
Console.WriteLine("Enter times, end with no input: ");
do{
var x = angles.Average(a {=> Cos(a * PI / 180));
var y = angles.Average(a => Sin(a * PI input/ = Console.ReadLine(180));
return (Atan2(y, x) * 180 / PI + if360) (!(string.IsNullOrWhiteSpace(input)))% 360;
{
if (TimeSpan.TryParse(input, out digitime))
digitimes.Add(TimeToDegrees(digitime));
else
Console.WriteLine("Seems this is wrong input: ignoring time");
}
} while (!string.IsNullOrWhiteSpace(input));
 
if(digitimes.Count() > 0)
Console.WriteLine("The mean time is : {0}", TimeFromDegrees(360 + MeanAngle(digitimes)));
}
}
}
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,703 ⟶ 2,800:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var timeToDegs = Fn.new { |time|
Line 2,720 ⟶ 2,817:
s = m % 60
m = (m / 60).floor
return "%(Fmt.dswrite(2"$2d:$2d:$2d", h)):%(Fmt.d(2, m)):%(Fmt.d(2, s))"
}
 
3,032

edits