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
- This is code golf. Lowest score (bytes) wins.
- standard loopholes apply.
Good luck!
14 Answers 14
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");}}
-
\$\begingroup\$ Nice! You can collapse your initializations to
k=j=q=0. Also, it won't save you any bytes but you could replace the latterx+=y?1:0withif(y)x++. If you like to optimize readability after length like I do, that's probably the way to go. \$\endgroup\$Jakob– Jakob2017年10月20日 05:05:01 +00:00Commented Oct 20, 2017 at 5:05 -
\$\begingroup\$ You can save 8 bytes by putting the
Stringandintdirectly (in the for-loop):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");}}\$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年10月20日 06:24:38 +00:00Commented Oct 20, 2017 at 6:24
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'
-
\$\begingroup\$ I think
-c<1can be~c, ascdoes not appear to ever get positive. \$\endgroup\$Jonathan Frech– Jonathan Frech2017年10月20日 21:05:05 +00:00Commented Oct 20, 2017 at 21:05
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.
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]}
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'
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,}ë\\}} ́
-
5\$\begingroup\$ No it works, I am just working on reducing the number of bytes \$\endgroup\$David Smith– David Smith2017年10月20日 00:09:35 +00:00Commented Oct 20, 2017 at 0:09
05AB1E, (削除) 53 (削除ここまで) 51 bytes
|UXv"€°‚•€ÛŒî"D„ ́Î ì)yð«ìõ ̧ìXð¡øεy#θQO}`.S>sèˆ} ̄õK»
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
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.
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
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}
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.
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]
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++}
Johnsoncount asJohn, or is it different? \$\endgroup\$Johnsonwould be different thanJohn. Names must match exactly. \$\endgroup\$definitely has two first namesandhas two first names? It can be always one of those? \$\endgroup\$definitelyif the the name occurs more times in the first-name column than the last-name column. This case overrides, so only displaydefinitely has two first names. Check out the examples. \$\endgroup\$