24
\$\begingroup\$

The challenge

Write a function or a program that takes a string composed of one or more subjective personal pronouns, separated by + signs, as an argument. The output must be a single pronoun, that is the result of the relation defined in the next paragraph.

Of course, feel free to edit as you wish to correct those grammatical errors that are surely present ;)

This is a code-golf challenge, so the shortest code wins.

The relation

The goal of this task is to translate in "math-speak" something that we use daily. How do we think of "You and I" ? Well, "We", don't we? So, if the input is You+I the output should be the string We. With more than two pronouns, it should look like: You+He+They -> You

The relation is defined as this table:

      I  You    He   We   You   They
I     I  We     We   We   We    We
You      You    You  We   You   You
He              He   We   You   They
We                   We   We    We
You                       You   You
They                            They

The 'You' Problem

Well, as you can see I'm not a native English speaker. In my language (Italian) there's a difference between the plural you (voi, in italian) and the singular you (tu). When I thought this challenge I didn't think in English, so there's the problem that it is impossible to recognize if I'm using plural or singular form. Fortunately (or obviously?), the input/output doesn't change in both forms, so you can use one of them and you will cover both cases!

About the input

The input will be always in the form "Pronoun+Pronoun+Pronoun" ... The pronouns will have the first letter in uppercase and the rest in lowercase. Pluses will be not be surrounded by spaces, only by pronouns. Empty input is possible and the result must be empty output.

Bonus

A little bonus of 15% if the program will manage two new pronouns: She and It. They are the same as He, obviously. Remember that this relation is reflexive, so She -> She and It -> It. Therefore, any combination that includes only She, He or It should output They.

Examples

You+I          -> We
You+He+They    -> You
I+You+He+They  -> We
They           -> They
They+You       -> You
You+You+I+You  -> We

For Bonus
She            -> She
She+He         -> They
I+It+He        -> We
It+You         -> You
\$\endgroup\$
3
  • \$\begingroup\$ @Timwi, if you are talking about the examples you are right, I'll add a few. Anyway, this "plus" relation is reflexive, so He+He is He, I+I is I .... \$\endgroup\$ Commented Jan 4, 2016 at 11:05
  • 5
    \$\begingroup\$ I get "I+I=I", since there can be only one "I" from a given speaker. But couldn't "He+He=They"? Generally if you say "He" twice in this construction, you're referring to two different male subjects, not the same one twice. \$\endgroup\$ Commented Jan 4, 2016 at 14:42
  • \$\begingroup\$ Many dialects of English have equivalents of voi. In mine, it's y'all, and used in the formal register. Another common one is yous, though I don't think that one gets used in formal speech really. Traditionally, you also have ye, well known to many because it's common in certain biblical translations. You should add in one of these or toss in thou as well to really shake things up. \$\endgroup\$ Commented Jan 4, 2016 at 21:31

5 Answers 5

9
\$\begingroup\$

Retina, 62 61 56 53 52 bytes

(.+)\+(?=\1)

.*(W|.I|I.).*
We
.*Y.*
You
.{4,}
They

Further golfing and explanation comes later.

The 4 substitution steps do the following:

  • anything multiple times is itself
  • if there is any We or I + anyhing the result is We
  • for anything else containing You the result is You
  • if we still have multiple parts or a sole They it's They as only He's and They's can be left

Try it online here.

3 bytes saved thanks to Martin Büttner.

\$\endgroup\$
1
  • \$\begingroup\$ Except for the last stage, you can use . instead of \+, since that's the only character allowed in front of a capital letter or after I. \$\endgroup\$ Commented Jan 4, 2016 at 13:01
6
\$\begingroup\$

JavaScript (ES6), 130 bytes

s=>(a=",I,You,He,We,They".split`,`,m="012345014444042242042345044444042545",r=0,s.split`+`.map(p=>r=m[+m[a.indexOf(p)]+r*6]),a[r])

Explanation

s=>(

  // a = array of each pronoun (including an empty string at index 0)
  a=",I,You,He,We,They".split`,`,

  // m = 6 x 6 map of pronoun indices for each combination of pronouns
  m="012345014444042242042345044444042545",

  r=0,                        // r = index of result pronoun
  s.split`+`.map(p=>          // for each pronoun in the input string
    r=m[+m[a.indexOf(p)]+r*6] // combine each pronoun with the previous one
  ),
  a[r]                        // return the resulting pronoun
)

Test

var solution = s=>(a=",I,You,He,We,They".split`,`,m="012345014444042242042345044444042545",r=0,s.split`+`.map(p=>r=m[+m[a.indexOf(p)]+r*6]),a[r])
<input type="text" id="input" value="You+You+I+You" />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>

\$\endgroup\$
1
  • \$\begingroup\$ Wow, I like this approach! I think I made a very poor bonus, cause to reach it here you'd have to add 7 bytes in the array (",She,It") and 28 in the matrix, reaching 165 -15% = 140 ... \$\endgroup\$ Commented Jan 4, 2016 at 10:28
2
\$\begingroup\$

Python 159 153 bytes

EDIT: Thanks @Pietu1998

This is a direct translation of the Javascript ES6 answer:

a=",I,You,He,We,They".split(',')
m="012345014444042242042345044444042545"
r=0
for p in raw_input().split('+'):r=int(m[int(m[a.index(p)])+r*6])
print a[r]

Try it here

\$\endgroup\$
1
  • \$\begingroup\$ s doesn't need to be a variable, and you can remove the extra line & space between the for and r= lines. Also, you might want to check if this could be shorter as a function. \$\endgroup\$ Commented Jan 5, 2016 at 15:07
1
\$\begingroup\$

Perl 5, 67 bytes

79 bytes really, but there's a 15% bonus.

$a{$_}=""for split/[+\s]/,<>;@_=%a;say@_<3?@_:I~~@_||We~~@_?We:You~~@_?You:They
\$\endgroup\$
0
1
\$\begingroup\$

Ruby, 150 136 131 119 111 bytes

ARGV.each{|a|puts %w[We You I He They][a.bytes.inject(0){|m,c|m|({87=>15,73=>7,89=>11,84=>9,72=>8}[c]||0)}%5]}

Bonus feature: handles multiple expressions on the same command line.

\$\endgroup\$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.