谁是美国总统?


56

挑战

编写一个程序,将1789年4月30日至2019年8月21日的一天作为输入,并返回当日为美国总统的输出。

笔记

有关美国总统名单,姓名格式和任期,请参见以下内容:

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

总统任期包括第一天和最后一天。例如,“ 1861年3月4日至1865年4月15日;亚伯拉罕·林肯”表示林肯从1861年3月4日至1865 年4月14日担任总统。

这是代码高尔夫球,每种语言中最短的程序胜出。

输入:“ 1865年2月7日”输出:“亚伯拉罕·林肯”


18
您好,欢迎来到CGCC!这看起来是一个不错的挑战,但是所有挑战都应该是独立的。而不是Wikipedia链接,请在此处列出所有总统及其任期(最好是在代码段中,这样会出现一个滚动条)。另外,我建议您使用灵活的输入格式,这样人们就可以以任何格式的字符串,三个松散的整数,一个日期对象等形式输入字符串,以便他们选择适合自己的语言。
凯文·克鲁伊森

3
另外,对于未来的挑战,最好先将它们发布在“ 提议的挑战沙盒中。在将其发布到main之前,您可以在那里获得反馈并改善挑战。入住愉快!:)
凯文·克鲁伊森

11
IO格式的灵活性如何?
qwr

