6
\$\begingroup\$

My work has recently changed the employee tracking system to one that happens to include a fun game to learn your coworker's faces:

a game to learn faces

Just a small problem: all the pictures include that person's trigram, which is derived from their name.

It then becomes quite easy to find the only name in the list that could have generated that trigram and click it, yeilding many points (yes, there is a company-wide scoreboard).

A trigram is 3 letters. The first letter is always the the first letter of the first name.

Depending on gender (how very archaic of us), the rest is:

  • if female: the second letter is the second letter of the first name, and the third is the first letter of the last name.
  • if male: the second and third letters are the first and second letters of the last name, respectively.

Some examples might be clearer:

John Doe, M -> JDO
Jane Doe, F -> JAD

Task:

Given a trigram and a gender, choose the name of that person from a list.

IO:

Input: a 3-character string, a gender marker, and a collection of strings representing the list to choose from. Format flexible.

Ouput: Something that represents the correct choice. It could be the full string, or the index of that string or any reasonable other thing. Format flexible.

The gender is binary for the purposes of this challenge.
The trigram will not contain whitespace.
There will always be a correct name in the list, and it will always be unambiguous.
You are not required to parse whether or not a name in the list is "male" or "female". If the trigram parses to that name, it is correct regardless of if it is the "correct" gender.
The list may contain an arbitrary number of names.
Names are always composed of 1 first name and 1 last name. you may take them in whatever way you see fit, including as an array, space separated, a tuple, two lists, etc.
You may take input strings capitalized how you see fit, but the capitalization must be consistent across all strings.

test cases:

