Dating agency: Difference between revisions

Added FreeBASIC
mNo edit summary
(Added FreeBASIC)
 
(10 intermediate revisions by 6 users not shown)
Line 19:
This task is intended as a bit of fun as well as a simple exercise in object modelling so hopefully it won't offend anyone!
<br><br>
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V sailors = [‘Adrian’, ‘Caspian’, ‘Dune’, ‘Finn’, ‘Fisher’, ‘Heron’, ‘Kai’, ‘Ray’, ‘Sailor’, ‘Tao’]
 
V ladies = [‘Ariel’, ‘Bertha’, ‘Blue’, ‘Cali’, ‘Catalina’, ‘Gale’, ‘Hannah’, ‘Isla’, ‘Marina’, ‘Shelly’]
 
F isnicegirl(s)
R Int(s[0].code) % 2 == 0
 
F islovable(slady, ssailor)
R Int(slady.last.code) % 2 == Int(ssailor.last.code) % 2
 
L(lady) ladies
I isnicegirl(lady)
print(‘Dating service should offer a date with ’lady)
L(sailor) sailors
I islovable(lady, sailor)
print(‘ Sailor ’sailor‘ should take an offer to date her.’)
E
print(‘Dating service should NOT offer a date with ’lady)</syntaxhighlight>
 
{{out}}
<pre>
Dating service should NOT offer a date with Ariel
Dating service should offer a date with Bertha
Sailor Dune should take an offer to date her.
Sailor Kai should take an offer to date her.
Sailor Ray should take an offer to date her.
Sailor Tao should take an offer to date her.
Dating service should offer a date with Blue
Sailor Dune should take an offer to date her.
Sailor Kai should take an offer to date her.
Sailor Ray should take an offer to date her.
Sailor Tao should take an offer to date her.
Dating service should NOT offer a date with Cali
Dating service should NOT offer a date with Catalina
Dating service should NOT offer a date with Gale
Dating service should offer a date with Hannah
Sailor Adrian should take an offer to date her.
Sailor Caspian should take an offer to date her.
Sailor Finn should take an offer to date her.
Sailor Fisher should take an offer to date her.
Sailor Heron should take an offer to date her.
Sailor Sailor should take an offer to date her.
Dating service should NOT offer a date with Isla
Dating service should NOT offer a date with Marina
Dating service should NOT offer a date with Shelly
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<syntaxhighlight lang="factor">USING: formatting io kernel math math.primes math.vectors
prettyprint qw sequences sets ;
 
CONSTANT: ladies qw{
Abigail Emily Haley Leah Maru
Penny Caroline Jodi Marnie Robin
}
 
CONSTANT: sailor "Willy"
 
: prime-root? ( n -- ? ) 1 - 9 mod 1 + prime? ; ! is the digital root prime?
: nice? ( lady sailor -- ? ) [ hashcode ] bi@ + prime-root? ; ! a lady is nice if the sum of the hashcode of her name and the sailor's name has a prime digit root
: lovable? ( lady sailor -- ? ) vdot prime-root? ; ! a lady is lovable if the dot product of her name and the sailor's name has a prime digital root
 
: nice ( -- seq ) ladies [ sailor nice? ] filter ;
: lovable ( -- seq ) ladies [ sailor lovable? ] filter ;
: compatible ( -- seq ) nice lovable intersect ;
: ladies. ( seq -- ) ", " join print ;
 
"lady nice? lovable?" print
"------------------------------" print
ladies [
dup sailor [ nice? ] [ lovable? ] bi-curry bi
[ "yes" "no" ? ] bi@
"%-10s %-10s %-10s\n" printf
] each nl
 
"Based on this analysis:" print nl
"The dating agency should suggest the following dates:" print
nice ladies. nl
sailor "And %s should offer to date these ones:\n" printf
lovable ladies. nl
sailor "But just between us, only the following ladies are compatible with %s:\n" printf
compatible ladies.</syntaxhighlight>
{{out}}
<pre>
lady nice? lovable?
------------------------------
Abigail no yes
Emily no no
Haley no no
Leah no no
Maru yes no
Penny yes no
Caroline no yes
Jodi no yes
Marnie yes no
Robin yes yes
 
Based on this analysis:
 
The dating agency should suggest the following dates:
Maru, Penny, Marnie, Robin
 
And Willy should offer to date these ones:
Abigail, Caroline, Jodi, Robin
 
But just between us, only the following ladies are compatible with Willy:
Robin
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Function digitalRoot(n As Integer) As Integer
Assert(n >= 0)
While n > 9
Dim As Integer tot = 0
While n > 0
tot += (n Mod 10)
n \= 10
Wend
n = tot
Wend
Return n
End Function
 
Type Lady
nombre As String
Declare Function lovable() As Integer
Declare Function love(s As Any Ptr) As Integer
End Type
 
Function Lady.lovable() As Integer
Return (Asc(this.nombre) Mod 2)
End Function
 
Function Lady.love(s As Any Ptr) As Integer
Return (digitalRoot(Asc(this.nombre)) > 4)
End Function
 
Function newLady(nombre As String) As Lady
Dim As Lady l
l.nombre = nombre
Return l
End Function
 
Function ladyNames(ladies() As Lady) As String
Dim As String res = ""
For i As Integer = Lbound(ladies) To Ubound(ladies)
If ladies(i).nombre <> "" Then res &= ladies(i).nombre & ", "
Next i
Return Left(res, Len(res)-2) ' remove last comma and space
End Function
 
Type Marinero
nombre As String
Declare Function love(l As Any Ptr) As Integer
End Type
 
Function Marinero.love(l As Any Ptr) As Integer
Return Cast(Lady Ptr, l)->lovable()
End Function
 
Dim As String nombres(9) = {"Ada", "Crystal", "Elena", "Euphoria", "Janet", "Julia", "Lily", "Miranda", "Perl", "Ruby"}
Dim As Lady ladies(Ubound(nombres))
For i As Integer = Lbound(nombres) To Ubound(nombres)
ladies(i) = newLady(nombres(i))
Next i
Dim As Marinero sailor
sailor.nombre = "Pascal"
Dim As Integer eligiblesCount = 0
Dim As Lady eligibles(Ubound(ladies))
 
For i As Integer = Lbound(ladies) To Ubound(ladies)
If ladies(i).love(@sailor) Then
eligibles(eligiblesCount) = ladies(i)
eligiblesCount += 1
End If
Next i
 
Print "lady loves sailor lovable"
Print "---- ------------ -------"
 
For i As Integer = Lbound(ladies) To Ubound(ladies)
Print Using "\ \ & &"; ladies(i).nombre; Iif(ladies(i).love(@sailor), "True ", "False"); Iif(ladies(i).lovable(), "True ", "False")
Next i
 
Print !"\nBased on this analysis:"
Print "The dating agency should suggest the following ladies:"
Print ladyNames(eligibles())
 
Print !"\nand "; sailor.nombre; " should offer to date these ones:"
Print ladyNames(eligibles())
 
Sleep</syntaxhighlight>
 
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">sailors = ["Adrian", "Caspian", "Dune", "Finn", "Fisher", "Heron", "Kai",
"Ray", "Sailor", "Tao"]
 
ladies = ["Ariel", "Bertha", "Blue", "Cali", "Catalina", "Gale", "Hannah",
"Isla", "Marina", "Shelly"]
 
isnicegirl(s) = Int(s[begin]) % 2 == 0
 
islovable(slady, ssailor) = Int(slady[end]) % 2 == Int(ssailor[end]) % 2
 
for lady in ladies
if isnicegirl(lady)
println("Dating service should offer a date with ", lady)
for sailor in sailors
if islovable(lady, sailor)
println(" Sailor ", sailor, " should take an offer to date her.")
end
end
else
println("Dating service should NOT offer a date with ", lady)
end
end
</syntaxhighlight>{{out}}
<pre>
Dating service should NOT offer a date with Ariel
Dating service should offer a date with Bertha
Sailor Dune should take an offer to date her.
Sailor Kai should take an offer to date her.
Sailor Ray should take an offer to date her.
Sailor Tao should take an offer to date her.
Dating service should offer a date with Blue
Sailor Dune should take an offer to date her.
Sailor Kai should take an offer to date her.
Sailor Ray should take an offer to date her.
Sailor Tao should take an offer to date her.
Dating service should NOT offer a date with Cali
Dating service should NOT offer a date with Catalina
Dating service should NOT offer a date with Gale
Dating service should offer a date with Hannah
Sailor Adrian should take an offer to date her.
Sailor Caspian should take an offer to date her.
Sailor Finn should take an offer to date her.
Sailor Fisher should take an offer to date her.
Sailor Heron should take an offer to date her.
Sailor Sailor should take an offer to date her.
Dating service should NOT offer a date with Isla
Dating service should NOT offer a date with Marina
Dating service should NOT offer a date with Shelly
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
 
use Digest::SHA qw(sha1_hex);
use List::Util <max head>;
 
my(%laddies,%taylors);
 
for my $name ( qw( Adam Bob Conrad Drew Eddie Fred George Harry Ian Jake Ken Larry Mike
Ned Oscar Peter Quincy Richard Sam Tom Uriah Victor Will Yogi Zach ) ) {
$laddies{$name}{loves} = hex substr sha1_hex($name), 0, 4;
$laddies{$name}{lovable} = hex substr sha1_hex($name), -4, 4;
}
 
for my $name ( < Elizabeth Swift Rip > ) {
$taylors{$name}{loves} = hex substr sha1_hex($name), 0, 4;
$taylors{$name}{lovable} = hex substr sha1_hex($name), -4, 4;
}
 
sub rank_by {
my($subk,$k,$t,$l) = @_;
sort { abs $$t{$k}{$subk} - $$l{$a}{$subk} <=> abs $$t{$k}{$subk} - $$l{$b}{$subk} } keys %$l;
}
 
for my $taylor (sort keys %taylors) {
printf "%9s will like: %s\n", $taylor, join ', ', my @likes = head 10, rank_by('loves', $taylor, \%taylors, \%laddies);
printf " Is liked by: %s\n", join ', ', my @liked = head 10, rank_by('lovable',$taylor, \%taylors, \%laddies);
 
my($max,%matches) = 0;
$matches{$liked[$_]} = @liked-$_ for reverse 0..$#liked;
$matches{$likes[$_]} += @likes-$_ for reverse 0..$#likes;
$matches{$_} < $max or $max = $matches{$_} for keys %matches;
print 'Best match(s): ' . join(', ', sort grep { $matches{$_} == $max } keys %matches) . "\n\n";
}</syntaxhighlight>
{{out}}
<pre>Elizabeth will like: Adam, Conrad, Ian, Jake, Larry, Bob, Drew, Quincy, Mike, Victor
Is liked by: Ian, Mike, Uriah, Jake, Tom, Richard, Drew, George, Victor, Larry
Best match(s): Ian
 
Rip will like: Jake, Larry, Bob, Drew, Quincy, Mike, Adam, Conrad, Ian, Victor
Is liked by: Ned, Will, Eddie, Oscar, Quincy, Conrad, George, Richard, Jake, Uriah
Best match(s): Jake, Quincy
 
Swift will like: Ken, Oscar, Zach, Ned, Sam, Tom, Will, Yogi, Fred, Uriah
Is liked by: Ken, Adam, Yogi, Sam, Larry, Victor, Drew, Bob, Tom, Ian
Best match(s): Ken</pre>
 
=={{header|Phix}}==
{{trans|Wren}}
Line 33 ⟶ 332:
Booleans in Phix are just integers, and need to be printed with %t rather than %s.
Of course all this is open source, yada-yada-ya.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">structs</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 104 ⟶ 403:
<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;">"\nand %s should offer to date these ones:\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">sailor</span><span style="color: #0000FF;">.</span><span style="color: #000000;">name</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;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lady_names</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">eligibles</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sailor_love</span><span style="color: #0000FF;">)))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 130 ⟶ 429:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">"""
Jokes about courtesans aside, the selection process is by the letters of the names.
If the integer corresponding to the ASCII character of the first letter of the
Line 141 ⟶ 440:
'Ray', 'Sailor', 'Tao']
 
ladies = ['Ariel', 'Bertha', 'Blue', 'Cali', 'Catalina', 'DorisGale', 'GaleHannah',
'Isla', 'Marina', 'Shelly']
 
Line 158 ⟶ 457:
else:
print("Dating service should NOT offer a date with", lady)
</langsyntaxhighlight>{{out}}
<pre>
Dating service should NOT offer a date with Ariel
Line 173 ⟶ 472:
Dating service should NOT offer a date with Cali
Dating service should NOT offer a date with Catalina
Dating service should offer a date with Doris
Sailor Dune should take an offer to date her.
Sailor Kai should take an offer to date her.
Sailor Ray should take an offer to date her.
Sailor Tao should take an offer to date her.
Dating service should NOT offer a date with Gale
Dating service should offer a date with Hannah
Sailor Adrian should take an offer to date her.
Sailor Caspian should take an offer to date her.
Sailor Finn should take an offer to date her.
Sailor Fisher should take an offer to date her.
Sailor Heron should take an offer to date her.
Sailor Sailor should take an offer to date her.
Dating service should NOT offer a date with Isla
Dating service should NOT offer a date with Marina
Dating service should NOT offer a date with Shelly
</pre>
 
 
=={{header|Raku}}==
Welcome to the '''Arbitrary and Capricious Dating Agency''', (We don't use zodiacs, but we're just as arbitrary!)
 
<syntaxhighlight lang="raku" perl6line>use Digest::SHA1::Native;
 
my %ladies = < Alice Beth Cecilia Donna Eunice Fran Genevieve Holly Irene Josephine Kathlene Loralie Margaret
Line 213 ⟶ 513:
for @liked.reverse Z, (1..10) { %matches{.[0]} += .[1] };
for @likes.reverse Z, (1..10) { %matches{.[0]} += .[1] };
say 'Best match(s): ' ~ %matches.maxgrep( *.value == %matches.values.max)».key.sort.join(', ');
say '';
}</langsyntaxhighlight>
 
<pre> Ahab will like: Eunice, Ursula, Irene, Holly, Fran, Genevieve, Zoey, Donna, Cecilia, Alice
Is liked by: Nancy, Theresa, Victoria, Genevieve, Alice, Rhonda, Kathlene, Odelle, Eunice, Pamela
Best match(s): Eunice, Genevieve
 
Brutus will like: Beth, Nancy, Quinci, Odelle, Josephine, Kathlene, Theresa, Rhonda, Wren, Margaret
Is liked by: Yasmine, Margaret, Loralie, Holly, Wren, Fran, Quinci, Eunice, Alice, Victoria
Best match(s): Quinci
 
Popeye will like: Loralie, Theresa, Stephanie, Yasmine, Alice, Cecilia, Donna, Zoey, Nancy, Genevieve
Is liked by: Loralie, Margaret, Holly, Wren, Yasmine, Fran, Quinci, Eunice, Alice, Victoria
Best match(s): Loralie</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int, Nums
import "./fmt" for Fmt
 
Line 295 ⟶ 595:
System.print(eligibles)
System.print("\nand %(sailor) should offer to date these ones:")
System.print(eligibles.where { |e| sailor.love(e) }.toList)</langsyntaxhighlight>
 
{{out}}
2,127

edits