Jump to content

Cheryl's birthday: Difference between revisions

Added 11l
No edit summary
(Added 11l)
Line 27:
* [https://en.wikipedia.org/wiki/Tuple_relational_calculus, Tuple Relational Calculus]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<lang 11l>T Date
String month
Int day
F (month, day)
.month = month
.day = day
 
V dates = [Date(‘May’, 15), Date(‘May’, 16), Date(‘May’, 19), Date(‘June’, 17), Date(‘June’, 18),
Date(‘July’, 14), Date(‘July’, 16), Date(‘August’, 14), Date(‘August’, 15), Date(‘August’, 17)]
 
DefaultDict[Int, Set[String]] monthTable
L(date) dates
monthTable[date.day].add(date.month)
 
DefaultDict[String, Set[Int]] dayTable
L(date) dates
dayTable[date.month].add(date.day)
 
V possibleMonths = Set[String]()
V possibleDays = Set[Int]()
 
L(month, days) dayTable
I days.len > 1
possibleMonths.add(month)
 
L(month, days) dayTable
L(day) days
I monthTable[day].len == 1
possibleMonths.remove(month)
print(‘After first Albert's sentence, possible months are ’Array(possibleMonths).join(‘, ’)‘.’)
 
L(day, months) monthTable
I months.len > 1
possibleDays.add(day)
 
V impossibleDays = Set[Int]()
L(day) possibleDays
I monthTable[day].intersection(possibleMonths).len > 1
impossibleDays.add(day)
L(day) impossibleDays
possibleDays.remove(day)
print(‘After Bernard's sentence, possible days are ’Array(possibleDays).join(‘, ’)‘.’)
 
V impossibleMonths = Set[String]()
L(month) possibleMonths
I dayTable[month].intersection(possibleDays).len > 1
impossibleMonths.add(month)
L(month) impossibleMonths
possibleMonths.remove(month)
 
assert(possibleMonths.len == 1)
V month = possibleMonths.pop()
print(‘After second Albert's sentence, remaining month is ’month‘...’)
 
possibleDays = possibleDays.intersection(dayTable[month])
assert(possibleDays.len == 1)
V day = possibleDays.pop()
print(‘and thus remaining day is ’day‘.’)
 
print()
print(‘So birthday date is ’month‘ ’day‘.’)</lang>
 
{{out}}
<pre>
After first Albert's sentence, possible months are August, July.
After Bernard's sentence, possible days are 15, 16, 17.
After second Albert's sentence, remaining month is July...
and thus remaining day is 16.
 
So birthday date is July 16.
</pre>
 
=={{header|AppleScript}}==
1,481

edits

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