首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >爱测试码高尔夫

爱测试码高尔夫
EN

Code Golf用户
提问于 2015-10-12 04:59:35
回答 6查看 774关注 0票数 9

创建一个程序,计算两个名字共有的字母总数,并找到它们长度的乘积,作为一个“爱的测试者”。

条件:你可能得不到1:1的答案(3比3,等等)输出。

输入

两个名字来自STDIN或最近的替代。

输出

x计算为两个名称之间共有的字母总数,忽略大小写。将y计算为名称长度的乘积。然后输出,到STDOUT或最近的替代,是

代码语言:javascript
复制
Name1 and Name2 have x out of y chances of love.

示例

输入:

代码语言:javascript
复制
Wesley
Polly

输出:

代码语言:javascript
复制
Wesley and Polly have 2 out of 30 chances of love.

卫斯理和波莉有两个共同的字母,yl,其长度的乘积为6*5= 30。

输入:

代码语言:javascript
复制
Bill
Jill

输出:

代码语言:javascript
复制
Bill and Jill have 3 out of 16 chances of love.

奖金

  • 使用简化的分数减去30个字节,即x out of y是完全缩减的形式。

评分

这是密码-高尔夫答案将以字节为单位得分,而更少的字节更好。

EN

回答 6

Code Golf用户

回答已采纳

发布于 2015-10-13 02:55:13

Pyth,40字节

代码语言:javascript
复制
jd[z"and"Jw"have"/K-lzl.-rz0rJ0i=J*lzlJK"out of"/JiJK"chances of love.

该代码有70字节长,并有资格获得-30字节加值。

在网上试试。

代码语言:javascript
复制
  [                        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.
票数 3
EN

Code Golf用户

发布于 2015-10-13 03:32:32

CJam,55字节

代码语言:javascript
复制
ll]_~@_{'~,\elfe=}/.e<:+\:,:*]_2>~{_@\%}h;f/"and
have
out of
chances of love."N/.{}S*

代码长85字节,并有资格获得-30字节加值。

CJam解释器网上试一试。

是如何工作的

代码语言:javascript
复制
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.
票数 3
EN

Code Golf用户

发布于 2020-12-27 05:10:02

斯塔克斯,42-30= 12 字节数

代码语言:javascript
复制
±à>mª≈◄ΣÇyⁿ`à>²CΔ7ù«▀╦X╠;7╣ⁿ¡♣ë╩■B§F»é▲Y∩«

运行并调试它

分数是很棒的。

票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/60459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档