Dating agency

From Rosetta Code
Revision as of 12:06, 10 November 2021 by PureFox (talk | contribs) (Created a new draft task, Dating agency, and added a Wren solution.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Dating agency is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Scenario

A sailor signs up to a dating agency hoping to find a suitable mate.

The dating agency has 10 ladies on its books who may be suitable and uses the algorithm "all the nice girls love a sailor" to decide which ones to put forward for the sailor's consideration.

The sailor uses a different algorithm ("lady is lovable") to decide which ladies to actually date.

Task

Model this scenario in your language.

Give the sailor and ladies some names.

Use some arbitrary method based on the ladies' names to determine whether they're nice and/or lovable. For preference, choose a method where the outcomes are (more or less) equally likely.

Hence, determine which ladies the dating agency should suggest and which of these the sailor should offer to date.

Note

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!

Wren

Library: Wren-math
Library: Wren-fmt

<lang ecmascript>import "./math" for Int, Nums import "./fmt" for Fmt

class Lady {

   construct new(name) {
       _name = name
   }
   name { _name }
   // Sum the ASCII values of the characters in the ladies' names and find the digital root.
   // If it's more than 4, they're 'nice'.
   nice {
       var sum = Nums.sum(_name.bytes)
       return Int.digitalRoot(sum)[0] > 4
   }
   // Sum the ASCII values of the characters in the ladies' names.
   // If it's odd, they're 'lovable'.
   lovable {
       var sum = Nums.sum(_name.bytes)
       return sum % 2 == 1
   }
   love(s) {
       if (!(s is Sailor)) return null // indeterminate
       return nice
   }
   toString { _name }

}

class Sailor {

   construct new(name) {
       _name = name
   }
   name { _name }
   love(l) {
       if (!(l is Lady)) return null // indeterminate
       return l.lovable
   }
   toString { _name }

}

var names = ["Ada", "Crystal", "Elena", "Euphoria", "Janet", "Julia", "Lily", "Miranda", "Perl", "Ruby"] var ladies = names.map { |n| Lady.new(n) }.toList var sailor = Sailor.new("Pascal") var eligibles = [] var format = "$-10s $-12s $s" Fmt.print(format, "lady", "loves sailor", "lovable") Fmt.print(format, "----", "------------", "-------") for (lady in ladies) {

   var lovesSailor = lady.love(sailor)
   if (lovesSailor) eligibles.add(lady)
   Fmt.print(format, lady, lovesSailor, lady.lovable)

} System.print("\nBased on this analysis:") System.print("\nThe dating agency should suggest the following ladies:") System.print(eligibles) System.print("\nand %(sailor) should offer to date these ones:") System.print(eligibles.where { |e| e.lovable }.toList)</lang>

Output:
lady        loves sailor  lovable
----        ------------  -------
Ada         false         false
Crystal     true          false
Elena       true          true
Euphoria    false         true
Janet       false         false
Julia       true          true
Lily        true          false
Miranda     true          false
Perl        true          true
Ruby        false         false

Based on this analysis:

The dating agency should suggest the following ladies:
[Crystal, Elena, Julia, Lily, Miranda, Perl]

and Pascal should offer to date these ones:
[Elena, Julia, Perl]