Cheryl's birthday: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (→‎{{header|Haskell}}: Reduced <$> to .)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 787:
3 remaining.
Jul, 16</pre>
 
=={{header|C sharp}}==
<lang csharp>public static class CherylsBirthday
{
public static void Main() {
var dates = new HashSet<(string month, int day)> {
("May", 15),
("May", 16),
("May", 19),
("June", 17),
("June", 18),
("July", 14),
("July", 16),
("August", 14),
("August", 15),
("August", 17)
};
 
Console.WriteLine(dates.Count + " remaining.");
//The month cannot have a unique day.
var monthsWithUniqueDays = dates.GroupBy(d => d.day).Where(g => g.Count() == 1).Select(g => g.First().month).ToHashSet();
dates.RemoveWhere(d => monthsWithUniqueDays.Contains(d.month));
Console.WriteLine(dates.Count + " remaining.");
//The day must now be unique.
dates.IntersectWith(dates.GroupBy(d => d.day).Where(g => g.Count() == 1).Select(g => g.First()));
Console.WriteLine(dates.Count + " remaining.");
//The month must now be unique.
dates.IntersectWith(dates.GroupBy(d => d.month).Where(g => g.Count() == 1).Select(g => g.First()));
Console.WriteLine(dates.Single());
}
}</lang>
{{out}}
<pre>
10 remaining.
5 remaining.
3 remaining.
(July, 16)
</pre>
 
=={{header|C++}}==
Line 903 ⟶ 942:
{{out}}
<pre>Cheryl's birthday is Jul 16</pre>
 
=={{header|C sharp}}==
<lang csharp>public static class CherylsBirthday
{
public static void Main() {
var dates = new HashSet<(string month, int day)> {
("May", 15),
("May", 16),
("May", 19),
("June", 17),
("June", 18),
("July", 14),
("July", 16),
("August", 14),
("August", 15),
("August", 17)
};
 
Console.WriteLine(dates.Count + " remaining.");
//The month cannot have a unique day.
var monthsWithUniqueDays = dates.GroupBy(d => d.day).Where(g => g.Count() == 1).Select(g => g.First().month).ToHashSet();
dates.RemoveWhere(d => monthsWithUniqueDays.Contains(d.month));
Console.WriteLine(dates.Count + " remaining.");
//The day must now be unique.
dates.IntersectWith(dates.GroupBy(d => d.day).Where(g => g.Count() == 1).Select(g => g.First()));
Console.WriteLine(dates.Count + " remaining.");
//The month must now be unique.
dates.IntersectWith(dates.GroupBy(d => d.month).Where(g => g.Count() == 1).Select(g => g.First()));
Console.WriteLine(dates.Single());
}
}</lang>
{{out}}
<pre>
10 remaining.
5 remaining.
3 remaining.
(July, 16)
</pre>
 
=={{header|Common Lisp}}==
Line 1,705:
my ($m, $d) = split '-', $dates[0];
print "Cheryl's birthday is $months[$m] $d.\n";</lang>
{{out}}
<pre>Cheryl's birthday is July 16.</pre>
 
=={{header|Perl 6}}==
 
<lang perl6>my @dates =
{ :15day, :5month },
{ :16day, :5month },
{ :19day, :5month },
{ :17day, :6month },
{ :18day, :6month },
{ :14day, :7month },
{ :16day, :7month },
{ :14day, :8month },
{ :15day, :8month },
{ :17day, :8month }
;
 
# Month can't have a unique day
my @filtered = @dates.grep(*.<month> != one(@dates.grep(*.<day> == one(@dates».<day>))».<month>));
 
# Day must be unique and unambiguous in remaining months
my $birthday = @filtered.grep(*.<day> == one(@filtered».<day>)).classify({.<month>})\
.first(*.value.elems == 1).value[0];
 
# convenience array
my @months = <'' January February March April May June July August September October November December>;
 
say "Cheryl's birthday is { @months[$birthday<month>] } {$birthday<day>}.";</lang>
{{out}}
<pre>Cheryl's birthday is July 16.</pre>
Line 1,923 ⟶ 1,894:
'((July 16))
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<lang perl6>my @dates =
{ :15day, :5month },
{ :16day, :5month },
{ :19day, :5month },
{ :17day, :6month },
{ :18day, :6month },
{ :14day, :7month },
{ :16day, :7month },
{ :14day, :8month },
{ :15day, :8month },
{ :17day, :8month }
;
 
# Month can't have a unique day
my @filtered = @dates.grep(*.<month> != one(@dates.grep(*.<day> == one(@dates».<day>))».<month>));
 
# Day must be unique and unambiguous in remaining months
my $birthday = @filtered.grep(*.<day> == one(@filtered».<day>)).classify({.<month>})\
.first(*.value.elems == 1).value[0];
 
# convenience array
my @months = <'' January February March April May June July August September October November December>;
 
say "Cheryl's birthday is { @months[$birthday<month>] } {$birthday<day>}.";</lang>
{{out}}
<pre>Cheryl's birthday is July 16.</pre>
 
=={{header|Scala}}==
Line 2,176 ⟶ 2,177:
End Sub</lang>{{out}}
<pre>July 16</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
10,333

edits