6
\$\begingroup\$

The least weird fact about the US presidency right now is that there have been two unrelated presidents whose last names start with T-R-U-M.

That made me think, how many US presidents share other combinations of characters? There are obviously two presidents that share the characters "BUSH", and "ROOSEVELT". But did you know there are four presidents who share the characters "BU" (Buchanan, Buren, Bush and Bush), three presidents who share "CL" (Cleveland, Cleveland and Clinton), but only one president whose name starts with an "E" (Eisenhower).


Challenge:

Take a non-empty string as input, and output how many presidents have names starting at those letters. You may assume that the string will match at least one president.

That means, you may get inputs such as "J" (Jackson, Jefferson, Johnson and Johnson), "TA" (Taft and Taylor) and "Nixon" (Nixon). You will not get inputs such as "Z", "RANT" and "RUMP".


Rules

  • The input is on any convenient format, with optional upper or lower case.
  • The output should be an integer in the range 1 ... 5.
  • Shortest code in bytes in each language wins

Test cases

There are three strings that should return 5 (C, H, T), two strings that should return 4 (BU, J), five strings that should return 3 (A, CL, HAR, M, R). There are many strings that should return 2. Where there are parentheses, it means both AD, ADA, ADAM and ADAMS should all return 2, as well as TR, TRU and TRUM. You will not get "TRUT" as input, since that doesn't match a president.

5: C, H, T
4: BU, J, 
3: A, CL, HAR, M, R
2: AD (ADAMS), BUS (BUSH), CLE (CLEVELAND), F, G, HARR (HARRISON), JO (JOHNSON), P, RO (ROOSEVELT), TA, TR (TRUM), W
1: Every other valid string  

Alphabetical list of surnames

Adams, Adams, Arthur, Buchanan, Buren, Bush, Bush, Carter, Cleveland, Cleveland, Clinton, Coolidge, Eisenhower, Fillmore, Ford, Garfield, Grant, Harding, Harrison, Harrison, Hayes, Hoover, Jackson, Jefferson, Johnson, Johnson, Kennedy, Lincoln, Madison, McKinley, Monroe, Nixon, Obama, Pierce, Polk, Reagan, Roosevelt, Roosevelt, Taft, Taylor, Truman, Trump, Tyler, Washington, Wilson
-------- Same list (obviously) ----------- 
'Adams', 'Adams', 'Arthur', 'Buchanan', 'Buren', 'Bush', 'Bush', 'Carter', 'Cleveland', 'Cleveland', 'Clinton', 'Coolidge', 'Eisenhower', 'Fillmore', 'Ford', 'Garfield', 'Grant', 'Harding', 'Harrison', 'Harrison', 'Hayes', 'Hoover', 'Jackson', 'Jefferson', 'Johnson', 'Johnson', 'Kennedy', 'Lincoln', 'Madison', 'McKinley', 'Monroe', 'Nixon', 'Obama', 'Pierce', 'Polk', 'Reagan', 'Roosevelt', 'Roosevelt', 'Taft', 'Taylor', 'Truman', 'Trump', 'Tyler', 'Washington', 'Wilson'
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Welcome to challenge writing! My OCD is a bit bothered by the fact that Cleveland and Cleveland are the same Cleveland, who was simply elected to two non-consecutive terms, but since there are already answers it's best not to change it now. You may want to post future challenges in the Sandbox first to iron out the wrinkles. Otherwise, this looks like a well-specified challenge. \$\endgroup\$
    – DLosc
    Commented Sep 19, 2018 at 19:46
  • 1
    \$\begingroup\$ I feel I should point out that the name is "Van Buren", not "Buren". Too late to change now, of course. \$\endgroup\$
    – user48543
    Commented Sep 20, 2018 at 14:57

5 Answers 5

5
\$\begingroup\$

Python 2, 179 175 173 bytes

lambda s:sum(any(s==X[:len(s)]for X in x.split())for x in[A,B,'A CL HAR M R '+B,'ADAMS BUSH CLEVELAND F G HARRISON JOHNSON P ROOSEVELT TA TRUM W M',s])
A='C H T';B='BU J '+A

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ tf is this. what is this sorcery \$\endgroup\$ Commented Sep 19, 2018 at 15:18
5
\$\begingroup\$

Python 3, 141 140 111 106 97 bytes

Input is in title case, 114 bytes with lowercase input.

-5 bytes thanks to Value Ink.

lambda n:(f"AdamsBushCleveFGHarriJohnsPRooseTaTrumW{0:31}AClHarMR{0:36}BuJCHT".rfind(n[:5]))/39+2

Try it online!

\$\endgroup\$
1
4
\$\begingroup\$

JavaScript (ES6), 109 105 102 bytes

Expects the input string in title case.

s=>-~"CHT4BuHaJ3AClHarMR2AdamsBushCleveFGHarriJohnsPRooseTaTrumW1".match(s.slice(0,5)+'.*?(\\d)|1')[1]

Try it online!

How?

We use a lookup string holding prefixes between 1 and 5 characters, along with digits describing the corresponding number of occurrences:

"CHT4BuHaJ3AClHarMR2AdamsBushCleveFGHarriJohnsPRooseTaTrumW1"
 {5-}{4---}{3------}{2-------------------------------------}

We use the following regular expression on this string and apply -~ on the second entry returned by .match():

                                     /PREFIX.*?(\d)|1/
                                      \____/\_/\__/\/
                                        |    |  |  |
the 5 first characters of the input ----+    |  |  |
the shortest possible string (lazy match) ---+  |  |
a digit (captured) -----------------------------+  |
fallback: look for '1' instead (last character) ---+

Examples:

  • "C"['CHT4', '4']-~'4' = 5
  • "Har"['HarMR2', '2']-~'2' = 3
  • "Lincoln"['1', undefined]-~undefined = 1
\$\endgroup\$
3
\$\begingroup\$

R, 156 bytes

function(k)max(which(mapply(grepl,z<-paste('',k),c(z,' ADAMS BUSH CLEVELAND F G HARRISON JOHNSON P ROOSEVELT TA TRUM W',' A CL HAR M R',' BU J',' C H T'))))

Try it online!

Inspired by @TFeld Python 2 solution

\$\endgroup\$
0
\$\begingroup\$

Ruby, 132 100 92 bytes

->s{1+"AdamsBushCleveFGHarriJohnsPRooseTaTrumWAClHarMMRBuBuJJCCHHTT".scan(/#{s[0,5]}/).size}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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