代码高尔夫:钻石二十一点
挑战
按字符数计算的最短代码,从给定的数字牌列表中输出最佳情况的二十一点手牌。
输入是由空格分隔的从 1 到 10(含)的数字列表。
输出将是从该卡牌列表中形成的最佳二十一点手牌 - 最接近的可用组合,通过所有卡牌值的总和达到 21 点而不超过它。卡可以删除但不能添加。
如果需要移除两张或更多牌以支持一张牌来实现相同的结果(移除 5 或 4,1 以获得 21),则需要移除最少的牌。 如果要移除相同数量的牌(移除 1,4 或 3,2),则具有最小值的组将被移除(在前面的示例中,1,4 将被移除为 min(min (3,2), min(1,4))
属于该对)。如果出现重复的卡牌,则应删除第一次遇到的卡牌。
输出将形成为钻石卡,并保留输入顺序:
__________ __________ | || | | || /\ | | || \/ | | /\ || | | \/ || | | || /\ | | || \/ | |__________||__________| __________ __________ | || | | /\ || /\ /\ | | \/ || \/ \/ | | /\ || | | \/ || | | /\ || /\ /\ | | \/ || \/ \/ | |__________||__________| __________ __________ | || | | /\ /\ || /\ /\ | | \/ \/ || \/ \/ | | /\ || /\ /\ | | \/ || \/ \/ | | /\ /\ || /\ /\ | | \/ \/ || \/ \/ | |__________||__________| __________ __________ | || | | /\ /\ || /\ /\ | | \/ /\ \/ || \/ /\ \/ | | /\ \/ /\ || /\ \/ /\ | | \/ \/ || \/ /\ \/ | | /\ /\ || /\ \/ /\ | | \/ \/ || \/ \/ | |__________||__________| __________ __________ | /\ /\ || /\ /\ | | \/ \/ || \/ /\ \/ | | /\ /\ || /\ \/ /\ | | \/ /\ \/ || \/ \/ | | /\ \/ /\ || /\ /\ | | \/ \/ || \/ /\ \/ | | /\ /\ || /\ \/ /\ | |_\/____\/_||_\/____\/_|
测试用例
Input:
1 5 7 8
Output:
__________ __________ __________ __________
| || || || |
| || /\ /\ || /\ /\ || /\ /\ |
| || \/ \/ || \/ /\ \/ || \/ /\ \/ |
| /\ || /\ || /\ \/ /\ || /\ \/ /\ |
| \/ || \/ || \/ \/ || \/ /\ \/ |
| || /\ /\ || /\ /\ || /\ \/ /\ |
| || \/ \/ || \/ \/ || \/ \/ |
|__________||__________||__________||__________|
Input:
10 3 4 2 6
Output:
__________ __________ __________ __________
| /\ /\ || || || |
| \/ /\ \/ || /\ || /\ || /\ /\ |
| /\ \/ /\ || \/ || \/ || \/ \/ |
| \/ \/ || /\ || || /\ /\ |
| /\ /\ || \/ || || \/ \/ |
| \/ /\ \/ || /\ || /\ || /\ /\ |
| /\ \/ /\ || \/ || \/ || \/ \/ |
|_\/____\/_||__________||__________||__________|
Input
5 10 5 2 3
Output:
__________ __________ __________ __________
| /\ /\ || || || |
| \/ /\ \/ || /\ /\ || /\ || /\ |
| /\ \/ /\ || \/ \/ || \/ || \/ |
| \/ \/ || /\ || || /\ |
| /\ /\ || \/ || || \/ |
| \/ /\ \/ || /\ /\ || /\ || /\ |
| /\ \/ /\ || \/ \/ || \/ || \/ |
|_\/____\/_||__________||__________||__________|
代码计数包括输入/输出(即完整程序)。
The challenge
The shortest code by character count to output a best-case blackjack hand from the list of number-cards given.
Input is a list of numbers from 1 to 10 (inclusive) separated by space.
Output will be the best blackjack hand formed from that list of cards - the closest available combo to reach 21 by the sum of all card values without surpassing it. Cards can be removed but not added.
If a case where removing two or more cards in favor of one card to accomplish the same result (removing 5 or 4,1 to gain 21) removing the least cards is required.
If the equal amount of cards is to be removed (removing 1,4 or 3,2) the group with the minimum of the minimum will be removed (in the previous example, 1,4 will be removed as min(min(3,2), min(1,4))
belongs to that pair). In the case of duplicate cards, the first encounter should be removed.
Output will be formed as diamond cards, with input order preserved:
__________ __________ | || | | || /\ | | || \/ | | /\ || | | \/ || | | || /\ | | || \/ | |__________||__________| __________ __________ | || | | /\ || /\ /\ | | \/ || \/ \/ | | /\ || | | \/ || | | /\ || /\ /\ | | \/ || \/ \/ | |__________||__________| __________ __________ | || | | /\ /\ || /\ /\ | | \/ \/ || \/ \/ | | /\ || /\ /\ | | \/ || \/ \/ | | /\ /\ || /\ /\ | | \/ \/ || \/ \/ | |__________||__________| __________ __________ | || | | /\ /\ || /\ /\ | | \/ /\ \/ || \/ /\ \/ | | /\ \/ /\ || /\ \/ /\ | | \/ \/ || \/ /\ \/ | | /\ /\ || /\ \/ /\ | | \/ \/ || \/ \/ | |__________||__________| __________ __________ | /\ /\ || /\ /\ | | \/ \/ || \/ /\ \/ | | /\ /\ || /\ \/ /\ | | \/ /\ \/ || \/ \/ | | /\ \/ /\ || /\ /\ | | \/ \/ || \/ /\ \/ | | /\ /\ || /\ \/ /\ | |_\/____\/_||_\/____\/_|
Test cases
Input:
1 5 7 8
Output:
__________ __________ __________ __________
| || || || |
| || /\ /\ || /\ /\ || /\ /\ |
| || \/ \/ || \/ /\ \/ || \/ /\ \/ |
| /\ || /\ || /\ \/ /\ || /\ \/ /\ |
| \/ || \/ || \/ \/ || \/ /\ \/ |
| || /\ /\ || /\ /\ || /\ \/ /\ |
| || \/ \/ || \/ \/ || \/ \/ |
|__________||__________||__________||__________|
Input:
10 3 4 2 6
Output:
__________ __________ __________ __________
| /\ /\ || || || |
| \/ /\ \/ || /\ || /\ || /\ /\ |
| /\ \/ /\ || \/ || \/ || \/ \/ |
| \/ \/ || /\ || || /\ /\ |
| /\ /\ || \/ || || \/ \/ |
| \/ /\ \/ || /\ || /\ || /\ /\ |
| /\ \/ /\ || \/ || \/ || \/ \/ |
|_\/____\/_||__________||__________||__________|
Input
5 10 5 2 3
Output:
__________ __________ __________ __________
| /\ /\ || || || |
| \/ /\ \/ || /\ /\ || /\ || /\ |
| /\ \/ /\ || \/ \/ || \/ || \/ |
| \/ \/ || /\ || || /\ |
| /\ /\ || \/ || || \/ |
| \/ /\ \/ || /\ /\ || /\ || /\ |
| /\ \/ /\ || \/ \/ || \/ || \/ |
|_\/____\/_||__________||__________||__________|
Code count includes input/output (i.e full program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
J, 303 个字符
核心,找到最好的手牌,其实很容易。生成给定卡片的幂集,并选择最高值小于 22 的卡片。
然后我们从一张空白卡片开始
,在适当的位置铺上菱形。
该计划的其余部分主要是钻石的去向表。
J, 303 characters
The core, finding the best hand, is actually really easy. Generate the power set of the cards given, and select the one with the highest value less than 22.
Then we start with a blank card
and layer on diamonds in the appropriate places.
The rest of the program is dominated by the table of what diamonds go where.
Golfscript - 228 个字符
231 个字符
工作原理
Golfscript - 228 Characters
231 Characters
how it works
Python - 365 个字符
369 个字符
377 个字符
408 个字符
Python - 365 Characters
369 Characters
377 Characters
408 characters
Perl
公然窃取gnibbler的Python解决方案;如果在命令行上给出输入,则结果会短三个字符,为 362。
要从标准输入读取,请将@ARGV 替换为@F 并使用perl -an ;使用传统的 Perl 高尔夫计分,计为 362。
当然,gnibbler 的其他 Python 技巧 可以复制也。
Perl
Blatantly stolen from gnibbler's Python solution; turns out to be three characters shorter, at 362 -- if input is given on the command line.
To read from stdin, replace
@ARGV
by@F
and useperl -an
; using traditional Perl golf scoring, that counts as 362.And, of course, gnibbler's other other Python trick can be copied too.
Lua,895
好的,现在改正。甚至可以得到 1 分作为 A。
示例输出:
Lua, 895
OK, correct now. Can even score 1 as ace.
Sample output:
Python - 298 个字符
只需像这样重命名脚本
这是脚本
和示例运行
Python - 298 Characters
simply rename the script like this
Here is the script
and a sample run
Python,632 个字符
示例输出:
Python, 632 characters
Sample output:
Haskell 最低限度测试了 721 个字符
更新了 Haskell 版本,现在据说适用于所有情况,但可能需要更多测试。
Haskell 版本尚未完成 622 个字符
它不会赢得任何选美比赛,我确信还可以改进,并且不完全遵循规范(仍在研究
min( min(3,2), min(1,4))
问题以及重复过滤(o
函数需要更多情况))。尽管它不完整,但它确实通过了给出的所有测试用例。每行都带有注释 (
--
) 以及其中的字符数,包括新行。Haskell minimally tested 721 characters
Updated Haskell version, now supposedly works for all cases but it could probably do with more testing.
Haskell version not finished yet 622 characters
it won't win any beauty contests, could also be improved I'm sure, and doesn't exactly follow the spec (still working on the
min(min(3,2), min(1,4))
issue as well as the duplicate filtering (theo
function needs more cases)). Despite it's incompleteness it does pass all the test cases given.Each line is commented (
--
) with the number of characters on it, including the new line.Perl,410 char
不知道还有多少改进可能,但也许这比 gnibbler 的算法更容易移植到 GolfScript:
Perl, 410 char
Don't know how much more improvement is possible, but maybe this would be easier to port to GolfScript than gnibbler's algorithm: