Cheryl's birthday: Difference between revisions

Added FreeBASIC
No edit summary
(Added FreeBASIC)
(13 intermediate revisions by 8 users not shown)
Line 27:
* [https://en.wikipedia.org/wiki/Tuple_relational_calculus, Tuple Relational Calculus]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">T Date
String month
Int day
Line 91 ⟶ 90:
 
print()
print(‘So birthday date is ’month‘ ’day‘.’)</langsyntaxhighlight>
 
{{out}}
Line 105 ⟶ 104:
=={{Header|Ada}}==
{{trans|C}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Main is
Line 229 ⟶ 228:
 
print_answer;
end Main;</langsyntaxhighlight>
{{out}}
<pre>
Line 238 ⟶ 237:
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<syntaxhighlight lang="algol68">
BEGIN # Cheryl's birthday puzzle #
 
[ 1 : 4, 1 : 6 ]INT dates # non-zero indicates a possible date #
:= ( ( 0, 15, 16, 0, 0, 19 ) # may #
, ( 0, 0, 0, 17, 18, 0 ) # june #
, ( 14, 0, 16, 0, 0, 0 ) # july #
, ( 14, 15, 0, 17, 0, 0 ) # august #
);
[]STRING month name = ( "May", "June", "July", "August" );
print( ( "Cheryl tells Albert the month and Bernard the day", newline ) );
print( ( "Albert doesn't know the date and knows Bernard doesn't either", newline ) );
FOR d TO 2 UPB dates DO # elimiate the months with unique days #
INT day count := 0;
INT day := 0;
INT month := 0;
FOR m TO 1 UPB dates DO
IF dates[ m, d ] /= 0 THEN
day count +:= 1;
day := dates[ m, d ];
month := m
FI
OD;
IF day count = 1 THEN
print( ( " Eliminating ", month name[ month ], ", ", whole( day, 0 ), "th is unique", newline ) );
FOR p TO 2 UPB dates DO dates[ month, p ] := 0 OD
FI
OD;
print( ( "Bernard now knows the date", newline ) );
FOR d TO 2 UPB dates DO # eliminate the days that aren't unique #
INT day count := 0;
INT day := 0;
INT month := 0;
FOR m TO 1 UPB dates DO
IF dates[ m, d ] /= 0 THEN
day count +:= 1;
day := dates[ m, d ];
month := m
FI
OD;
IF day count > 1 THEN
print( ( " Eliminating ", whole( day, 0 ), "th, it is non-unique", newline ) );
FOR p TO 1 UPB dates DO dates[ p, d ] := 0 OD
FI
OD;
print( ( "Albert now knows the date", newline ) );
FOR m TO 1 UPB dates DO # eliminate months with non-unique days #
INT day count := 0;
INT day := 0;
INT month := 0;
FOR d TO 2 UPB dates DO
IF dates[ m, d ] /= 0 THEN
day count +:= 1;
day := dates[ m, d ];
month := m
FI
OD;
IF day count > 1 THEN
print( ( " Eliminating ", month name[ m ], ", it has multiple days", newline ) );
FOR p TO 2 UPB dates DO dates[ m, p ] := 0 OD
FI
OD;
print( ( "Cheryl's birthday: " ) ); # show the solution(s) #
FOR m TO 1 UPB dates DO
FOR d TO 2 UPB dates DO
IF dates[ m, d ] /= 0 THEN
print( ( " ", month name[ m ], " ", whole( dates[ m, d ], 0 ), "th" ) )
FI
OD
OD;
print( ( newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
Cheryl tells Albert the month and Bernard the day
Albert doesn't know the date and knows Bernard doesn't either
Eliminating June, 18th is unique
Eliminating May, 19th is unique
Bernard now knows the date
Eliminating 14th, it is non-unique
Albert now knows the date
Eliminating August, it has multiple days
Cheryl's birthday: July 16th
</pre>
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 862 ⟶ 947:
filteredArrayUsingPredicate:(ca's ¬
NSPredicate's predicateWithFormat:"0 < length")) as list
end |words|</langsyntaxhighlight>
{{Out}}
<pre>"[('july', '16')]"</pre>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">dates: [
[May 15] [May 16] [May 19]
[June 17] [June 18]
Line 900 ⟶ 984:
print ["\t-> remaining:" dates]
 
print ["\nCheryl's birthday:" first dates]</langsyntaxhighlight>
 
{{out}}
Line 919 ⟶ 1,003:
 
Cheryl's birthday: [July 16]</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">oDates:= {"May" : [ 15, 16, 19]
,"Jun" : [ 17, 18]
,"Jul" : [14, 16]
Line 973 ⟶ 1,056:
else
return m " " obj.1
}</langsyntaxhighlight>
{{out}}
<pre>Jul 16</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f CHERYLS_BIRTHDAY.AWK [-v debug={0|1}]
#
Line 1,064 ⟶ 1,146:
printf("\n\n")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,084 ⟶ 1,166:
07/16
</pre>
 
=={{header|C}}==
{{trans|C#}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 1,212 ⟶ 1,293:
printAnswer();
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>10 remaining.
Line 1,218 ⟶ 1,299:
3 remaining.
Jul, 16</pre>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">public static class CherylsBirthday
{
public static void Main() {
Line 1,249 ⟶ 1,329:
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,257 ⟶ 1,337:
(July, 16)
</pre>
 
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <vector>
Line 1,370 ⟶ 1,449:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Cheryl's birthday is Jul 16</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
;; Author: Amir Teymuri, Saturday 20.10.2018
 
Line 1,405 ⟶ 1,483:
 
(cheryls-birthday *possible-dates*) ;; => ((16 . JULY))
</syntaxhighlight>
</lang>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.algorithm.iteration : filter, joiner, map;
import std.algorithm.searching : canFind;
import std.algorithm.sorting : sort;
Line 1,447 ⟶ 1,524:
// print the result
writeln(birthDay.month, " ", birthDay.day);
}</langsyntaxhighlight>
{{out}}
<pre>jul 16</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
//Find Cheryl's Birthday. Nigel Galloway: October 23rd., 2018
type Month = |May |June |July |August
Line 1,462 ⟶ 1,538:
let _,e = List.concat g |> List.groupBy fst |> fN
printfn "%A" e
</syntaxhighlight>
</lang>
{{out}}
<pre>
[[(July, 16)]]
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs calendar.english fry io kernel prettyprint
sequences sets.extras ;
 
Line 1,494 ⟶ 1,569:
 
! print a date that looks like { { 16 7 } }
first first2 month-name write bl .</langsyntaxhighlight>
{{out}}
<pre>
July 16
</pre>
 
=={{header|FreeBASIC}}==
{{trans|ALGOL 68}}
<syntaxhighlight lang="vbnet">Dim As Integer i, j, contarDias, dia, mes
Dim fechas(1 To 4, 1 To 6) As Integer => {{0, 15, 16, 0, 0, 19}, {0, 0, 0, 17, 18, 0}, {14, 0, 16, 0, 0, 0}, {14, 15, 0, 17, 0, 0}}
Dim nombreMes(1 To 4) As String => {"May", "June", "July", "August"}
 
Print "Cheryl tells Albert the month and Bernard the day"
Print "Albert doesn't know the date and knows Bernard doesn't either"
 
' elimiate the months with unique days
For i = 1 To 6
contarDias = 0
dia = 0
mes = 0
For j = 1 To 4
If fechas(j, i) <> 0 Then
contarDias += 1
dia = fechas(j, i)
mes = j
End If
Next j
If contarDias = 1 Then
Print " Eliminating "; nombreMes(mes); ", "; Str(dia); "th is unique"
For j = 1 To 6
fechas(mes, j) = 0
Next j
End If
Next i
 
Print "Bernard now knows the date"
 
' eliminate the days that aren't unique
For i = 1 To 6
contarDias = 0
dia = 0
mes = 0
For j = 1 To 4
If fechas(j, i) <> 0 Then
contarDias += 1
dia = fechas(j, i)
mes = j
End If
Next j
If contarDias > 1 Then
Print " Eliminating "; Str(dia); "th, it is non-unique"
For j = 1 To 4
fechas(j, i) = 0
Next j
End If
Next i
 
Print "Albert now knows the date"
 
' eliminate months with non-unique days
For i = 1 To 4
contarDias = 0
dia = 0
mes = 0
For j = 1 To 6
If fechas(i, j) <> 0 Then
contarDias += 1
dia = fechas(i, j)
mes = i
End If
Next j
If contarDias > 1 Then
Print " Eliminating "; nombreMes(i); ", it has multiple days"
For j = 1 To 6
fechas(i, j) = 0
Next j
End If
Next i
 
Print "Cheryl's birthday: ";
For i = 1 To 4
For j = 1 To 6
If fechas(i, j) <> 0 Then
Print " "; nombreMes(i); " "; Str(fechas(i, j)); "th"
End If
Next j
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as ALGOL 68 entry.</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,596 ⟶ 1,757:
fmt.Println("Something went wrong!")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,602 ⟶ 1,763:
Cheryl's birthday is July 16
</pre>
=={{header|Fortran}}==
{{trans|C}}
<syntaxhighlight lang="fortran">
program code_translation
implicit none
character(len=3), dimension(13) :: months = ["ERR", "Jan", "Feb", "Mar", "Apr", "May",&
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
type :: Date
integer :: month, day
logical :: active
end type Date
type(Date), dimension(10) :: dates = [Date(5,15,.true.), Date(5,16,.true.), Date(5,19,.true.), &
Date(6,17,.true.), Date(6,18,.true.), &
Date(7,14,.true.), Date(7,16,.true.), &
Date(8,14,.true.), Date(8,15,.true.), Date(8,17,.true.)]
integer, parameter :: UPPER_BOUND = size(dates)
write(*,*) 'possible dates: [[May 15] [May 16] [May 19] [June 17] [June 18] [July 14] [July 16] [August 14] [August 15] [August &
17]]'
write(*,*)
write(*,*) '(1) Albert: I don''t know when Cheryl''s birthday is, but I know that Bernard does not know too.'
write(*,*) ' -> meaning: the month cannot have a unique day'
write(*,*) ' -> remaining: [[July 14] [July 16] [August 14] [August 15] [August 17]] '
write(*,*)
write(*,*) "(2) Bernard: At first I don't know when Cheryl's birthday is, but I know now."
write(*,*) ' -> meaning: the day must be unique'
write(*,*) ' -> remaining: [[July 16] [August 15] [August 17]] '
write(*,*)
write(*,*) '(3) Albert: Then I also know when Cheryl''s birthday is.'
write(*,*) ' -> meaning: the month must be unique'
write(*,*) ' -> remaining: [[July 16]] '
 
call printRemaining()
! the month cannot have a unique day
call firstPass()
call printRemaining()
! the day must now be unique
call secondPass()
call printRemaining()
! the month must now be unique
call thirdPass()
call printAnswer()
 
contains
 
subroutine printRemaining()
integer :: i, c
do i = 1, UPPER_BOUND
if (dates(i)%active) then
write(*,'(a,1x,i0,1x)',advance="no") months(dates(i)%month+1),dates(i)%day
c = c + 1
end if
end do
!
write(*,*)
end subroutine printRemaining
 
subroutine printAnswer()
integer :: i
write(*,'(a)',advance ='no') 'Cheryl''s birtday is on '
do i = 1, UPPER_BOUND
if (dates(i)%active) then
write(*,'(a,1a1,i0)') trim(months(dates(i)%month+1)), ",", dates(i)%day
end if
end do
end subroutine printAnswer
 
subroutine firstPass()
! the month cannot have a unique day
integer :: i, j, c
do i = 1, UPPER_BOUND
c = 0
do j = 1, UPPER_BOUND
if (dates(j)%day == dates(i)%day) then
c = c + 1
end if
end do
if (c == 1) then
do j = 1, UPPER_BOUND
if (.not. dates(j)%active) cycle
if (dates(j)%month == dates(i)%month) then
dates(j)%active = .false.
end if
end do
end if
end do
end subroutine firstPass
 
subroutine secondPass()
! the day must now be unique
integer :: i, j, c
do i = 1, UPPER_BOUND
if (.not. dates(i)%active) cycle
c = 0
do j = 1, UPPER_BOUND
if (.not. dates(j)%active) cycle
if (dates(j)%day == dates(i)%day) then
c = c + 1
end if
end do
if (c > 1) then
do j = 1, UPPER_BOUND
if (.not. dates(j)%active) cycle
if (dates(j)%day == dates(i)%day) then
dates(j)%active = .false.
end if
end do
end if
end do
end subroutine secondPass
 
subroutine thirdPass()
! the month must now be unique
integer :: i, j, c
do i = 1, UPPER_BOUND
if (.not. dates(i)%active) cycle
c = 0
do j = 1, UPPER_BOUND
if (.not. dates(j)%active) cycle
if (dates(j)%month == dates(i)%month) then
c = c + 1
end if
end do
if (c > 1) then
do j = 1, UPPER_BOUND
if (.not. dates(j)%active) cycle
if (dates(j)%month == dates(i)%month) then
dates(j)%active = .false.
end if
end do
end if
end do
end subroutine thirdPass
 
end program code_translation
</syntaxhighlight>
{{out}}
<pre>
possible dates: [[May 15] [May 16] [May 19] [June 17] [June 18] [July 14] [July 16] [August 14] [August 15] [August 17]]
 
(1) Albert: I don't know when Cheryl's birthday is, but I know that Bernard does not know too.
-> meaning: the month cannot have a unique day
-> remaining: [[July 14] [July 16] [August 14] [August 15] [August 17]]
 
(2) Bernard: At first I don't know when Cheryl's birthday is, but I know now.
-> meaning: the day must be unique
-> remaining: [[July 16] [August 15] [August 17]]
 
(3) Albert: Then I also know when Cheryl's birthday is.
-> meaning: the month must be unique
-> remaining: [[July 16]]
May 15 May 16 May 19 Jun 17 Jun 18 Jul 14 Jul 16 Aug 14 Aug 15 Aug 17
Jul 14 Jul 16 Aug 14 Aug 15 Aug 17
Jul 16 Aug 15 Aug 17
Cheryl's birthday is on Jul,16
</Pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">import java.time.Month
 
class Main {
Line 1,678 ⟶ 1,994:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>There are 10 candidates remaining.
Line 1,685 ⟶ 2,001:
There are 1 candidates remaining.
Cheryl's birthday is JULY 16</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
 
import Data.List as L (filter, groupBy, head, length, sortOn)
Line 1,758 ⟶ 2,073:
M.fromList $
((,) . fst . L.head) <*> fmap snd <$>
L.groupBy (on (==) fst) (L.sortOn fst xs)</langsyntaxhighlight>
{{Out}}
<pre>[("July","16")]</pre>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">Dates=: <;._2cutLF noun define
15 May
16 May
Line 1,777 ⟶ 2,091:
)
 
getDayMonth=: |:@:(' '&splitstringcut&>) NB. retrieve lists of days and months from dates
keep=: adverb def '] #~ u' NB. apply mask to filter dates
 
Line 1,787 ⟶ 2,101:
 
uniqueMonth=: ~.@] #~ (1=#)/.~ NB. list of months with 1 unique day
isUniqueMonth=: (] e. uniqueMonth)/@getDayMonth NB. mask of dates with a month that has 1 unique day</langsyntaxhighlight>
'''Usage:'''
<langsyntaxhighlight lang="j"> isUniqueMonth keep isUniqueDayInMonth keep isMonthWithoutUniqueDay keep Dates
+-------+
|16 July|
+-------+</langsyntaxhighlight>
 
===Alternative Approach===
Line 1,798 ⟶ 2,112:
The concepts here are the same, of course, it's just the presentation that's different.
 
<langsyntaxhighlight Jlang="j">possible=: cut;._2 'May 15, May 16, May 19, June 17, June 18, July 14, July 16, August 14, August 15, August 17,'
 
Albert=: {."1 NB. Albert knows month
Line 1,815 ⟶ 2,129:
possibleB=. (days e. days-.invaliddays)# possibleA
 
NB. our understanding of Albert's secondunderstanding of Bernard's understanding of Albert's first pass
months=: {."1 possibleB
invalidmonths=: (1<#/.~months)#~.months
echo ;:inv (months e. months -. invalidmonths)#possibleB</langsyntaxhighlight>
 
This gives us the July 16 result we were expecting
Line 1,824 ⟶ 2,138:
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang="java">import java.time.Month;
import java.util.Collection;
import java.util.List;
Line 1,909 ⟶ 2,223:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>There are 10 candidates remaining.
Line 1,916 ⟶ 2,230:
There are 1 candidates remaining.
Cheryl's birthday is JULY 16</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 2,094 ⟶ 2,407:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[["July","16"]]</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
Line 2,104 ⟶ 2,416:
 
A Birthday is represented by a JSON object {month, day} where {month:0} represents January.
<langsyntaxhighlight lang="jq">def count(stream; cond):
reduce stream as $i (0; if $i|cond then .+1 else . end);
 
Line 2,167 ⟶ 2,479:
end;
 
solve</langsyntaxhighlight>
{{out}}
<pre>
Cheryl's birthday is July 16.
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const dates = [[15, "May"], [16, "May"], [19, "May"], [17, "June"], [18, "June"],
[14, "July"], [16, "July"], [14, "August"], [15, "August"], [17, "August"]]
Line 2,189 ⟶ 2,500:
 
println("Cheryl's birthday is $(bday[2]) $(bday[1]).")
</langsyntaxhighlight>{{out}}
<pre>
Cheryl's birthday is July 16.
</pre>
 
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.2.71
 
val months = listOf(
Line 2,246 ⟶ 2,556:
else
println("Something went wrong!")
}</langsyntaxhighlight>
 
{{output}}
Line 2,252 ⟶ 2,562:
Cheryl's birthday is July 16
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Cheryl's Birthday in Lua 6/15/2020 db
 
local function Date(mon,day)
Line 2,324 ⟶ 2,633:
if count(subset) > 1 then apply(subset, invalidate) end
end)
listValidChoices()</langsyntaxhighlight>
{{out}}
<pre>Cheryl offers these ten choices:
Line 2,337 ⟶ 2,646:
3) After Bernard's revelation, Albert now knows, so month must be unique, leaving only:
July 16</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">opts = Tuples[{{"May"}, {15, 16, 19}}]~Join~Tuples[{{"June"}, {17, 18}}]~Join~Tuples[{{"July"}, {14, 16}}]~Join~Tuples[{{"August"}, {14, 15, 17}}];
monthsdelete = Select[GatherBy[opts, Last], Length /* EqualTo[1]][[All, 1, 1]];
opts = DeleteCases[opts, {Alternatives @@ monthsdelete, _}]
removedates = Catenate@Select[GatherBy[opts, Last], Length /* GreaterThan[1]];
opts = DeleteCases[opts, Alternatives @@ removedates]
Select[GatherBy[opts, First], Length /* EqualTo[1]]</langsyntaxhighlight>
{{out}}
<pre>{{"July", 14}, {"July", 16}, {"August", 14}, {"August", 15}, {"August", 17}}
{{"July", 16}, {"August", 15}, {"August", 17}}
{{{"July", 16}}}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import tables
import sets
import strformat
Line 2,427 ⟶ 2,734:
 
echo ""
echo fmt"So birthday date is {month} {day}."</langsyntaxhighlight>
 
{{out}}
Line 2,436 ⟶ 2,743:
 
So birthday date is July 16</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub filter {
my($test,@dates) = @_;
my(%M,%D,@filtered);
Line 2,477 ⟶ 2,783:
 
my ($m, $d) = split '-', $dates[0];
print "Cheryl's birthday is $months[$m] $d.\n";</langsyntaxhighlight>
{{out}}
<pre>Cheryl's birthday is July 16.</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Cheryls_Birthday.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,507 ⟶ 2,812:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">choices</span>
<!--</langsyntaxhighlight>-->
Iterating backwards down the choices array simplifies element removal, or more accurately removes the need for "not increment i".<br>
Step 1&2 is months with unique days, step 3 is days with unique months, step 4 is unique months.
Line 2,517 ⟶ 2,822:
=== functional/filter ===
(this can also be found in demo\rosetta\Cheryls_Birthday.exw)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">MONTH</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">DAY</span>
Line 2,563 ⟶ 2,868:
<span style="color: #0000FF;">{</span><span style="color: #000000;">td</span><span style="color: #0000FF;">[</span><span style="color: #004600;">DT_MONTH</span><span style="color: #0000FF;">],</span><span style="color: #000000;">td</span><span style="color: #0000FF;">[</span><span style="color: #004600;">DT_DAY</span><span style="color: #0000FF;">]}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">choices</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Cheryl's birthday is %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">td</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Mmmm ddth"</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Cheryl's birthday is July 16th
</pre>
 
=={{header|Python}}==
===Functional===
{{Works with|Python|3}}
<langsyntaxhighlight lang="python">'''Cheryl's Birthday'''
 
from itertools import groupby
Line 2,699 ⟶ 3,003:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[('July', '16')]</pre>
 
=={{header|R}}==
{{libheader|dplyr}}
<langsyntaxhighlight Rlang="r">options <- dplyr::tibble(mon = rep(c("May", "June", "July", "August"),times = c(3,2,2,3)),
day = c(15, 16, 19, 17, 18, 14, 16, 14, 15, 17))
 
Line 2,724 ⟶ 3,027:
for(i in unique(remaining$mon)){
if(sum(remaining$mon == i) == 1) {print(remaining[remaining$mon == i,])}
}</langsyntaxhighlight>
{{Out}}
<pre># A tibble: 1 x 2
Line 2,730 ⟶ 3,033:
<chr> <dbl>
1 July 16</pre>
 
=={{header|Racket}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="racket">#lang racket
 
(define ((is x #:key [key identity]) y) (equal? (key x) (key y)))
Line 2,762 ⟶ 3,064:
;; Albert now knows the answer too.
;; So the month must be unique within the remaining choices
[filter (unique albert)])</langsyntaxhighlight>
 
{{out}}
Line 2,768 ⟶ 3,070:
'((July 16))
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>my @dates =
{ :15day, :5month },
{ :16day, :5month },
Line 2,795 ⟶ 3,096:
my @months = <'' January February March April May June July August September October November December>;
 
say "Cheryl's birthday is { @months[$birthday<month>] } {$birthday<day>}.";</langsyntaxhighlight>
{{out}}
<pre>Cheryl's birthday is July 16.</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds Cheryl's birth date based on a person knowing the birth month, another */
/*──────────────────────── person knowing the birth day, given a list of possible dates.*/
$= 'May-15 May-16 May-19 June-17 June-18 July-14 July-16 August-14 August-15 August-17'
Line 2,834 ⟶ 3,134:
end /*k*/
end /*j*/
return $</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Cheryl's birthday is July 16
</pre>
 
=={{header|Ruby}}==
{{trans|C#}}
<langsyntaxhighlight lang="ruby">dates = [
["May", 15],
["May", 16],
Line 2,876 ⟶ 3,175:
.map { |k,v| v }
.flatten
print dates</langsyntaxhighlight>
{{out}}
<pre>10 remaining
Line 2,883 ⟶ 3,182:
["July", 16]</pre>
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
// This version is based on the Go version on Rosettacode
 
Line 3,002 ⟶ 3,301:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Cheryl's birthday is Birthday { month: July, day: 16 }
</pre>
 
=={{header|Scala}}==
==={{trans|D}}===
<langsyntaxhighlight lang="scala">import java.time.format.DateTimeFormatter
import java.time.{LocalDate, Month}
 
Line 3,054 ⟶ 3,352:
printf(birthDay.format(DateTimeFormatter.ofPattern("MMMM dd")))
}
}</langsyntaxhighlight>
{{out}}
<pre>July 16</pre>
Line 3,066 ⟶ 3,364:
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<langsyntaxhighlight Scalalang="scala">object Cheryl_sBirthday extends App {
 
private val possiblerDates = Set(
Line 3,098 ⟶ 3,396:
 
println(clou3)
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func f(day, month) {
Date.parse("#{day} #{month}", "%d %B")
}
Line 3,129 ⟶ 3,426:
}.group_by{ .month }.values.first_by { .len == 1 }[0]
 
say "Cheryl's birthday is #{birthday.fullmonth} #{birthday.day}."</langsyntaxhighlight>
{{out}}
<pre>
Cheryl's birthday is July 16.
</pre>
 
=={{header|Swift}}==
 
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">struct MonthDay: CustomStringConvertible {
static let months = [
"January", "February", "March", "April", "May", "June",
Line 3,194 ⟶ 3,490:
}
 
print("Cheryl's birthday is \(birthday)")</langsyntaxhighlight>
 
{{out}}
 
<pre>Cheryl's birthday is July 16</pre>
=={{header|uBasic/4tH}}==
{{trans|C}}
<syntaxhighlight lang="ubasic/4th">Dim @d(30)
Dim @m(13)
 
Push 5,15,1, 5,16,1, 5,19,1, 6,17,1 ,6,18,1, 7,14,1, 7,16,1, 8,14,1, 8,15,1, 8,17,1
For x = 29 To 0 Step -1 : @d(x) = Pop() : Next
 
Push "ERR", "Jan", "Feb", "Mar", "Apr", "May"
Push "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
For x = 12 To 0 Step -1 : @m(x) = Pop() : Next
 
Proc _printRemaining ' the month cannot have a unique day
Proc _firstPass
 
Proc _printRemaining ' the day must now be unique
Proc _secondPass
 
Proc _printRemaining ' the month must now be unique
Proc _thirdPass
 
Proc _printAnswer
End
 
_printRemaining
Local (2)
b@ = 0
For a@ = 0 To 29 Step 3
If @d(a@+2) Then b@ = b@ + 1
Next
 
Print b@; " remaining."
Return
 
_printAnswer
Local (1)
 
For a@ = 0 To 29 Step 3
If @d(a@+2) Then Print Show (@m(@d(a@))); ", "; @d(a@+1)
Next
Return
 
_firstPass ' the month cannot have a unique day
Local (3)
For a@ = 0 To 29 Step 3
c@ = 0
For b@ = 0 To 29 Step 3
If @d(b@+1) = @d(a@+1) Then c@ = c@ + 1
Next
If c@ = 1 Then
For b@ = 0 To 29 Step 3
If @d(b@+2) = 0 Then
Continue
EndIf
If @d(b@) = @d(a@) Then
@d(b@+2) = 0
EndIf
Next
EndIf
Next
Return
 
_secondPass ' the day must now be unique
Local (3)
For a@ = 0 To 29 Step 3
If @d(a@+2) = 0 Then Continue
c@ = 0
For b@ = 0 To 29 Step 3
If @d(b@+2) = 0 Then Continue
If @d(b@+1) = @d(a@+1) Then c@ = c@ + 1
Next
If c@ > 1 Then
For b@ = 0 To 29 Step 3
If @d(b@+2) = 0 Then
Continue
EndIf
If @d(b@+1) = @d(a@+1) Then
@d(b@+2) = 0
EndIf
Next
EndIf
Next
Return
 
_thirdPass ' the month must now be unique
Local (3)
For a@ = 0 To 29 Step 3
If @d(a@+2) = 0 Then Continue
c@ = 0
For b@ = 0 To 29 Step 3
If @d(b@+2) = 0 Then Continue
If @d(b@) = @d(a@) Then c@ = c@ + 1
Next
If c@ > 1 Then
For b@ = 0 To 29 Step 3
If @d(b@+2) = 0 Then
Continue
EndIf
If @d(b@) = @d(a@) Then
@d(b@+2) = 0
EndIf
Next
EndIf
Next
Return</syntaxhighlight>
{{Out}}
<pre>10 remaining.
5 remaining.
3 remaining.
Jul, 16
 
0 OK, 0:657</pre>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Private Sub exclude_unique_days(w As Collection)
Dim number_of_dates(31) As Integer
Dim months_to_exclude As New Collection
Line 3,260 ⟶ 3,681:
exclude_non_unique_months v
Debug.Print v(1)(0); " "; v(1)(1)
End Sub</langsyntaxhighlight>{{out}}
<pre>July 16</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Structure MonDay
Line 3,310 ⟶ 3,731:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>10 remaining.
Line 3,316 ⟶ 3,737:
3 remaining.
(July, 16)</pre>
=={{header|V (Vlang)}}==
 
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import time
 
struct Birthday {
Line 3,412 ⟶ 3,832:
println("Something went wrong!")
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,421 ⟶ 3,841:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var Months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
Line 3,470 ⟶ 3,890:
} else {
System.print("Something went wrong!")
}</langsyntaxhighlight>
 
{{out}}
Line 3,478 ⟶ 3,898:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">dates:=T(T("May", 15), T("May", 16), T("May", 19),
T("June", 17), T("June", 18),
T("July", 14), T("July", 16),
Line 3,496 ⟶ 3,916:
 
// print birthday such that muliples are shown, if any
println("Cheryl's birthday is ",theDay.flatten().flatten().concat(" "));</langsyntaxhighlight>
{{out}}
<pre>
2,130

edits