Jump to content

Averages/Mean time of day: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 215:
The mean time is : 23:47:43</pre>
----
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RosettaCode
{
class Program
{
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 (int i = 0; i < numItems; i++)
{
x_part += Math.Cos(angles[i] * Math.PI / 180);
y_part += Math.Sin(angles[i] * Math.PI / 180);
}
 
return Math.Atan2(y_part / numItems, x_part / numItems) * 180 / Math.PI;
};
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;
 
Console.WriteLine("Enter times, end with no input: ");
do
{
input = Console.ReadLine();
if (!(string.IsNullOrWhiteSpace(input)))
{
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)));
}
}
}
</lang>
{{out}}
<pre>
Enter times, end with no input:
23:00:17
23:40:20
00:12:45
00:17:19
 
The mean time is : 23:47:43
</pre>
 
=={{header|C++}}==
Line 285 ⟶ 353:
{{out}}
<pre>23:47:43</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RosettaCode
{
class Program
{
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 (int i = 0; i < numItems; i++)
{
x_part += Math.Cos(angles[i] * Math.PI / 180);
y_part += Math.Sin(angles[i] * Math.PI / 180);
}
 
return Math.Atan2(y_part / numItems, x_part / numItems) * 180 / Math.PI;
};
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;
 
Console.WriteLine("Enter times, end with no input: ");
do
{
input = Console.ReadLine();
if (!(string.IsNullOrWhiteSpace(input)))
{
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)));
}
}
}
</lang>
{{out}}
<pre>
Enter times, end with no input:
23:00:17
23:40:20
00:12:45
00:17:19
 
The mean time is : 23:47:43
</pre>
 
=={{header|D}}==
Line 397:
{{out}}
<pre>23:47:43</pre>
 
 
=={{header|EchoLisp}}==
Line 462 ⟶ 461:
The mean time of ["23:00:17","23:40:20","00:12:45","00:17:19"] is: "23:47:43"
</pre>
 
 
 
=={{header|Euphoria}}==
Line 1,184 ⟶ 1,181:
is 23:47:43
</pre>
 
 
=={{header|Lua}}==
Line 1,490 ⟶ 1,486:
{{out}}
<pre>23:47:43 is the mean time of 23:00:17 23:40:20 00:12:45 00:17:19</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2015.12}}
 
<lang perl6>sub tod2rad($_) { [+](.comb(/\d+/) Z* 3600,60,1) * tau / 86400 }
sub rad2tod ($r) {
my $x = $r * 86400 / tau;
(($x xx 3 Z/ 3600,60,1) Z% 24,60,60).fmt('%02d',':');
}
sub phase ($c) { $c.polar[1] }
sub mean-time (@t) { rad2tod phase [+] map { cis tod2rad $_ }, @t }
 
my @times = ["23:00:17", "23:40:20", "00:12:45", "00:17:19"];
say "{ mean-time(@times) } is the mean time of @times[]";</lang>
 
{{out}}
<pre>
23:47:43 is the mean time of 23:00:17 23:40:20 00:12:45 00:17:19
</pre>
 
=={{header|Phix}}==
Line 1,800 ⟶ 1,773:
<pre>"23:47:43"</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
 
<lang perl6>sub tod2rad($_) { [+](.comb(/\d+/) Z* 3600,60,1) * tau / 86400 }
sub rad2tod ($r) {
my $x = $r * 86400 / tau;
(($x xx 3 Z/ 3600,60,1) Z% 24,60,60).fmt('%02d',':');
}
sub phase ($c) { $c.polar[1] }
sub mean-time (@t) { rad2tod phase [+] map { cis tod2rad $_ }, @t }
 
my @times = ["23:00:17", "23:40:20", "00:12:45", "00:17:19"];
say "{ mean-time(@times) } is the mean time of @times[]";</lang>
 
{{out}}
<pre>
23:47:43 is the mean time of 23:00:17 23:40:20 00:12:45 00:17:19
</pre>
 
=={{header|REXX}}==
Line 2,203 ⟶ 2,199:
{{out}}
23:47:43
 
=={{header|VBA}}==
Uses Excel and [[Averages/Mean_angle#VBA|mean angle]].
<lang vb>Public Sub mean_time()
Dim angles() As Double
s = [{"23:00:17","23:40:20","00:12:45","00:17:19"}]
For i = 1 To UBound(s)
s(i) = 360 * TimeValue(s(i))
Next i
Debug.Print Format(mean_angle(s) / 360 + 1, "hh:mm:ss")
End Sub</lang>{{out}}
<pre>23:47:43</pre>
 
=={{header|XPL0}}==
Line 2,246 ⟶ 2,254:
23:47:43
</pre>
 
=={{header|VBA}}==
Uses Excel and [[Averages/Mean_angle#VBA|mean angle]].
<lang vb>Public Sub mean_time()
Dim angles() As Double
s = [{"23:00:17","23:40:20","00:12:45","00:17:19"}]
For i = 1 To UBound(s)
s(i) = 360 * TimeValue(s(i))
Next i
Debug.Print Format(mean_angle(s) / 360 + 1, "hh:mm:ss")
End Sub</lang>{{out}}
<pre>23:47:43</pre>
 
=={{header|Yabasic}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.