Cheryl's birthday: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 31:
{{trans|Nim}}
 
<langsyntaxhighlight lang=11l>T Date
String month
Int day
Line 91:
 
print()
print(‘So birthday date is ’month‘ ’day‘.’)</langsyntaxhighlight>
 
{{out}}
Line 105:
=={{Header|Ada}}==
{{trans|C}}
<langsyntaxhighlight lang=Ada>with Ada.Text_IO; use Ada.Text_IO;
 
procedure Main is
Line 229:
 
print_answer;
end Main;</langsyntaxhighlight>
{{out}}
<pre>
Line 241:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang=applescript>use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 862:
filteredArrayUsingPredicate:(ca's ¬
NSPredicate's predicateWithFormat:"0 < length")) as list
end |words|</langsyntaxhighlight>
{{Out}}
<pre>"[('july', '16')]"</pre>
Line 868:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>dates: [
[May 15] [May 16] [May 19]
[June 17] [June 18]
Line 900:
print ["\t-> remaining:" dates]
 
print ["\nCheryl's birthday:" first dates]</langsyntaxhighlight>
 
{{out}}
Line 921:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>oDates:= {"May" : [ 15, 16, 19]
,"Jun" : [ 17, 18]
,"Jul" : [14, 16]
Line 973:
else
return m " " obj.1
}</langsyntaxhighlight>
{{out}}
<pre>Jul 16</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f CHERYLS_BIRTHDAY.AWK [-v debug={0|1}]
#
Line 1,064:
printf("\n\n")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,087:
=={{header|C}}==
{{trans|C#}}
<langsyntaxhighlight lang=c>#include <stdbool.h>
#include <stdio.h>
 
Line 1,212:
printAnswer();
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>10 remaining.
Line 1,220:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>public static class CherylsBirthday
{
public static void Main() {
Line 1,249:
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,260:
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang=cpp>#include <algorithm>
#include <iostream>
#include <vector>
Line 1,370:
 
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:
 
(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:
// 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:
let _,e = List.concat g |> List.groupBy fst |> fN
printfn "%A" e
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,469:
 
=={{header|Factor}}==
<langsyntaxhighlight lang=factor>USING: assocs calendar.english fry io kernel prettyprint
sequences sets.extras ;
 
Line 1,494:
 
! print a date that looks like { { 16 7 } }
first first2 month-name write bl .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,501:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,596:
fmt.Println("Something went wrong!")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,605:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang=groovy>import java.time.Month
 
class Main {
Line 1,678:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>There are 10 candidates remaining.
Line 1,687:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>{-# LANGUAGE OverloadedStrings #-}
 
import Data.List as L (filter, groupBy, head, length, sortOn)
Line 1,758:
M.fromList $
((,) . fst . L.head) <*> fmap snd <$>
L.groupBy (on (==) fst) (L.sortOn fst xs)</langsyntaxhighlight>
{{Out}}
<pre>[("July","16")]</pre>
Line 1,764:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang=j>Dates=: <;._2 noun define
15 May
16 May
Line 1,787:
 
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:
The concepts here are the same, of course, it's just the presentation that's different.
 
<langsyntaxhighlight lang=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,818:
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:
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang=java>import java.time.Month;
import java.util.Collection;
import java.util.List;
Line 1,909:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>There are 10 candidates remaining.
Line 1,918:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang=javascript>(() => {
'use strict';
 
Line 2,094:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[["July","16"]]</pre>
Line 2,104:
 
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:
end;
 
solve</langsyntaxhighlight>
{{out}}
<pre>
Line 2,174:
 
=={{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:
 
println("Cheryl's birthday is $(bday[2]) $(bday[1]).")
</langsyntaxhighlight>{{out}}
<pre>
Cheryl's birthday is July 16.
Line 2,196:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang=scala>// Version 1.2.71
 
val months = listOf(
Line 2,246:
else
println("Something went wrong!")
}</langsyntaxhighlight>
 
{{output}}
Line 2,254:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>-- Cheryl's Birthday in Lua 6/15/2020 db
 
local function Date(mon,day)
Line 2,324:
if count(subset) > 1 then apply(subset, invalidate) end
end)
listValidChoices()</langsyntaxhighlight>
{{out}}
<pre>Cheryl offers these ten choices:
Line 2,339:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=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}}
Line 2,351:
 
=={{header|Nim}}==
<langsyntaxhighlight lang=Nim>import tables
import sets
import strformat
Line 2,427:
 
echo ""
echo fmt"So birthday date is {month} {day}."</langsyntaxhighlight>
 
{{out}}
Line 2,438:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>sub filter {
my($test,@dates) = @_;
my(%M,%D,@filtered);
Line 2,477:
 
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 lang=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:
<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:
=== functional/filter ===
(this can also be found in demo\rosetta\Cheryls_Birthday.exw)
<!--<langsyntaxhighlight lang=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:
<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>
Line 2,572:
===Functional===
{{Works with|Python|3}}
<langsyntaxhighlight lang=python>'''Cheryl's Birthday'''
 
from itertools import groupby
Line 2,699:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[('July', '16')]</pre>
Line 2,705:
=={{header|R}}==
{{libheader|dplyr}}
<langsyntaxhighlight lang=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:
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,733:
=={{header|Racket}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=racket>#lang racket
 
(define ((is x #:key [key identity]) y) (equal? (key x) (key y)))
Line 2,762:
;; Albert now knows the answer too.
;; So the month must be unique within the remaining choices
[filter (unique albert)])</langsyntaxhighlight>
 
{{out}}
Line 2,772:
(formerly Perl 6)
 
<syntaxhighlight lang=raku perl6line>my @dates =
{ :15day, :5month },
{ :16day, :5month },
Line 2,795:
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:
end /*k*/
end /*j*/
return $</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 2,842:
=={{header|Ruby}}==
{{trans|C#}}
<langsyntaxhighlight lang=ruby>dates = [
["May", 15],
["May", 16],
Line 2,876:
.map { |k,v| v }
.flatten
print dates</langsyntaxhighlight>
{{out}}
<pre>10 remaining
Line 2,883:
["July", 16]</pre>
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>
// This version is based on the Go version on Rosettacode
 
Line 3,002:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,010:
=={{header|Scala}}==
==={{trans|D}}===
<langsyntaxhighlight lang=scala>import java.time.format.DateTimeFormatter
import java.time.{LocalDate, Month}
 
Line 3,054:
printf(birthDay.format(DateTimeFormatter.ofPattern("MMMM dd")))
}
}</langsyntaxhighlight>
{{out}}
<pre>July 16</pre>
Line 3,066:
{{libheader|Scastie qualified}}
{{works with|Scala|2.13}}
<langsyntaxhighlight lang=Scala>object Cheryl_sBirthday extends App {
 
private val possiblerDates = Set(
Line 3,098:
 
println(clou3)
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang=ruby>func f(day, month) {
Date.parse("#{day} #{month}", "%d %B")
}
Line 3,129:
}.group_by{ .month }.values.first_by { .len == 1 }[0]
 
say "Cheryl's birthday is #{birthday.fullmonth} #{birthday.day}."</langsyntaxhighlight>
{{out}}
<pre>
Line 3,139:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang=swift>struct MonthDay: CustomStringConvertible {
static let months = [
"January", "February", "March", "April", "May", "June",
Line 3,194:
}
 
print("Cheryl's birthday is \(birthday)")</langsyntaxhighlight>
 
{{out}}
Line 3,201:
 
=={{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:
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:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>10 remaining.
Line 3,319:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang=vlang>import time
 
struct Birthday {
Line 3,412:
println("Something went wrong!")
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,421:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=ecmascript>var Months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
Line 3,470:
} else {
System.print("Something went wrong!")
}</langsyntaxhighlight>
 
{{out}}
Line 3,478:
 
=={{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:
 
// print birthday such that muliples are shown, if any
println("Cheryl's birthday is ",theDay.flatten().flatten().concat(" "));</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits