Pronoun operation

24

1

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

Simone Chelo

Posted 2016-01-04T09:03:31.603

Reputation: 543

@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 – 2016-01-04T11:05:11.000

5I 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 – 2016-01-04T14:42:39.843

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. – user0721090601 – 2016-01-04T21:31:32.267

Answers

9

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.

randomra

Posted 2016-01-04T09:03:31.603

Reputation: 19 909

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 – 2016-01-04T13:01:06.137

6

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>

user81655

Posted 2016-01-04T09:03:31.603

Reputation: 10 181

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 – 2016-01-04T10:28:01.687

2

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

TanMath

Posted 2016-01-04T09:03:31.603

Reputation: 1 431

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. – PurkkaKoodari – 2016-01-05T15:07:30.687

2

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

msh210

Posted 2016-01-04T09:03:31.603

Reputation: 3 094

1

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.

PellMell

Posted 2016-01-04T09:03:31.603

Reputation: 171