Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
    
@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 .... – Simone Chelo Jan 4 at 11:05
5  
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. – Darrel Hoffman Jan 4 at 14:42
    
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. – guifa Jan 4 at 21:31

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.

share|improve this answer
    
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. – Martin Ender Jan 4 at 13:01

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>

share|improve this answer
    
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 ... – Simone Chelo Jan 4 at 10:28

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
share|improve this answer

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

share|improve this answer
    
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. – Pietu1998 Jan 5 at 15:07

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.

share|improve this answer
    
You can save 8 bytes by changing your array literal to %w[We You I He They]. – Jesse Sielaff Jan 5 at 3:07
    
@JesseSielaff, good catch. – PellMell Jan 5 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.