14
\$\begingroup\$

A person has two first names if their last name is also a common first name. You are tasked with determining which full names in a list are two first names.

John Smith
John Doe
Luke Ryan
Ryan Johnson
Jenna Jackson
Tom John

Any name that occurs in the first name column is potentially a first name. If the number of occurrences of the name in the first name column is greater than the number of occurrences in the last name column, it is definitely a first name.

In the above list, John appears twice in the first names and once in the last names so it is definitely a first name. Ryan appears once in the first and once in the last so it is (probably) a first name.

Therefore, Tom John definitely has two first names and Luke Ryan probably does.

Given the above list, your code should output the follow:

Luke Ryan has two first names
Tom John definitely has two first names

Input

As mentioned above, your code will take in a list of full names (from standard input, one per line) separated by spaces. Names can include hyphens or apostrophes, but you will never be given a first or last name that includes spaces (ie no Liam De Rosa, but Liam De-Rosa or Liam De'Rosa are fair game. In other words, names will match [-'A-Za-z]+.

Each full name will be unique (ie John Smith will not appear twice).

Output

Print full names names (once per line) followed by either has two first names or definitely has two first names if they meet the criteria above. Names should only be printed once.

Names that are not two first names do not need to be printed.

You must preserve the case and special characters of the name.

Examples

Input

Madison Harris
Riley Hudson
Addison Hills
Riley Phillips
Scott Hill
Levi Murphy
Hudson Wright
Nathan Baker
Harper Brooks
Chloe Morris
Aubrey Miller
Hudson Lopez
Samuel Owen
Wyatt Victoria
Brooklyn Cox
Nathan Murphy
Ryan Scott

Output

Riley Hudson definitely has two first names
Ryan Scott has two first names

Input

Owen Parker
Daniel Hall
Cameron Hall
Sofia Watson
Mia Murphy
Ryan Jones
Emily Ramirez

Output

[no output]

Input

Olivia Robinson
Jacob van-Dyke
Jacob Ella
Brayden De'Rosa
Levi Brook
Brook Bella
Ella Hill
Ella Anderson
Brook-Anne van-Dyke

Output

Jacob Ella definitely has two first names
Levi Brook has two first names

Notes and Scoring

Good luck!

Jonathan Frech
7,5111 gold badge23 silver badges44 bronze badges
asked Oct 19, 2017 at 15:35
\$\endgroup\$
7
  • \$\begingroup\$ Does Johnson count as John, or is it different? \$\endgroup\$ Commented Oct 19, 2017 at 15:40
  • 2
    \$\begingroup\$ Johnson would be different than John. Names must match exactly. \$\endgroup\$ Commented Oct 19, 2017 at 15:43
  • 1
    \$\begingroup\$ How we choose between definitely has two first names and has two first names ? It can be always one of those? \$\endgroup\$ Commented Oct 19, 2017 at 15:46
  • \$\begingroup\$ Only display definitely if the the name occurs more times in the first-name column than the last-name column. This case overrides, so only display definitely has two first names. Check out the examples. \$\endgroup\$ Commented Oct 19, 2017 at 15:49
  • 2
    \$\begingroup\$ How flexible is input format? Can it be an array of strings, one per person? Or a 2D array of strings? \$\endgroup\$ Commented Oct 19, 2017 at 16:08

14 Answers 14

6
\$\begingroup\$

Java (OpenJDK 8), (削除) 238 (削除ここまで) (削除) 222 (削除ここまで) (削除) 221 (削除ここまで) (削除) 220 (削除ここまで) 212 bytes

l->{for(String n:l){int k=0,j=0,q=0;for(String b=n.split(" ")[1];k<l.length;j+=l[k++].matches(b+" .*")?1:0)q+=l[k].endsWith(" "+b)?1:0;if(j>0)System.out.println(n+(j>q?" definitely":"")+" has two first names");}}

Try it online!

answered Oct 19, 2017 at 16:05
\$\endgroup\$
2
6
\$\begingroup\$

Python 2, (削除) 141 (削除ここまで) (削除) 137 (削除ここまで) (削除) 132 (削除ここまで) 130 bytes

-2 bytes thanks to @JonathanFrech

n=map(str.split,input())
for f,l in n:
 c=cmp(*[x.count(l)for x in zip(*n)])
 if~c:print f,l,'definitely '*c+'has two first names'

Try it online!

ShreevatsaR
1,1158 silver badges21 bronze badges
answered Oct 19, 2017 at 17:35
\$\endgroup\$
1
  • \$\begingroup\$ I think -c<1 can be ~c, as c does not appear to ever get positive. \$\endgroup\$ Commented Oct 20, 2017 at 21:05
3
\$\begingroup\$

AWK, (削除) 127 (削除ここまで) 123 bytes

{a[1ドル]++
b[L[++X]=2ドル]++
N[X]=0ドル}END{for(;++i<=X;)if(A=a[L[i]])print N[i],(b[L[i]]<A?"definitely ":"")"has two first names"}

Saving 1 byte by not using the built-in NR value.

Try it online!

answered Oct 19, 2017 at 18:29
\$\endgroup\$
3
\$\begingroup\$

Perl 5, (削除) 120 (削除ここまで) 118 + 2 (-al) = 120 bytes