["JDO", M, ["John Doe", "John Dae"]] -> 0       (only first matches)
["JDO", M, ["John Doe", "Jdane Orwell"]] -> 0   (only first is correct, because the second only works if gender is F)
["JDO", F, ["John Doe", "Jdane Orwell"]] -> 1   (opposite of above)
["JOD", F, ["John Doe", "Jane Doe"]] -> 0       (even though john is typically a "male" name, it's still the correct answer)
["JAD", F, 
  ["John Doe", "Jdane Orwell", "John Dae", "Jane Doe", "Jane Adsten"] -> 3 
["JAD", M, 
  ["John Doe", "Jdane Orwell", "John Dae", "Jane Doe", "Jane Adsten"] -> 4 

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Not everyone considers the concept of gender archaic (or will find your remark funny, if that was the intention). I would suggest to avoid such sensitive (and unnecessary) comments in a code golf challenge \$\endgroup\$
    – Luis Mendo
    Commented yesterday
  • 2
    \$\begingroup\$ I have to wonder what scheme they would use should a nonbinary employee need a trigram. \$\endgroup\$
    – Hearth
    Commented yesterday
  • \$\begingroup\$ I think a better code golf problem would be writing the function that converted a full name and gender to a trigram. The rest is extra lookup stuff. \$\endgroup\$ Commented 21 hours ago
  • 6
    \$\begingroup\$ @LuisMendo I don't think OP is saying that the concept of gender is archaic, they're saying that using different rules for code generation based on gender is archaic. (IMHO it is pretty weird...pointlessly gendered.) \$\endgroup\$ Commented 21 hours ago
  • 1
    \$\begingroup\$ @SteveBennett my point precisely. I have been told during onboarding that this was useful once when 2 married people worked here, and they happened to have first name beggining with the same letter. \$\endgroup\$ Commented 21 hours ago

7 Answers 7

6
\$\begingroup\$

JavaScript (ES6), 48 bytes

Expects (trigram, gender, array) where gender is 0 for female / 1 for male and the names in array are [first_name, last_name] pairs in upper case.

Returns one of the pairs.

(t,g,a)=>a.find(b=>b[0][0]+b[g][g^1]+b[1][g]==t)

Try it online!

Commented

(               // anonymous function taking:
  t,            //   t = trigram
  g,            //   g = gender (0 = female, 1 = male)
  a             //   a[] = list of names
) =>            //
a.find(b =>     // for each name b[] in a[]:
  b[0][0] +     //   1st letter of first name
  b[g][g ^ 1] + //   either the 1st letter of last name (if male)
                //   or the 2nd letter of first name (if female)
  b[1][g]       //   either the 1st (if female)
                //   or 2nd letter (if male) of last name
  == t          //   check if that's equal to t
)               // end of find()
\$\endgroup\$
4
\$\begingroup\$

Google Sheets, 43 bytes

=filter(B:C,A1=left(B:B,2/A2)&left(C:C,A2))

Takes gender as 1 for female and 2 for male. Outputs [firstName, lastName] in a spilled array. This works because string comparison is case-insensitive in many spreadsheet contexts.

screenshot

\$\endgroup\$
3
\$\begingroup\$

Jelly, 12 bytes

?"?^?3?}Fe??

A dyadic Link that accepts the choices on the left, as pairs of [first_name, last_name] in uppercase, and a pair on the right containing [trigram, gender], where gender is 1 for male and 2 for female, and yields a list of the matching choice(s).

Try it online!

How?

?"?^?3?}Fe?? - Link: Names; [trigram, gender]
           ? - keep the {Names} for which:
          ?  -   last four links a dyad - f(Name, [trigram, gender]):
      ?}     -     last three links as a monad - f([trigram, gender]):
  ?          -       dequeue {[trigram, gender]} -> [gender]
    ?        -       collect up while distinct under:
   ^         -         XOR with...
     3       -       three
                     -> Instructions = [[gender], [gender^3]]
                                     = [[1], [2]] (male)
                                    or [[2], [1]] (female)
 "           -     {Name} zip {Instructions} with:
?            -       head {NamePart} to 1-index {Instruction}
        F    -     flatten
         e   -     exists in {[trigram, gender]}?
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 16 bytes

Φζ?θ???ι …λ∨?ημ2

Try it online! Link is to verbose version of code. Takes the gender as 0 for female or 1 for male and outputs all matching names. Explanation:

 ζ                  Third input
Φ                   Filtered where
       ι            Current name
      ?             Split into spaces
     ?              Map over parts and join
          λ         Current part
         …          Truncated to length
             η      Second input
            ?       Plus
              μ     Inner index
           ∨        Logical Or
               2    Literal integer `2`
    ?               Uppercased
  ?                 Equals
   θ                First input
                    Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Retina 0.8.2, 46 bytes

i!`(?<=(.+)((?(4).).)?(F|(M))(.|?)*?)\1.* \2.*

Try it online! Takes input on separate lines but link is to test suite that takes a list of comma-separated inputs for convenience. Explanation:

i!`

Search case-insensitively and output matches.

(?<=...?)

Match after a newline.

(?<=...(.|?)*...)

Search back to the beginning of the input.

(?<=...?(F|(M))...)

Capture whether the employee is male.

(?<=(.+)((?(4).).)...)

Capture the last two letters as the second prefix if so, otherwise just one letter, and the remaining letters as the first prefix.

\1.* \2.*

Match each name against the appropriate prefix.

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 70 bytes

Param($t,$g,$s)"$($s|?{$t-eq"$($_[0][0])$($_[$g][!$g])$($_[1][$g])"})"

Try it online!

Expects gender as 0 for F, 1 for M, and the names as an array of ["FirstName", "LastName"].

Constructs the trigram using the gender to index where required (that is, where the indexes differ between F and M):

F: $s[0][0] $s[0][1] $s[1][0]
M: $s[0][0] $s[1][0] $s[1][1]

Ungolfed:

Param($t,$g,$s)                                        # Trigram, Gender, Strings
"$(                                                    # Open a string, and start a subexpression
    $s | Where-Object {                                # Pipe the array of names to Where-Object; each iteration will process a two-element array with FirstName, LastName in $_
        $t -eq "$($_[0][0])$($_[$g][!$g])$($_[1][$g])" # Construct the trigram from first and last name, and compare against the trigram parameter
    }                                                  # Where-Object will return the first and last name where the trigrams matched
)"                                                     # Enclosing the two strings inside a string will automatically join them with a space, returning the full name as single string

PowerShell, 73 bytes

Param($t,$g,$s)$s-match("^($($t[0])).*? ($($t[2]))"|% I*t(3,9)[$g] $t[1])

Try it online!

Expects gender as 0 for F, 1 for M, and the names as an array of "FirstName LastName".

Constructs a regex based on the gender and matches against the names:

F: ^($t[0]$t[1]).*? ($t[2])
M: ^($t[0]).*? ($t[1]$t[2])

Ungolfed:

Param($t,$g,$s)  # Trigram, Gender, Strings
$s -match (      # -match against an array will return all elements that match
    "^($($t[0])).*? ($($t[2]))" | ForEach-Object -MemberName Insert (3,9)[$g] $t[1]
                 # Constructs a partial regex for a trigram XYZ as "^(X).*? (Z)", then inserts Y depending on gender either after the X (at index 3), or before the Z (at index 9)
)
\$\endgroup\$
0
\$\begingroup\$

APL+WIN, 57 bytes

All characters lower case. Index origin = 1. Prompts for gender: male = 1 female = 2, then trigram, then nested vector of second names and finally nested vector of first names.

g←??k←?g↑t←??l←?g↓t?(3=(+/¨k=¨g↑¨?)++/¨l=¨(3-g)↑¨s)/??s←?

Try it online! Thanks to Dyalog Classic

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.