15
WolframAlpha["president on "<>#]&我敢肯定,纯Mathematica在某处具有内置函数,但是直到找到所谓的东西,我才坚持使用W | A。
帕维尔

8
在这种情况下,@ Pavel WolframAlpha["US president on "<>#]&会使我的输出不正确
jonatjano

Answers:


27

PHP,936字节

for($a=explode('-',gzinflate(base64_decode('dVTRbtswDPwV/YANKXZq+zFJ1wRts3VZtgB7Y2Mm1ipLAO0089+PtNIuHbAnAzySd+RRNkVZ6XxSJUsMdES1g66x/tgHn5iiKnSms+Q+NF7Nami7xJTajLFtE1ro1D0eDkidZJe6itnQYqfWUNsYNsV1OHgKyNHJ9G/rryfr98M7wyT2mfma8KzuYf8SG2Wx0Rqot179AK/mJ0JB8qhpZ52z0KoVehrUCoguEhjP38i2g0OS2PRK1kOqnoJ7kXAk/wn7BrjJFgYXJH2qdaHLZM0UQLW6428bSCaZZmPFHYF/cSzsySLtR+B68PmJG3oQNTdR7eyZoGG1jzx8cCMw1bnJ3wdntVH+TdT03Q1dx62+pWrJZD0jRaTYnPoG6RBY2DzlwQeUPZbmin/GRUAHi66OUGXKZNFg1yMJOKO+OcmgZdzLksIrIwuHr+jAj0VRxhz9L2h50KsFV9n/ii4n9GbNev9gvcMhMZUWDXJIGGrepNqE0EldL1j10dBwlqVv4SCgiWS7EGoKZ8VJo4hqcrkCloVeLWURVPMtC5RpPtxkAe6VhS9CcLY+ogCRaIX0jNQzkQzA8ewfU2/TD/pyccoksoFB/NjSqRVvKz4GY6rk9myPTS9Vn2yHvgnnsSs7L+h4h3epekDvsZZd3GTGTEzyOPg6ePHw3fyKzZeajZWDrNVn+3sMFzlPVPKrJXB8jmy9BIvY37btoBb8TkbWMrJugpfUDcJxlFrGxpd3v0rVLuUr7RqGqjjFnNfPZlovf4OJ1ua64C2b47HRHIifqvryDC1w1EQtfwA=')));$d=$a[+$i++];)date(Ymd,strtotime($argn))>$d?$m=$a[$i++]:die($m);echo'Donald Trump';

在线尝试!


测试: 在线尝试!

用于测试的代码具有相同的逻辑,但是用代替die(...)return...因此我可以循环测试。


我创建了一个字符串,其开始日期为总统开始时间减去1天,格式为YYYYMMDD,名称为总裁,并以分隔-。例:17890429-George Washington-17970303-John Adams-18010303-Thomas Jefferson-...

该字符串使用gzdeflate压缩,压缩后的输出也转换为base-64,因此所有字符都是可打印的ASCII,我可以在此处发布它们或在TIO中使用它们。

在脚本中,字符串被分割-并循环。对于每个循环迭代:

  • $d设置为当前迭代值(这是总裁的开始日期-1天),并且循环变量($i)增加1。
  • 如果输入日期大于$d,则将$m其设置为下一个迭代值(这是的总裁名称$d),然后循环变量($i)再次加一。这允许循环获取总裁姓名,但在迭代中跳过它们。
  • 如果输入日期不大于$d,则表示先前的迭代是正确的总裁,因此$m使用die函数进行打印,这将在打印名称后停止整个程序。

最后,如果循环结束(表示没有die调用),则将打印出最后一位主席的姓名,因为我们知道之前的所有总裁都没有匹配项(因此,之后的任何内容January 19, 2017)都将打印最后一位主席的姓名,即使January 1, 3999



我不知道PHP,但是您可以删除base64并插入原始的不可打印字符(您可以提供hexdump)。
MilkyWay90

@RyanMoore,感谢更好的压缩字符串,可悲的是我不再控制这个答案,我的目标已被删除(在本修订版中,该目标获得了很多赞成(也有一些反对)。因此,即使有高尔夫的可能性,我也不会编辑或打高尔夫球。
夜间2

@ MilkyWay90,这完全有可能。但是我故意发布此版本,以便在此处和TIO链接中提供可读的代码。基本上,这并不是硬性回答。
2

15

Bash1229 1124字节

-103字节,带有@GammaFunction的出色注释,删除了几个非生产性的替换(Adams),并重构了代码以使用相对于上任日期的上一日期而不是绝对日期的日期偏移量。

M=(. Ja F Mar Ap May Jun Jul Au S O N D)
A=40000
B=80000
s=son
a=James
c=Andrew
d=William
e=Harri$s
f=Franklin
g=George
h=Grover#Cleveland
i=Roosevelt
j=John
k=#W.#Bush
X="304:$g#Washington
$A:$j#Adams
$B:Thomas#Jeffer$s
$B:$a#Madi$s
$B:$a#Monroe
$A:$j#Quincy#Adams
$B:$c#Jack$s
$A:Martin#Van#Buren
100:$d#Henry#$e
39900:$j#Tyler
$A:$a#K.#Polk
10405:Zachary#Taylor
29595:Millard#Fillmore
$A:$f#Pierce
$A:$a#Buchanan
40111:Abraham#Lincoln
39889:$c#$j$s
$B:Ulysses#S.#Grant
$A:Rutherford#B.#Hayes
615:$a#A.#Garfield
39385:Chester#A.#Arthur
$A:$h
$A:Benjamin#$e
$A:$h
40610:$d#McKinley
79390:Theodore#$i
$A:$d#Howard#Taft
$B:Woodrow#Wilson
20598:Warren#G.#Harding
59402:Calvin#Coolidge
$A:Herbert#Hoover
120108:$f#D.#$i
79708:Harry#S.#Truman
$B:Dwight#D.#Eisenhower
21002:$j#F.#Kennedy
58998:Lyndon#B.#$j$s
50789:Richard#Nixon
29211:Gerald#Ford
$A:Jimmy#Carter
$B:Ronald#Reagan
$A:$g#H.$k
$B:Bill#Clinton
$B:$g$k
$B:Barack#Obama"
T=($1)
D=0${T[1]%,}
for((;;)){ [[ $T =~ ${M[++i]} ]]&&break;}
N=0$i
S=$[${T[2]}-1797]${N: -2}${D: -2}
for z in $X;{ O=$[O+${z%:*}];[ $S -lt $O ]&&z=${z//#/ }&&echo ${z#*:}&&exit;}
echo Donald Trump

在线尝试!

很多手动压缩:)


其他一些:摆脱掉X=($X),循环,就像for z in $X在$ IFS上拆分$ X一样。使用[ -lt ]和使用$T代替${T[0]}最后几行,总体应为1191字节
GammaFunction

1092!我简化了月份解析(例如,如果我们已经通过6月并且以J开头,则必须在7月之前。我还将测试更改为[ ${T#${M[++i]}*} ]。现在我们可以完全省略12月了)。我还:用了几个名字的开头(这样做的:John意思是用所有定界符:代替#)。
GammaFunction

12

T-SQL,1169 981 979 977字节

天哪,我发现了SQL中的字符串压缩(在SQL 2016及更高版本中可用):

SELECT TOP 1 STUFF(value,1,8,'')
FROM d,STRING_SPLIT(
    CAST(DECOMPRESS(CAST('H4sIAAAAAAAEAHVT23LaMBD9FX1AxiMZg+1HIAWGhDaltMz0bYMXrEaWOmsc6r/vrpw27kOHB8ZnL+fs2VWqTa6NKe+DB1epA3XNz7tU61LABRCcXtSnZ2hAQCPgGgNdUB0Tteja+s6U5STmWufU0ll/DZ7BohzlbpJRehG77Ae+PcIFJD+PKra2aXq1BLoiCZjpQhdrJEldBaoYm8XGe3uqgSr10f6KdLOJMal57H0VvFokahtq3w6RSCffapWoB/Qeq57xaVR9f7OX+qruE/XBtujrcIvE2VRnxmyAqFdfkuhKVDmZaP6tCPwLTypl+xBafEV35WhaSnSD9Ix0VZsQXmOzdMJTmCW4Vy5ZhuBsdUHBjaQfmQS9WieK6SrrLxwxkeYYQkXhpo7WDaPo2J8/nYWG+9/EgQOchZt3w2WHGkMVCMeyijIfl+1OD9Y77CUQadYkOnl1kg+ePS6KSLRA/wMa1iw+2CihKKb/KxH+Ylljy6tT80TN6Vp3NES4ZAsNtoKvgc4WndTkUdi+u9ZIZ96uLG4DPbYcm0UJX13ftlzHO1iz6TLNTFaTzX1FeHtfczGLLPNngpqHfLT+FJzg0/ydfdHx0XiI8L+LfLJIJxRc61wXO/ZKvF3xf8N2ciCLer6DnF3PpvcuyHDZ9L39Q6KegnsR1OiMUbm5Q++QBmi8O/TcZeTrJMrc8eGzmm/AN9zxVXBguKk/4/JrHPLTgVcYPnc8bK/mFTRinBkNvAuegqgfLucNhOqNVEdNhzo00Kotns9IMZAPFxO7v7XN+SSy9O/bh7bmS+WX/hut0W9jPwQAAA=='as XML).value('.','varbinary(max)'))AS varchar(max)),',')
WHERE LEFT(value,8)<i

不保存相当就像我希望它可能会,可能是由于代码需要转换/ deconvert量。

这是原始版本,等效于解压缩编码的字符串(1169字节)之后运行的代码:

SELECT TOP 1 SUBSTRING(value,9,99)
FROM d,STRING_SPLIT(
    '20170119Donald Trump,20090119Barack Obama,20010119George W. Bush,19930119Bill Clinton,19890119George H. W. Bush,19810119Ronald Reagan,19770119Jimmy Carter,19740808Gerald Ford,19690119Richard Nixon,19631121Lyndon B. Johnson,19610119John F. Kennedy,19530119Dwight D. Eisenhower,19450411Harry S. Truman,19330303Franklin D. Roosevelt,19290303Herbert Hoover,19230801Calvin Coolidge,19210303Warren G. Harding,19130303Woodrow Wilson,19090303William Howard Taft,19010913Theodore Roosevelt,18970303William McKinley,18930303Grover Cleveland,18890303Benjamin Harrison,18850303Grover Cleveland,18810918Chester A. Arthur,18810303James A. Garfield,18770303Rutherford B. Hayes,18690303Ulysses S. Grant,18650414Andrew Johnson,18610303Abraham Lincoln,18570303James Buchanan,18530303Franklin Pierce,18500708Millard Fillmore,18490303Zachary Taylor,18450303James K. Polk,18410403John Tyler,18410303William Henry Harrison,18370303Martin Van Buren,18290303Andrew Jackson,18250303John Quincy Adams,18170303James Monroe,18090303James Madison,18010303Thomas Jefferson,17970303John Adams,17890429George Washington',',')
WHERE LEFT(value,8)<i

换行符仅供阅读。

dINT一世YYYYMMDD

我用来保存字节的一些技巧:

  • 我对总裁的降序进行了排序,因此我不必添加ORDER BY在代码中
  • 我在每个学期的“开始”日期中减去了1,所以我可以使用<代替<=
  • 字符串分割比一个大列表短得多VALUES(),即使我必须用SUBSTRING和分开字符串LEFT
  • 编辑:通过更改保存了2个字节SUBSTRING()STUFF()

在SQL中使用Base64编码的压缩字符串

(已添加到SQL Tips线程中

因此,Microsoft 在SQL 2016中添加了COMPRESSDECOMPRESS函数来处理GZIP。问题是它返回a VARBINARY,尽管字节(存储在SQL VARBINARY字段中)字节较短,但较长以字符显示(原始十六进制)时,这使其不适合打高尔夫球。

转换为Base64显然是答案,但是找到一个简短的简单实现是一个挑战。我基于这个旧答案的版本,尽管它没有使用新的GZIP函数,但它满足了我们的大部分需求。我只需要在正确的位置插入新功能,然后将它打一点即可。

要在自己的代码中使用此方法:

--Run this to generate your compressed string:
DECLARE @s VARCHAR(MAX)='Your really long string goes right here'
SELECT CONVERT(VARCHAR(MAX),(SELECT CONVERT(VARBINARY(MAX),COMPRESS(@s))FOR XML PATH(''),BINARY BASE64))

--Use your compressed string and the following code to get back to the original:
DECLARE @e VARCHAR(MAX)='H4sIAAAAAAAEAIvMLy1SKEpNzMmpVMjJz0tXKC4pygRS6fmpxQpFmekZJQoZqUWpAGGwW5YnAAAA'
SELECT CAST(DECOMPRESS(CAST(@e as XML).value('.','varbinary(max)'))AS varchar(max))

那是75额外 73个字节的代码来解压缩编码的字符串,因此显然仅将其用于很长的字符串。

编辑:用CAST代替的解压缩代码中保存了2个字节CONVERT


12

Excel,1243 1206 1180字节

通过从所有日期中减去-37字节-通过
对减法值进行摆弄

=CHOOSE(MATCH(A1-65^4,{39805,119679,159679,239679,319679,399679,439679,519679,559679,559779,599679,639679,650084,679679,719679,759679,799790,839679,919679,959679,960294,999679,1039679,1079679,1119679,1160289,1239679,1279679,1359679,1380177,1439679,1479679,1599787,1679495,1759495,1780497,1839495,1890184,1919495,1959495,2039495,2079495,2159495,2239495,2319495}),"George Washington","John Adams","Thomas Jefferson","James Madison","James Monroe","John Quincy Adams","Andrew Jackson","Martin Van Buren","William Henry Harrison","John Tyler","James K. Polk","Zachary Taylor","Millard Fillmore","Franklin Pierce","James Buchanan","Abraham Lincoln","Andrew Johnson","Ulysses S. Grant","Rutherford B. Hayes","James A. Garfield","Chester A. Arthur","Grover Cleveland","Benjamin Harrison","Grover Cleveland","William McKinley","Theodore Roosevelt","William Howard Taft","Woodrow Wilson","Warren G. Harding","Calvin Coolidge","Herbert Hoover","Franklin D. Roosevelt","Harry S. Truman","Dwight D. Eisenhower","John F. Kennedy","Lyndon B. Johnson","Richard Nixon","Gerald Ford","Jimmy Carter","Ronald Reagan","George H. W. Bush","Bill Clinton","George W. Bush","Barack Obama","Donald Trump")

由于Excel不喜欢1900年之前的日期A1,因此输入使用的是格式的单元格YYYYMMDD

我不知道不会增加更多字节的日期或名称的压缩方法。“从所有日期减去[值]”方法可以应用于任何人的解决方案。


我使用相同的原理在Google表格中找到了1102字节的解决方案。Excel没有该Split功能,因此不可翻译。

=Index(Split("George Washington,John Adams,Thomas Jefferson,James Madison,James Monroe,John Quincy Adams,Andrew Jackson,Martin Van Buren,William Henry Harrison,John Tyler,James K. Polk,Zachary Taylor,Millard Fillmore,Franklin Pierce,James Buchanan,Abraham Lincoln,Andrew Johnson,Ulysses S. Grant,Rutherford B. Hayes,James A. Garfield,Chester A. Arthur,Grover Cleveland,Benjamin Harrison,Grover Cleveland,William McKinley,Theodore Roosevelt,William Howard Taft,Woodrow Wilson,Warren G. Harding,Calvin Coolidge,Herbert Hoover,Franklin D. Roosevelt,Harry S. Truman,Dwight D. Eisenhower,John F. Kennedy,Lyndon B. Johnson,Richard Nixon,Gerald Ford,Jimmy Carter,Ronald Reagan,George H. W. Bush,Bill Clinton,George W. Bush,Barack Obama,Donald Trump",","),Match(A4-65^4,{39805,119679,159679,239679,319679,399679,439679,519679,559679,559779,599679,639679,650084,679679,719679,759679,799790,839679,919679,959679,960294,999679,1039679,1079679,1119679,1160289,1239679,1279679,1359679,1380177,1439679,1479679,1599787,1679495,1759495,1780497,1839495,1890184,1919495,1959495,2039495,2079495,2159495,2239495,2319495}))

2
我不太了解Excel(我的本地Excel是荷兰语,所以我无法验证),但这是正确的吗?这不是在寻找完全匹配的日期,而不是低于日期的匹配吗?可以说19000101什么输出吗?
凯文·克鲁伊森

3
@KevinCruijssen Nope,MATCH()一个可选的第三个参数match_type,用于确定是否使用完全匹配。排除该参数将使用默认值,即“小于或等于”匹配项,因此此代码正确。
BradC

@BradC啊,不知道。感谢您的解释!向我+1。:)
凯文·克鲁伊森

1
从每个值中减去一个固定值的妙招,我必须看看它是否也对我有用。
BradC

9

05AB1E587个 584 568 字节

.•6}©ß þz∊‘Îнúj7a>º ·,$_+šüÑßu?PδIëàá©ā.ǝ/¬j«Ížr‹þVwô¨"¶&ˆ₃ǝαh^¼ì∞öîNÊÌ.ÚáýиÇ÷©Mèé…—¾…тŒ¹/ÈÂÓŒš/«ª³PÉÇJúΓÁ¦';X·₅ï»/„q<ƶÚÙÊtÔιuš2βƒ¼ï\~ö…3ihD·¢ðΘmζ8É9Ïγ=^$¨Ä₆ËÂÊPv5+}þ¯ÑX4hÖdÀÒuàнº₁›ǝΘ³P@¥ùnðT“É)ó_“:Èx³«äƒæ¤∞Wgþ¶š}ô|ićcÁ‘.·ƒp>Z¬Aéγê‰ù‰¥Ó₃#/›pÐT¹Â¹J3—ΛΓkÙ»(áÌ›₃z@ìqW›Â"™raÒ›₂%ïx{
_Ωb≠¹₁ľÊÌ›}çºô|ôëKÝǝÚï,‚£=A©áāY∊~"KøVóΓyΛJÚΘ·ˆUΩ₂TнîëнÐQнĆ!!¶%Ý₃ó¸ß0ûиî€AvÙ∍Õ
'«₂θSè‡H¿ÏLôǝāqaмÑIƶ%ŸSdUbåÐÉιδJ¯4₆>θçàƶñwC¨OVÀàczdRæs?5ʒP8“ƒÍÜðαŸÝf,ƒη,™`ÿeиäƵθC≠ýzX‡)и∞úø+þćÎÌвĆ挱ðí1•™„qz„K.‡„  ¡•gpöz&»XZ1rć¾{¦¯Û¿p~Θø•Ƶaв₄R«•e½òœu#ÈĆÔFƵ•Ƶ`вŽcƵ«•9…åœòFýLÂ]FÜÔĀ=Ƶ®5¿ùK»Ωм••Rwg•в««•1L₄Æ•+I‹Oè

输入是采用格式的串联日期yyyyMMdd(即20190821,2019年8月21日)。

-16个字节感谢@Grimy

在线尝试验证更多测试用例

说明:

我们首先创建按时间顺序排列的总裁列表:

.•6}©...ðí1            # Push compressed string "george washington  john adams  thomas jefferson  james madison  james monroe  john quincy adams  andrew jackson  martin van buren  william henry harrison  john tyler  james kz polk  zachary taylor  millard fillmore  franklin pierce  james buchanan  abraham lincoln  andrew johnson  ulysses sz grant  rutherford bz hayes  james az garfield  chester az arthur  grover cleveland  benjamin harrison  grover cleveland  william mcqinley  theodore roosevelt  william howard taft  woodrow wilson  warren gz harding  calvin coolidge  herbert hoover  franklin dz roosevelt  harry sz truman  dwight dz eisenhower  john fz kennedy  lyndon bz johnson  richard nixon  gerald ford  jimmy carter  ronald reagan  george hz wz bush  bill clinton  george wz bush  barack obama  donald trump"
                       # Titlecase each word in the string
qzK.‡                 # Replace all lowercase "q" with "K" (for McKinley),
                        # and all lowercase "z" with "."
                        # (only lowercase letters and spaces can be compressed as string)
  ¡                    # Split the string on "  " (two spaces)

然后,我们以格式yyyyMMdd(整数与该列表无关)的形式将所有日期的列表创建为整数:

gpözXZ1rć¾{¦¯Û¿p~Θø• # Push compressed integer 57036763189461803778321913240836786322696217070172
  Ƶa                    # Push compressed integer 137
    в                   # Convert the large integer to base-137 as list:
                        #  [4,12,20,28,32,40,44,48,52,56,60,64,72,80,84,88,92,96,100,112,116,124,132,136]
     R                 # Push 1000, and reverse it to "0001"
       «                # And concat it to each item:
                        #  [40001,120001,200001,280001,320001,400001,440001,480001,520001,560001,600001,640001,720001,800001,840001,880001,920001,960001,1000001,1120001,1160001,1240001,1320001,1360001]
                        # (this is for all the March 4th dates)
e½òœu#ÈĆÔFƵ•           # Push compressed integer 47362342587646909879438102
  Ƶ`                    # Push compressed integer 223
    в                   # Convert the large integer to base-223 as list:
                        #  [155,163,171,179,183,191,195,203,211,219,222]
     ŽcƵ                # Push compressed integer 9817
        «               # And concat it to each item:
                        #  [1559817,1639817,1719817,1799817,1839817,1919817,1959817,2039817,2119817,2199817,2229817]
                        # (this is for all the January 20th dates)
1厨£ÆÊÂç-iYúΛȾEW½Ω≠tW
                        # Push compressed integer 42494608868313599134973321717434089414197466371857150907
  Rwg                 # Push compressed integer 1770507
       в                # Convert the large integer to base-1770507 as list:
                        #  [440101,530406,680112,840616,1040611,1260499,1480109,1660819,1770506]
                        # (these are the remaining dates; i.e. those who died earlier)
««                      # Merge the three lists together:
                        #  [40001,120001,200001,280001,320001,400001,440001,480001,520001,560001,600001,640001,720001,800001,840001,880001,920001,960001,1000001,1120001,1160001,1240001,1320001,1360001,1559817,1639817,1719817,1799817,1839817,1919817,1959817,2039817,2119817,2199817,2229817,440101,530406,680112,840616,1040611,1260499,1480109,1660819,1770506]
  1L₄Æ•                # Push compressed integer 17970302
        +               # Add it to each item in the list:
                        #  [18010303,18090303,18170303,18250303,18290303,18370303,18410303,18450303,18490303,18530303,18570303,18610303,18690303,18770303,18810303,18850303,18890303,18930303,18970303,19090303,19130303,19210303,19290303,19330303,19530119,19610119,19690119,19770119,19810119,19890119,19930119,20010119,20090119,20170119,20200119,18410403,18500708,18650414,18810918,19010913,19230801,19450411,19631121,19740808]

然后,根据列表中的日期,使用输入来确定输出的总统姓名:

I                      # Check for each value in the integer-list if the Input is smaller
                        # (1 if truthy; 0 if falsey)
  O                     # Sum these to get the amount of truthy values
   è                    # And use that to (0-based) index into the president-names list
                        # (after which the result is output implicitly)

请参阅我的05AB1E技巧(部分如何压缩不属于字典的字符串?如何压缩大整数?如何压缩整数列表?了解所有压缩的字符串,整数和列表如何工作。


1
@Grimy我无法真正复制它,因此在看到您的操作后不得不自己再次压缩它。我检查了每个总统的名字,所有字母都被取走了。但是先保存标题字节,然后使用不再使用的小写字母转换为点和大写字母的好方法。谢谢-16!
凯文·克鲁伊森

这些名称中有很多是字典单词,因此可以带来很大的改进。首先,这是-32
Grimmy

1
@Grimy下周可能会尝试这种方法。目前正要吃饭,由于截止日期,明天我要忙于工作。
凯文·克鲁伊森

1
464。这仍然是可改进的,但是我认为它不能超过果冻426。他们的字符串压缩效果更好。
Grimmy

7

果冻 431个428 427  426 字节

“¡Ẹị’ḃ4ż“ƓĠƒƝƊ!ɓa(‘ḅ⁹
“FQIȥ’ḃ8Ėẋ/€<8ż¢F1,303yo119
“f×Ð<Mk7LqƇ%_¡¤cċ⁴ẏ’Do13’⁽¤.;Äż¢ḅȷ4<VSị“¡ṀÑṅyȮÇæjRṂ~ɠƥ⁺'Ḃġ"Ƈ~Ị'9ƒȮṃ$ḶPȯṅṡḊl¥JƊ€ẊḥSṣọwɦṡ5ėṂU #Fɲẓ÷iḄþ%~%ñçŒfƒƙɼ=ɓ⁼¡⁻Q`⁵¡ḍ36)żȥÑR\ḋ⁾ḶƤ0ÞṘḳ÷¦IṂ\}ƊĊÐḳŻƈɦ!zɠ⁺ƈṖṭW⁸ƥṛU|ḄØ^ẓfḂḣƈCȦ8ɱḤH⁵AKṢzȯỵƭḊ:TʠƬ/~ƝÑȥgṖ(ṁȯDƲṫỴftlBOȧ6Ụiṃıẉ_ƈvh5ṄUɓạẓDẠẸıṣƥ"bçȥ¿çOġQẹİẊẸs*⁶ż,Ḍg®ƲW×ɗ,ẏ¡Ḅ5M5)ṀạƈCṛʂẎŒƬwġ⁶NẠṆRƇsẊ<fŀȯ⁵øȤ\⁾.>ḋTṖĠUṇŻṬTṖræ_TçṡȤOʠƥðȦe]ƈ⁵cṆWƭƲ>`½n¿O¡ṭOþV¤ṡẊ²rdS€ė)ƘIÆ&ṙḞ¬5[pṁȥLṘVỴɠ»ḲṚƑ”.ẋṭƲ€KỴ¤

接受字符串的完整程序,该字符串YYYYMMDD会打印出总统的姓名。

在线尝试!或参见(凯文·克鲁伊森(Kevin Cruijssen's))的测试套件

怎么样?

“¡Ẹị’ḃ4ż“...‘ḅ⁹ - Link 1, getNonStandardEndMonthDays: no arguments
“¡Ẹị’           - base 250 literal       = 106467
     ḃ4         - to bijective base 4    = [1,2,1,3,3,3,1,4,3]
        “...‘   - code-page indices      = [147,196,158,150,145,33,155,97,40]
       ż        - zip together           = [[1,147],[2,196],...,[3,40]]
              ⁹ - built-in literal       = 256
             ḅ  - from base (vectorises) = [403,708,414,918,913,801,411,1121,808]

“FQIȥ’ḃ8Ėẋ/€<8ż¢F1,303yo119 - Link 2, getEndMonthDays: no arguments
“FQIȥ’                      - base 250 literal    = 1114518671
      ḃ8                    - to bijective base 8 = [8,2,3,3,4,3,2,2,1,7]
        Ė                   - enumerate           = [[1,8],[2,2],[3,3],[4,3],[5,4],[6,3],[7,2],[8,2],[9,1],[10,7]]
           €                - for each:
          /                 -   reduce with: 
         ẋ                  -     repeat          = [[1,1,1,1,1,1,1,1],[2,2],[3,3,3],[4,4,4],[5,5,5,5],[6,6,6],[7,7],[8,8],9,[10,10,10,10,10,10,10]]
            <8              - less than 8?        = [[1,1,1,1,1,1,1,1],[1,1],[1,1,1],[1,1,1],[1,1,1,1],[1,1,1],[1,1],[0,0],0,[0,0,0,0,0,0,0]]
               ¢            - call last Link (1)  = [403,708,414,918,913,801,411,1121,808]
              ż             - zip together        = [[[1,1,1,1,1,1,1,1],403],[[1,1],708],[[1,1,1],414],[[1,1,1],918],[[1,1,1,1],913],[[1,1,1],801],[[1,1],411],[[0,0],1121],[0,808],[0,0,0,0,0,0,0]]
                F           - flatten             = [1,1,1,1,1,1,1,1,403,1,1,708,1,1,1,414,1,1,1,918,1,1,1,1,913,1,1,1,801,1,1,411,0,0,1121,0,808,0,0,0,0,0,0,0]
                 1,303      - pair literal        = [1,303]
                      y     - translate           = [303,303,303,303,303,303,303,303,403,303,303,708,303,303,303,414,303,303,303,918,303,303,303,303,913,303,303,303,801,303,303,411,0,0,1121,0,808,0,0,0,0,0,0,0]
                        119 - literal             = 119
                     o      - logical OR          = [303,303,303,303,303,303,303,303,403,303,303,708,303,303,303,414,303,303,303,918,303,303,303,303,913,303,303,303,801,303,303,411,119,119,1121,119,808,119,119,119,119,119,119,119]

“...’Do13’⁽¤.;Äż¢ḅȷ4<VSị“...»ḲṚƑ”.ẋṭƲ€KỴ¤ - Main Link: list of characters, X
“...’                                     - base 250 literal    = 5999595155245555951555559593750993764595999
     D                                    - to base 10          = [5,9,9,9,5,9,5,1,5,5,2,4,5,5,5,5,9,5,1,5,5,5,5,5,9,5,9,3,7,5,0,9,9,3,7,6,4,5,9,5,9,9,9]
       13                                 - literal             = 13
      o                                   - logical OR          = [5,9,9,9,5,9,5,1,5,5,2,4,5,5,5,5,9,5,1,5,5,5,5,5,9,5,9,3,7,5,13,9,9,3,7,6,4,5,9,5,9,9,9]
         ’                                - decrement           = [4,8,8,8,4,8,4,0,4,4,1,3,4,4,4,4,8,4,0,4,4,4,4,4,8,4,8,2,6,4,12,8,8,2,6,5,3,4,8,4,8,8,8]
          ⁽¤.                             - literal             = 1797
             ;                            - concatenate         = [1797,4,8,8,8,4,8,4,0,4,4,1,3,4,4,4,4,8,4,0,4,4,4,4,4,8,4,8,2,6,4,12,8,8,2,6,5,3,4,8,4,8,8,8]
              Ä                           - cumulative sums     = [1797,1801,1809,1817,1825,1829,1837,1841,1841,1845,1849,1850,1853,1857,1861,1865,1869,1877,1881,1881,1885,1889,1893,1897,1901,1909,1913,1921,1923,1929,1933,1945,1953,1961,1963,1969,1974,1977,1981,1989,1993,2001,2009,2017]
                ¢                         - call last Link (2)  = [303,303,303,303,303,303,303,303,403,303,303,708,303,303,303,414,303,303,303,918,303,303,303,303,913,303,303,303,801,303,303,411,119,119,1121,119,808,119,119,119,119,119,119,119]
               ż                          - zip together        = [[1797,303],[1801,303],[1809,303],[1817,303],[1825,303],[1829,303],[1837,303],[1841,303],[1841,403],[1845,303],[1849,303],[1850,708],[1853,303],[1857,303],[1861,303],[1865,414],[1869,303],[1877,303],[1881,303],[1881,918],[1885,303],[1889,303],[1893,303],[1897,303],[1901,913],[1909,303],[1913,303],[1921,303],[1923,801],[1929,303],[1933,303],[1945,411],[1953,119],[1961,119],[1963,1121],[1969,119],[1974,808],[1977,119],[1981,119],[1989,119],[1993,119],[2001,119],[2009,119],[2017,119]]
                  ȷ4                      - literal 10^4        = 10000
                 ḅ                        - from base           = [17970303,18010303,18090303,18170303,18250303,18290303,18370303,18410303,18410403,18450303,18490303,18500708,18530303,18570303,18610303,18650414,18690303,18770303,18810303,18810918,18850303,18890303,18930303,18970303,19010913,19090303,19130303,19210303,19230801,19290303,19330303,19450411,19530119,19610119,19631121,19690119,19740808,19770119,19810119,19890119,19930119,20010119,20090119,20170119]
                     V                    - evaluate (X)        (i.e. X -> YYYYMMDD)
                    <                     - less than? (vectorises)
                      S                   - sum                 (a number between 0 and 44 inclusive)
                                        ¤ - nilad followed by link(s) as a nilad:
                        “...»             -   compression of dictionary words and strings
                             Ḳ            -   split at spaces
                                     €    -   for each (part):
                                    Ʋ     -     last four links as a monad:
                               Ƒ          -        invariant under?:
                              Ṛ           -          reverse (true for initials only)
                                ”.        -        literal = '.'
                                  ẋ       -        repeat
                                   ṭ      -        tack (the part - e.g. [['A'],'.'])
                                      K   -   join (back up) with spaces
                                       Ỵ  -   split at newline characters
                       ị                  - index into (that list) (1-indexed & modular)
                                          - implicit (smashing) print

再加上我一个。这看起来很棒
MilkyWay90

谢谢!花了相当长的时间来整理。
乔纳森·艾伦

5

果冻,454字节

“¡#<Ḷ“¡D{“ß“®“c“ƒⱮ“¡“(Ȥ“u^“£“ɠn“ƒ⁺“¥“¢r“ƈ⁶“©“ƙm“¡B⁻“¤“Pẋ“ḣ¢“¡“¡ẓk“¡CẆ“¢“S¢“ėẋ“ṄỴ“t<“Œ“¦’ḃ2$¹<?€ȷ2F4ȷ4,8ȷ4ĖF¤yÄ<⁸TṪị“€`ɲ×ƥF7ȯẒ⁼ẈẎu⁻ɠṇẠẈƊ⁻QḲỌ ỊƭʠµḢƊCṾr⁻=ßạḌ`^ḌX§®tṭN£jx,ƈ=suĖɼ_ḍf9*ÆṙỊṪṄ Jʋ ȯṡẉ ỌẏⱮøÞ⁴xḤżḅñ£ṡṭʂMṚ¶ṇwıƲṁEnÄ`:®ƥʂ_xd⁶ŒĖṁƙƝg°ṂʠkṫhO5Ịḅḳ³'ø⁵aẏṄKḂẆZØ4JḌþḂ⁸⁸XlIVḤOḲØṢÐ!}?Ọ°ḲẆiḢAr`ġƘ⁸ḷ[¿T§ẓ⁵|ḍdɓ^ṭƲfỴĖġJw(Ȯ:⁺k=£ŀnṗ*'hẹḋŻZOLæزẋ⁼ẏ&ı6ḣṗẈƥ¡:9D¢=G§⁴eỤ6wẈ6ṗw6⁺zOẉ}OṆƲdṃ'ıẠ£¤þ4¢1ȷfɓ.ṪpṀ⁴Ṡtṁ⁷ẇlŒ!ṠyaṪ66xvḍgġ¤goḍRẒ³żẎịṣ6ĿⱮ}ḲkƒiL²?Ḃ¿ḥƝ¤dėḤ½Ṗ,cRÐþɼ©ṢƈḅṬḊṬɠṅÞ»Ỵ¤

在线尝试!

单字链接,以YYYYMMDD格式的日期作为参数,并返回带有答案的字符串。

测试用例(来自@KevinCruijssen的05AB1E答案)。


4

JavaScript(Node.js) 855851803 字节

d=>(g=i=>(p+=parseInt((a=(require('zlib').inflateRawSync(Buffer("ZVJtct0gDLwKB1A9dSaTtGfoKWSQDc+AXgS2S09f8T6cdPqDsc2utKuVv411jTC6FV4EzzO6eD53GG26vc0Wtp+3t9uhjkwn7+0dxnf6hPu5JHiJP8524mHcr7fPV3yDF+Z79XcL48em2ASy/yP+1dRCLAuZA4sPeamc4cI+G3SYClTPCYu50DyTlI5homISuvDli7Mw3cs+tpBte1RjdkKHuaBdOzuh1JDNjtlMm1CGI8QYMBlPWZrxKHLv2hvVFkkeAutgrhxX+INWSc1UbJEFkpajODPrM7EQzIJ5jSpxDSSWHtXTplUZM+Ak6FVOGZZjPu2pXJfdYitF+WUwizaqIFv1JDOrxDSovUbl0RKVgjIHig6sp1JJ+p3O5zeBRXjXCxtpp6giMFHWOvV1jvgf5RlFsr9CjtQ0eWKnQxlhLp1VP+Pio49dcdY7Zid8GMV6XwU0WLN0u+J0n2Ax7qpsmWNwC4FONJFUbdIdfEbmhi9K3WfrQVTZkibnjrD42jkUCmU10HfT1zQPZqWcyTWILTvOPapnohL6vpzJ4XefmQSjbkvzhEtIqRmrgWkj4dwBIVwwP/9HP5hj0N0VD5POrVGFXPmETwxF/y7DEyYEd2/UTV//Ag","base64"))+"").split`,`)[i],32))<new Date(d)/864e5?g(i+1):a[i+44].replace(/\b./g,c=>c.toUpperCase()))(p=0)

在线尝试!

怎么样?

压缩的字符串包含89个值的列表。

前44个值是总统授权的长度,以天数表示,并以32为基数(该基数压缩得最好)。

接下来的45个值是小写的总统姓名。随后应用标题大小写(从@KevinCruijssen的答案借来的技巧)。


由于标题的原因,麦金莱输出为“麦金莱”。
TheJim01

@ TheJim01感谢您的举报。现在已修复。
Arnauld

3

SOGL,475 字节

⁵æ“DƧ▓«ļ¶↕Q∫<⅔∑+fQyΛ‼Bī:׀═&Sf',№═׀ΔΥκ\κΗδ=6oΡΠω«i℮ī K }½6YΨδ~░pTø№ŗ◄&Λθ▒“Æš“─{1α:C'⁰%+:"⅞“*c'⁰÷+dr"⅞“*b'«*e++>?al"t!■u↔R⅝Θνj╤←⁵κP׀λc⅜-Gc⁸׀!┐)└█5¦@T¬[@∫↕‛ρ℮↑δŗιf√³I2~Δ▓η¤⁴b¡νθ@36⅛Π└Ξ±Ƨ⅜⅓mwθ:∫^⌠≥-N\T‛V;r□τθ⁹;C{¾lΠƨUΒΡ║⁸Κ≤øΔ°?U┼S→iļ┐Kπzz∑Θ⅛%τ`ωΗ2D≡ψ_;OR⁴w≡1Z⅓ēυ%─ž′ιVJοUζķπd└≈³ΘΟ‚GR2┌═NΒθ↔T²νsσ□lQMæν÷¦Π±°≡═øΡTe¼ž⁰λ_►○³1≡№╚█λΜβΤ½<Π±Cļ¬└⁴┘čgr⁰⁄▲8|}⁸ē4№╤#√⅔d?÷≥}▼eī┼^ƧX‽>≥▼k ¹Wļm║⌠¦→┘C׀ψΠ¼⁽tΠ↔:φbεļ▓Β√⁷->→⅜O∑░=`ξε↕ņ⅛╚πBπσ§T׀ķīT:‽4;%≥═pP¶G→○▼φβjτ9v⁄6Υ◄⁹¤{τ/∑a⁰}┐⁵e¬č‘ūƧkiƧKiŗƧ, Θw←

在这里尝试!

日期逻辑可能会有所改善。


3

PHP,888字节

$_=0;$l=explode('|',gzinflate(substr($a=base64_decode('AAALMQW0C2oLagtqBbULagW1AB8FlgW1AewDyQW1BbUF3wWLC2oFtQDHBO4FtQW1BbUGdgqoBbULagNxB/kFtRFGCxgLagQMB14H6wN/BbULagW1C2oLagtqZVLtjtpADHyVfYK8Ax8FBEd7pWmR+s9kTXbLxq68CXSlffjaheNO6q8o6/HMeOw1svTojpBDpH5kqlsO5GYehlzbwANkt8XzGSVbDQbMbg8+fvhjEsZ729cpUlce3TPygje3he5i6D3IGMn9AHLzSZDqMaYUYXAbJCluAyJ3ViNqS0J5COwa98rpUn9CF0CRLZTEUvfaDuLdSr8DC9aVAF2SSrxGlA4f3fNJuwiozk4CQeVe1CInetpTOZP9nkrOiv/WuLUSjfUwjQHlzCoxb9RewfygnCkE5Bwx+boImEcUe5vJGCapa+GrPiwSXjEB+TpH+gWD+nqO+B/kLYp9t4uUsGjyyF6HcgfmbKjxPS6+2dgtnPWN2QvfnNaM96gCSG5tdsXrPusC0lWVF8wp+h7rBuWEMiqJOXiPbNl8UDKfxYJoZRo0ueUt9mE0zKeYkQLfbDe2plXjdkiEvtSXQp7JonpL9BBtX959jn9sZhRIui3Ns27jMBS30INQogOTFQ4IPRjs3z1uGndsdHc51LnOrVFFGvlZftZA9LrclxMMUJd3IjP9+y8='),90)));foreach(str_split(substr($a,0,90),2)as$i=>$d)(strtotime($argn)+5701449600)/86400<($_+=hexdec(bin2hex($d)))&&@die($l[$i-1]);echo end($l);

尝试演示


3

05AB1E464个 444 442 418字节

通过窃取Kevin Cruijssen的答案中的想法,获得大约 -15个字节

越过444仍是444):

.•J@λ
A∞‚XÃUδ„«›Æx…ΔΣ¸ÃEÛö›³;Î!¦ǝzŽ`²œ∊ïøÞ‰Cf4₆и)Kuм¹5®‹м£Ž}мNPõüïí
^ā-Ǩåˆ[Ü_5δ³ÂBмzIΣŠÒÅÆè"²0¢₆'$Ćùù»õJ´õý¢ºн³õ?ëØ؆Dǝç.r˜èÌ•ć¡`“ŒÕ‡¹„б·Ž²¿É‰Þ³×‰Þ̯„Ðëìncy±·¢Œžˆ–ÄîèÿíÈ£ÎÈœ„ÐÕÀ‰Þ kz™éÿ¥•¹êardœãÿ²ÀâÓ‰Þÿ㳫ƢŒš•ÿ—§Î¶ÿ橉ޖÖÿל–Ö·Š¶‹r¯ŽÇÕÈœ¶‹r¯ŽíÈÿíÈ©Æ»äft–œrow¦‹¼‰ gz†èingãŸîÁÿ²Àÿ„Ð fz½¸ÿš••Öÿéï™»»É»‰ÖŠÿŒÕÕ¹ wzŒ›‰ŽµÍŒÕ wzŒ›‹²ÿ¹Ñÿ“™„qz„K.‡#•¥ü„¨%¨₃Ó8•3в4α£ðý•9ÕS&µç©•3в±žD*•¾¹iìzÓ.+,âšÜĀ‘ó₆x.»δ•žHв«9._.¥•U<¶•+I32β‹Oè

在线尝试!

第1部分:名称

05AB1E中有两种类型的字符串压缩:

  • Base-27字符串将每个字母编码为〜0.6字节(技术上:log(27)/ log(255)),但只能包含[a-z ](小写字母和空格)。
  • 字典字符串以2个字节编码常见的英语单词(加上前面的空格),每个ASCII字符都以1个字节编码(它们只是未经修改地传递)。他们还可以使用byte ÿ来从堆栈中弹出一个值并将其嵌入到字符串中。

我们将使用两者的最佳组合。因为从一种编码转换为另一种编码会产生一些开销,所以这比为每个单独的名称选择最短的编码更为复杂。例如:

  • ingHarding Calvin大约需要1.8个字节在碱-27 VS在词典字符串3个字节。但是HardCalvin都在字典中,因此我们最终将ing未压缩的内容留在字典字符串中。
  • HerbCoolidge Herbert Hoover大约需要3个字节在碱-27 VS 2只在词典中的字符串(因为herb是在词典)。但是字典中都Coolidge没有Hoover,所以最终Herb以base-27 编码。

为了绕开base-27字符串只能包含的限制[a-z ],我们为某些字母分配了特殊含义。他们之间的44位总统用尽了全部26封信,但我们仍然可以耍花招:

  • 唯一的js在字典单词(James,,Benjamin...)中,因此j对于base-27字符串是免费的。
  • 唯一q在中Quincy,因此小写字母q是免费的
  • 唯一z在中Zachary,因此小写字母z是免费的
  • 唯一的xNixon,因此大写字母X是免费的(我们实际上并未使用它)

话虽如此,让我们看一下代码。

.•J@λ
A∞‚XÃUδ„«›Æx…ΔΣ¸ÃEÛö›³;Î!¦ǝzŽ`²œ∊ïøÞ‰Cf4₆и)Kuм¹5®‹м£Ž}мNPõüïí
^ā-Ǩåˆ[Ü_5δ³ÂBмzIΣŠÒÅÆè"²0¢₆'$Ćùù»õJ´õý¢ºн³õ?ëØ؆Dǝç.r˜èÌ•

# (The newlines are significant.) This is the base-27 string:
# "j trumpjack obamaj reaganj nixonj lyndon bzj dz roosevelt harry sz truman dwight dz eisenhowerjidge herbert hooverj mcqinley theodore rooseveltj garfieldjerford bzj ulysses szj buchananjmorejlk zacharyj buren"

ć    # extract the first character
 ¡   # split the rest on that character (a "j")
  `  # dump the list on the stack
# We now have " buren" at the top of the stack, and " trump" at the bottom.

“ŒÕ‡¹„б·Ž²¿É‰Þ³×‰Þ̯„Ðëìncy±·¢Œžˆ–ÄîèÿíÈ£ÎÈœ„ÐÕÀ‰Þ kz™éÿ¥•¹êardœãÿ²ÀâÓ‰Þÿ㳫ƢŒš•ÿ—§Î¶ÿ橉ޖÖÿל–Ö·Š¶‹r¯ŽÇÕÈœ¶‹r¯ŽíÈÿíÈ©Æ»äft–œrow¦‹¼‰ gz†èingãŸîÁÿ²Àÿ„Ð fz½¸ÿš••Öÿéï™»»É»‰ÖŠÿŒÕÕ¹ wzŒ›‰ŽµÍŒÕ wzŒ›‹²ÿ¹Ñÿ“
# Dictionary string. All the values we just dumped on the stack are popped by `ÿ`s in this string. We end up with:
# "george washington john adams thomas jefferson james madison james monroe john quincy adams andrew jackson martin van buren william henry harrison john tyler james kz polk zachary taylor millard fillmore franklin pierce james buchanan abraham lincoln andrew johnson ulysses sz grant rutherford bz hayes james az garfield chester az arthur grover cleveland benjamin harrison grover cleveland william mcqinley theodore roosevelt william howard taft woodrow wilson warren gz harding calvin coolidge herbert hoover franklin dz roosevelt harry sz truman dwight dz eisenhower john fz kennedy lyndon bz johnson richard nixon gerald ford jimmy carter ronald reagan george hz wz bush bill clinton george wz bush barack obama donald trump"

™         # title case
 „qz      # literal "qz"
    „K.   # literal "K."
       ‡  # transliterate (replaces all "q" with "K" and all "z" with "."

# This takes care of two issues at once: title casing would leave McKinley with a lowercase k, and base-27 strings can't contain "."
# We now have a long string, with all the presidents' names in order. We need to split it into 45 strings (one per president).
# We could've used a special marker to delimit the presidents, but it turns out it's more efficient to count the words in each name:

#                  # split on spaces (yes i know i've been using # for comments, but there's no actual comment character in 05AB1E)
 •¥ü„¨%¨₃Ó8•3в     # compressed list 222221211212222221111222221212211111222202122
              4α   # vectorized absolute difference with 4: 222223233232222223333222223232233333222242322
                   # this is the list of the number of words in the presidents' names
                   # (note that "George H. W. Bush" is the only 4-word name, forcing us to use base-3 instead of base-2 for the compressed list, which costs 3 bytes)

£                  # split the list of words according to the list of wordcounts: [["George", "Washington"], ..., ["Donald", "Trump"]]
 ðý                # join each sub-list with spaces

现在,我们按时间顺序列出了45个全名,因此我们已经完成了这一部分。

第2部分:日期

为了更轻松地比较日期,我们将它们转换为整数。这是通过以32为基数的转换完成的,该转换为1024 *年+ 32 *月+天(并且不关心年份的“数字”是否大于32)。

我们以时间顺序相反的顺序开始,列出连续的连任数(不改变总统的选举):

•9ÕS&µç©•3в   # compressed list 111010111011020000011000000101110

前三个1对应于奥巴马,布什和克林顿连任一次。孤单2对应于麦金莱和西奥多·罗斯福都当选一次(麦金莱去世,西奥多·罗斯福是他的副总统,因此在他们的连任之间没有其他选举)。富兰克林·罗斯福(Franklin D. Roosevelt)+杜鲁门(Truman)那里应该有一个4,但这也是就职日从3月4日更改为1月20日的地方,因此我们稍后处理此特殊情况。

±             # bitwise not (equivalently: add 1, then negate): [-2, -2, ..., -1]
 žD*          # multiply by 4096 [-8192, -8192, ..., -4096]

请记住,1024表示一年,所以4096表示完整授权。现在,这是新当选的总统之间的时间差异列表(仍然按相反的时间顺序排列,这就是为什么数字需要为负)。

现在,让我们照顾没有履行全部职责的9位总统(4位因自然原因死亡,4位暗杀,尼克松辞职):

•¾¹iìzÓ.+,âšÜĀ‘ó₆x.»δ•žHв   # compressed list [45088, 9317, 15270, 16548, 20475, 22484, 22410, 18666, 11155, 43819]

45088是从1797年3月4日(在3月4日首次引入)到1841年4月4日(在办公室中首次死亡)的时间增量。以下数字是9个死亡/辞职之间的时间差。最后,43819是从1974年8月9日(尼克松辞职)到2017年1月20日(1月20日的最新介绍)的时间增量。

«             # merge the two lists of time deltas
 9._          # rotate left by 9 (the number of January 20 introductions)
    .¥        # undelta
      •U<¶•   # compressed integer 1979491, meaning March 4, 1933 (the last March 4 introduction)
           +  # add to each

现在,我们有了总统更换所有日期的清单。该命令有点时髦:它始于1933年,然后在3月4日前及时向后介绍,然后死亡/辞职及时进行,然后沿1月20日前来介绍。但是,顺序实际上并不重要。

I             # get the input date
 32β          # convert from base 32, yielding an integer
    ‹         # vectorized compare with our list, yielding a list of 0/1 booleans
     O        # sum
      è       # use the sum to index into the list of presidents' names
              # implicit output

我们完成了。


1
哦,您现在甚至打败了果冻!非常好。期待看到一个解释。
凯文·克鲁伊森

@KevinCruijssen我添加了一个解释(:
Grimmy

2

木炭,550字节

✂⊟Φ⪪”}¶⟲φε"1It↙θ|&s1%⎚÷X2J¦T↶M↙@P≡,7gSC{WTB¿μ;↗AEoU…⁹aⅉ:(↶Þ|‹Cρr]χY↨×⪪…✳μⅉⅈ²]⌈&ρτ“↖s∕↘yⅉ⊘K%QUi&Uρ⦃→↑^_tZ}↙s⪪3#\UZ¶L↥ρ÷Qa.ρE⊞B↘¿⦃{RC⁴βLΦï←⁹″*²⌊∕~L‽∧&;⁵⪪⪪◨sFψ⊗=₂εωb5⎚∧¬Ii»S⟲H⧴⮌⁵↷≕≦-Lκ↗⦃″✂DcgSOE}PΣ℅ξ»u➙ïA⁼×◧⁴Gθkιbσ6�V→↓B⮌⦃]σ0↶L↷'Þ¡σ?αδ⪫4⁶M"‽W◨-n℅O≡o ⊙^fκ{~✳N⦄y⊟≦Kθ!«↘pB;fJ<ιU%D‴▶p|‹⌕ιHpIt‽7}u1~φνL▶⮌;NY✂S>ÞSλ<«⦃¬k⦄J⦃◧‴}v≕﹪φEβ<⁴↘la»�λ⁼.Xξ.th⊕jEχ>m4J'⁺▷αZ↷|D$⸿Sz⪪←fFωρ↓oⅉ⊙⁰vFQ0T⪫?GE▷|℅±P;χ'|x>i↨E◧⁺4M↖‴№⟦Π~‹σ_▷⁵N?6ⅉ |x+\⁺“≡C⟦⁹⁸pC⊕Dïψ?◧⎇ςΠH&]▷≧β↓⧴J<(1←¦6‽GG✂¡58Go…Qψ/§§c↷ZQ\@ςr⁹}TZ?«fPO)⧴\`↘℅⦄&yθ%⪫iλ*fÀ-ⅈ↖→◨⬤F″‖↘→⁹S#″▶Φ∨¬w◧«℅ySq§mCIπF?sⅈP⍘↷≔f‽k↥ABMX﹪”¶‹ιθχ

在线尝试!链接是详细版本的代码。接受ISO格式的输入。说明:

    ”...”       List of decremented dates and presidents
   ⪪     ¶      Split on newlines
  Φ       ‹ιθ   Keep those that precede the input
 ⊟              Take the last
✂            χ  Remove the date

2

STAX550个 509 502 字节

ª╠TkªÑTs¬¢‼É▼╠→+╕s☼∩b°┴ò0J Öª◙%µ♪)Çb○Σ²╦àôä√P┼[╫»↓♪─y·:+x/ p~┤☺kèT┤╩☺♀◄╗*DX○e╩+ë%╫↔^║;◙/Θ╛B║K>▌GΣR╥♠╞N┬6dt♥h♪T⌠╥⌠ó←`±ΘÆM:\V►╢◘≥pe½╖→Ea}╚≤╠ΓÜ╗,πUF[╢▄¥¥▒t≥òK"=☺S╟▐ü%û▼98╕→→ñ`eB▌y↨\%Ç│╝û°6R┘K√â°◘RfçMñ♪3╦┤â╞╥┼«╧j/εⁿ○ó▀CäI3┘c▄}═%[C_%jgÿÉJÑS═g(┴`╦%êk9½é╔PT¶◄<║╕à¼╬≤`dεµc╨╔?Z=╧╥RDΓl.┘uFⁿ∟Juö♥,╠╛z¡ÇM7m☺╩?ïPª$πä♂ ╝Jò'æφ▬♠w►○ôr├=é┴╪ù#?∩E↓╣J∩╒░▒○LÑ∞¥╗å▐Ü└ä∙↨l♀╠├d┴Uÿ╩(╔♦)═╦¢αe£/bΦ╜%d$Öä¢M║╖q]¢╝8╨ÖBÿ┼÷{@0╕/#└â╪~V§πp<≡k♫á∩g4üÉà↔DR[√á√ü.îºV'X♥9↔øiè²≈τoæ┴0☻ó╪!‼⌡¼'↔ δ'X⌂■OpX¬E╡░├a"√4lB<└♣A╪º╕4íbz♠ìÿ°Γ═àÅ└5äº5æ2§ΩWª

运行和调试它还具有Kevin Cruijssen的测试功能

该程序可以正确执行,但是如果您使用“解压缩文字”工具,它将破坏源代码。最后将两个整数文字彼此相邻放置,这使一个更大的文字成为可能。当然,这与原始程序的功能不同。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.