$f{$F[0]}++;$l{$k{$_}=$F[1]}++}{map{print$_.' definitely'x($f{$t}>$l{$t})." has two first names"if$f{$t=$k{$_}}}keys%k

Try it online!

answered Oct 19, 2017 at 18:22
\$\endgroup\$
3
\$\begingroup\$

Ruby, (削除) 105 (削除ここまで) 129 bytes

+24 bytes because I missed part of the spec

->a{a.map{|e|l=e[r=/\S+$/];i=a.count{|n|n[/\S+/]==l};e+"#{' definitely'if i>a.count{|n|n[r]==l}} has two first names"if i>0}-[p]}

Try it online!

answered Oct 19, 2017 at 19:08
\$\endgroup\$
3
\$\begingroup\$

Python 2, (削除) 140 (削除ここまで) (削除) 127 (削除ここまで) (削除) 122 (削除ここまで) 131 bytes

N=map(str.split,input())
F,L=zip(*N)
for f,l in N:
 if l in F:print f,l,'definitely '*(F.count(l)>L.count(l))+'has two first names'

Try it online!

answered Oct 20, 2017 at 7:08
\$\endgroup\$
0
2
\$\begingroup\$

05AB1E, 144 bytes (Work in progress)

|vy#Dθˆн})©gF®®NèQO ̄®NèQO-D® ̄NèQO ̄ ̄NèQO-D.À>0›s>0›&i0›s0›&i®Nè" " ̄Nè" defínítely has two fírst names"J,ë®Nè" " ̄Nè" has two fírst names"J,}ë\\}} ́
answered Oct 19, 2017 at 23:22
\$\endgroup\$
1
  • 5
    \$\begingroup\$ No it works, I am just working on reducing the number of bytes \$\endgroup\$ Commented Oct 20, 2017 at 0:09
2
\$\begingroup\$

05AB1E, (削除) 53 (削除ここまで) 51 bytes

|UXv"€°‚•€ÛŒî"D„ ́Î ì)yð«ìõ ̧ìXð¡øεy#θQO}`.S>sèˆ} ̄õK»

Try it online!

Explanation

| # Take inputs as array
 UX # Store in X and push X to stack
 v } # For each name, do
 "€°‚•€ÛŒî"D # Push "has two first names" twice
 „ ́Î ì # Prepend "definitely " to one of those
 )yð«ì # Wrap both strings in an array and prepend (name + " ") to each
 õ ̧ì # Prepend " " to array
 Xð¡øεy#θQO}` # Get occurences in input first and last names
 .S> # 0 for not first name, 1 for first name and 2 for definitely first name
 sèˆ # Get string at that index and push to global array
 ̄õK» # Output global array, 1 string per line

Or (削除) 51 (削除ここまで) 49 bytes, assuming standard I/O Rules (input and output as arrays)

UXv"€°‚•€ÛŒî"D„ ́Î ì)yð«ìõ ̧ìXð¡øεy#θQO}`.S>sèˆ} ̄õK

Try it online!

answered Oct 20, 2017 at 11:43
\$\endgroup\$
1
\$\begingroup\$

PHP, 172 bytes

for(;$s=$argv[++$i];$f[$a]++,$l[$n[]=$b]++)[$a,$b]=explode(" ",$s);for(;$b=$n[+$k++];)$f[$b]<$l[$b]||print$argv[$k].($f[$b]>$l[$b]?" definetly":"")." has two first names
";

takes names as separate command line arguments.

Run with -nr or try it online.

answered Oct 20, 2017 at 8:11
\$\endgroup\$
0
\$\begingroup\$

Python 3, 187 bytes

n={}
c=[]
while 1:
 try:a,b=input().split();n[a]=n.get(a,0)+1;n[b]=n.get(b,0)-1;c+=[[a,b]]
 except:[print(a,b,'definitely has two first names'[(n[b]>0)*11:])for a,b in c if n[b]>=0];break
answered Oct 19, 2017 at 19:22
\$\endgroup\$
0
\$\begingroup\$

R, 207 bytes

n=readLines("stdin")
s='definitively has two first names\n'
x=matrix(t(unlist(sapply(n,strsplit,' '))),2)
j=1
for(i in n){a=apply(x[2,j]==x,1,sum)
b=a[2]>=a[1]
if(a[1])cat(paste(i,substr(s,14*b,34)))
j=j+1}

Try it online!

answered Oct 20, 2017 at 0:38
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 149 bytes

s=>s.replace(/(.*) (.*)\n/g,(_,t,u)=>g(`
${u} `)>1?t+` ${u}${v>g(` ${u}
`)?` definitely`:``} has two first names
`:``,g=u=>v=`
${s}`.split(u).length)

I/O includes trailing newline. Largely inspired by @RobertoGraham's answer.

answered Oct 20, 2017 at 9:33
\$\endgroup\$
0
\$\begingroup\$

Haskell, (削除) 144 (削除ここまで) (削除) 140 (削除ここまで) 139 bytes

g.map words.lines
g s=id=<<[n++' ':f++[c|c<-" definitely",t 0>t 1]++" has two first names\n"|[n,f]<-s,let t i=filter(==f)$map(!!i)s,[]<t 0]

Try it online!

answered Oct 20, 2017 at 20:33
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 176 bytes

$n=$args
$s=" has two first names"
$f=$n|%{$_.Split()[0]}
$l=$n|%{$_.Split()[1]}
$i=0
$l|%{switch(($f-match$_).count){{$_-eq1}{$n[$i]+$s}{$_-gt1}{$n[$i]+" definitely"+$s}}$i++}
answered Oct 23, 2017 at 17:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.