创建一个程序,计算两个名字共有的字母总数,并找到它们长度的乘积,作为一个“爱的测试者”。
条件:你可能得不到1:1的答案(3比3,等等)输出。
两个名字来自STDIN或最近的替代。
将x计算为两个名称之间共有的字母总数,忽略大小写。将y计算为名称长度的乘积。然后输出,到STDOUT或最近的替代,是
Name1 and Name2 have x out of y chances of love.输入:
Wesley
Polly输出:
Wesley and Polly have 2 out of 30 chances of love.卫斯理和波莉有两个共同的字母,y和l,其长度的乘积为6*5= 30。
输入:
Bill
Jill输出:
Bill and Jill have 3 out of 16 chances of love.x out of y是完全缩减的形式。这是密码-高尔夫答案将以字节为单位得分,而更少的字节更好。
发布于 2015-10-13 02:55:13
jd[z"and"Jw"have"/K-lzl.-rz0rJ0i=J*lzlJK"out of"/JiJK"chances of love.该代码有70字节长,并有资格获得-30字节加值。
[ Begin an array and fill it with the following:
z The first line of input.
"and" That string.
Jw The second line of input, saved in J.
"have" That string.
rz0rJ0 Convert both lines to lowercase.
.- Remove the characters form the second string
from the first, counting multiplicities.
l Get the length.
-lz Subtract it from the length of the first line.
K Save in K.
=J*lzlJ Save the lines' lengths' product in J.
i K Compute the GCD of J and K.
/ The quotient of K and the GCD.
"out of" That string.
/JiJK The quotient of J and the GCD of J and K.
"chances of love. That string.
jd Join the elements, separated by spaces.发布于 2015-10-13 03:32:32
ll]_~@_{'~,\elfe=}/.e<:+\:,:*]_2>~{_@\%}h;f/"and
have
out of
chances of love."N/.{}S*代码长85字节,并有资格获得-30字节加值。
在CJam解释器网上试一试。
ll] e# Read two lines and wrap them in an array.
_~ e# Copy the array and dump the lines on the stack.
@_ e# Rotate the array on top and push a copy.
{ e# For each line of the copy.
'~, e# Push the array of all ASCII charcters up to '}'.
\el e# Convert the line to lowercase.
fe= e# Count the occurrences of each character in the line.
}/ e#
.e< e# Vectorized minimum of the occurrences.
:+ e# Add to find the number of shared charaters.
\:, e# Compute the length of each line.
:* e# Push the product.
]_ e# Wrap the stack in an array and push a copy.
2>~ e# Discard the lines of the copy and dump the calculated integers.
{_@\%h}; e# Compute their GCD, using the Euclidean algorithm.
f/ e# Divide each element of the array by the GCD.
e# This also splits the names, which won't affect printing.
"and
have
out of
chances of love."
N/ e# Split the above string at linefeeds.
.{} e# Vectorized no-op. Interleaves the arrays.
S* e# Join the results elements, separated by spaces.https://codegolf.stackexchange.com/questions/60459
复制相似问题