57
7
Challenge
Write a program that takes as input a day from April 30, 1789 to August 21, 2019 and returns as output who was president of the USA that day.
Notes
For the list of US presidents, the format of their names, and the period of their presidency, see below:
April 30, 1789 - March 4, 1797 George Washington
March 4, 1797 - March 4, 1801 John Adams
March 4, 1801 - March 4, 1809 Thomas Jefferson
March 4, 1809 - March 4, 1817 James Madison
March 4, 1817 - March 4, 1825 James Monroe
March 4, 1825 - March 4, 1829 John Quincy Adams
March 4, 1829 - March 4, 1837 Andrew Jackson
March 4, 1837 - March 4, 1841 Martin Van Buren
March 4, 1841 - April 4, 1841 William Henry Harrison
April 4, 1841 - March 4, 1845 John Tyler
March 4, 1845 - March 4, 1849 James K. Polk
March 4, 1849 - July 9, 1850 Zachary Taylor
July 9, 1850 - March 4, 1853 Millard Fillmore
March 4, 1853 - March 4, 1857 Franklin Pierce
March 4, 1857 - March 4, 1861 James Buchanan
March 4, 1861 - April 15, 1865 Abraham Lincoln
April 15, 1865 - March 4, 1869 Andrew Johnson
March 4, 1869 - March 4, 1877 Ulysses S. Grant
March 4, 1877 - March 4, 1881 Rutherford B. Hayes
March 4, 1881 - September 19, 1881 James A. Garfield
September 19, 1881 - March 4, 1885 Chester A. Arthur
March 4, 1885 - March 4, 1889 Grover Cleveland
March 4, 1889 - March 4, 1893 Benjamin Harrison
March 4, 1893 - March 4, 1897 Grover Cleveland
March 4, 1897 - September 14, 1901 William McKinley
September 14, 1901 - March 4, 1909 Theodore Roosevelt
March 4, 1909 - March 4, 1913 William Howard Taft
March 4, 1913 - March 4, 1921 Woodrow Wilson
March 4, 1921 - August 2, 1923 Warren G. Harding
August 2, 1923 - March 4, 1929 Calvin Coolidge
March 4, 1929 - March 4, 1933 Herbert Hoover
March 4, 1933 - April 12, 1945 Franklin D. Roosevelt
April 12, 1945 - January 20, 1953 Harry S. Truman
January 20, 1953 - January 20, 1961 Dwight D. Eisenhower
January 20, 1961 - November 22, 1963 John F. Kennedy
November 22, 1963 - January 20, 1969 Lyndon B. Johnson
January 20, 1969 - August 9, 1974 Richard Nixon
August 9, 1974 - January 20, 1977 Gerald Ford
January 20, 1977 - January 20, 1981 Jimmy Carter
January 20, 1981 - January 20, 1989 Ronald Reagan
January 20, 1989 - January 20, 1993 George H. W. Bush
January 20, 1993 - January 20, 2001 Bill Clinton
January 20, 2001 - January 20, 2009 George W. Bush
January 20, 2009 - January 20, 2017 Barack Obama
January 20, 2017 - Incumbent Donald Trump
A presidency is intended as first day included and last day excluded. For example, "March 4, 1861 - April 15, 1865; Abraham Lincoln" means that Lincoln was president from March 4, 1861 to April 14, 1865.
This is code-golf, the shortest program in each language wins.
Example
Input: "February 7, 1865" Output: "Abraham Lincoln"
18Hi there, welcome to CGCC! This looks like a nice challenge, but all challenges should be self-contained. Instead of the Wikipedia link, please have a list here of all presidents and their periods (preferably in code-block, so a scroll-bar will appear). Also, I recommend having a flexible input-format, so people could input as a string in whatever format, three loose integers, a date object, etc. how they see fit in their language of choice. – Kevin Cruijssen – 2019-08-22T11:27:00.113
3
Also, for future challenges, it's best to first post them in the Sandbox of Proposed Challenges. There you can get feedback and make the challenge better, before posting it to main. Enjoy your stay! :)
– Kevin Cruijssen – 2019-08-22T11:27:13.4531@KevinCruijssen OK – Kate – 2019-08-22T11:52:51.803
1Great! I took the liberty to format the post a bit, and added some more relevant tags. +1 from me, though. Nice first challenge. – Kevin Cruijssen – 2019-08-22T12:00:46.207
@KevinCruijssen Is this really a [decision-problem]? – Esolanging Fruit – 2019-08-22T17:22:42.667
@EsolangingFruit Tbh, I'm not 100% sure. I saw [decision-problem] as a challenge where you get a certain input, and based on some checks, have a specific (hard) output. In this case it would be the checks by date, and outputting the hardcoded president-names. But maybe I'm misunderstanding the tag.. – Kevin Cruijssen – 2019-08-22T17:25:00.950
2@KevinCruijssen I believe this would be a decision problem if you were also given a president and had to determine whether the pairing was correct. As it is, it isn't really about deciding anything; it's more about information compression, so I've removed the tag. – FryAmTheEggman – 2019-08-22T18:31:55.537
@FryAmTheEggman Sounds good, sorry for the confusion. :) – Kevin Cruijssen – 2019-08-22T18:52:56.413
11How flexible is the IO format? – qwr – 2019-08-22T19:38:03.270
2"Write a program that takes as input a day from April 30, 1789 to August 21, 2019" - so taking the day as a single integer rather than a date is OK I assume? (i.e.
1
being April 30, 1789 and84098
being August 21, 2019). Answers are already takingYYYYMMDD
strings, so I also assume that is also OK? – Jonathan Allan – 2019-08-22T20:01:34.17315
WolframAlpha["president on "<>#]&
I'm sure pure Mathematica has a builtin somewhere but until I find what it's called I'm sticking with W|A. – Pavel – 2019-08-22T22:07:39.3238@Pavel in that case make it
WolframAlpha["US president on "<>#]&
cause for me the output is not correct – jonatjano – 2019-08-23T08:09:02.340I don't think [tag:kolmogorov-complexity] applies either, since the output depends on the input. I think it would apply if the challenge were just "output a list of presidents and their terms" – Jo King – 2019-08-23T09:19:04.627
@jonatjann answers are allowed to depend on the environment. In this case, geographical location. My suggestion would be to post both alternatives in the same answer. – John Dvorak – 2019-08-23T11:29:11.733