爱情测试员代码高尔夫


9

创建一个程序,该程序计算两个名称公用的字母总数,并查找其长度的乘积,以充当“爱情测试仪”。

条件:您可能不会得到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.

Wesley和Polly有两个共同的字母,yl,其长度的乘积为6 * 5 = 30。

输入:

Bill
Jill

输出:

Bill and Jill have 3 out of 16 chances of love.

奖金

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

排行榜:

排名将由语言决定。高尔夫标准赛将于10月17日太平洋夏令时间(加利福尼亚州)晚上10:20结束

代表奖

  • 您将获得前10名(最高票名除外)的10名代表。
  • 您将获得15名(第一名)。
  • 您可能还会从另一个人那里获得赏金奖励。

这是一个堆栈代码段,可按语言生成常规排行榜和获胜者概述。**

为确保您的答案显示出来,请使用以下Markdown模板以标题开头。

# Language Name, N bytes

N您提交的文件大小在哪里。如果您提高了分数,则可以通过打败旧分数保持标题。例如:

# Ruby, <s>104</s> <s>101</s> 96 bytes

** Kslkgh的Sign That Word中的堆栈片段


祝贺您:

  1. 冠军丹尼斯(Pyth)
  2. 丹尼斯(CJam)
  3. NBZ(APL)
  4. Mouldmanful(JavaScript ES6)
  5. 亚历克斯·A(朱莉娅)

3
输出应用于Aaron\nAhmad什么?还是samename\nsamename
lirtosiast,2015年

3
我不清楚当字母重复时,您如何计算共有的字母总数。如果名称中有a和且b有字母,那么这些是否算min(a,b)重复?
xnor

正如@xor所说,您如何计算重复的字母?在第二个示例中,您似乎在计算重复的字符,因此,如果第一个示例颠倒了,那么您期望得到不同的结果吗?
Rnet 2015年

它与kolmogorov复杂度完全无关吗?

Answers:


1

Pyth,40个字节

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.

3

Dyalog APL94 91-30 = 61字节

通常,APL打高尔夫球所产生的代码比普通代码更紧凑(但不更复杂),但是在这种情况下,我以丑陋的方式保存字符:

{1↓(∊' ',¨⍵,⍪'and' 'have'),' love.',⍨∊((⊢÷∨/)(≢⊃∩/32|⎕ucs¨⍵),×/≢¨⍵),⍪'out of' 'chances of'}

,⍪'out of' 'chances of'创建2×2的数字(左)和文本(右)
×/≢¨⍵长度乘积表,以
32|⎕UCS¨⍵协调大小写UCS值
≢⊃∩/相符,两组的交集
⊢÷∨/将相符和乘积及其GCD
,' love.',⍨∊分成一个简单列表并附加爱。
⍵,⍪'and' 'have'创建一个2×2的名称(左)和文本(右)
∊' ',¨表,在每个表单元格前面加一个空格,然后放入简单列表,
1↓删除初始多余的空间

感谢-3个字节的ngn。


看来这只是一个函数,而OP指定了来自STDIN的输入并输出到STDOUT(即完整的程序,而不仅仅是一个函数)。
Alex A.

@AlexA。APL没有STDIN,但可以提示您接受格式为的两个名称'Wesley' 'Polly'。如果您认为这样比较公平,请随时在行的末尾()之后加上(U + 2395),然后}将比分调整为65。
Adám2015年

2

Javascript ES6,123个字节

(a,b)=>a+` and ${b} have ${[...a].map(x=>eval(`/${x}/i.test(b)&&c++`),c=0),c} out of ${a.length*b.length} chances of love.`

这么多的“爱” ...我真的可以用更少的字节。

在Firefox中运行代码段。


2
看来这只是一个函数,而OP指定了来自STDIN的输入并输出到STDOUT(即完整的程序,而不仅仅是一个函数)。
Alex A.

2

朱莉娅,129个字节

该代码为159个字节,但有资格获得-30分的奖励。

A,B=map(chomp,readlines())
a,b=map(lowercase,[A,B])
L=length
r=Rational(L(ab),L(a)L(b))
print("$A and $B have $(r.num) out of $(r.den) chances of love.")

可以通过花红来缩短时间,但是我想炫耀Julia的有理数类型。:)

取消高尔夫:

# Read two names from STDIN on separate lines
A, B = map(chomp, readlines())

# Construct their lowercase equivalents
a, b = map(lowercase, [A, B])

# Construct a rational number where the numerator is the
# length of the intersection of a and b and the denominator
# is the product of the lengths
r = Rational(length(a  b), length(a) * length(b))

# Tell us about the love
print("$A and $B have $(r.num) out of $(r.den) chances of love.")

Rational()函数构造类型的对象Rational,其具有字段numden,分别对应于所述分子和分母。在这里使用这种类型的好处是,Julia为我们做了减少。我们不必担心自己减少比例。


2

CJam,55个字节

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.

1

Dyalog APL,84-30 = 54字节

∊' and ' ' have' 'out of' 'chances of love.',¨⍨⊢,{+/⌊⌿+⌿↑1↑¨⍨-32|⍉⎕ucs↑⍵}(,÷∨)(×/≢¨)

这是一列火车,灵感来自阿达姆的回答

×/≢¨ 长度的乘积

{+/⌊⌿+⌿↑1↑¨⍨-32|⍉⎕ucs↑⍵} 爱的机会

(,÷v)串联除以GCD;在上面两个表达式之间,这减少了分数

⊢, 在名称之前

,¨⍨ 将左侧的字符串与右侧的值混洗

展平


“爱的机会”的详细计算: {+/⌊⌿+⌿↑1↑¨⍨-32|⍉⎕ucs↑⍵} ↑⍵ arrange the names in a 2×N matrix, pad with spaces ⎕ucs take ascii codes ⍉ transpose the matrix as N×2 32| modulo 32 to case-fold 1↑¨⍨- for each X create a vector 0 0...0 1 of length X ↑ arrange into a 3d array, pad with 0s +⌿ 1st axis sum, obtain freqs per letter and per name ⌊⌿ 1st axis min, obt. nr of common occurrences per letter +/ sum

测试测试2


0

Java 8,192字节

BiFunction y=(a,b)->a+" and "+b+" have "+(""+a).chars().filter(c->(""+b).toUpperCase().indexOf(c>'Z'?c-32:c)>=0).count()+" out of "+(""+a).length()*(""+b).length()+" chances of love.";

例如:

System.out.println(y.apply("Bill","Jill"));
System.out.println(y.apply("Wesley","Polly"));

0

Ruby,153个字节

比我预期的更长。我不知道这30个字节的奖励是否适用。

d=[];2.times{d<<gets.chop<<$_.downcase.chars<<$_.length-1};$><<"#{d[0]} and #{d[3]} have #{d[2]-(d[1]-d[4]).length} out of #{d[2]*d[5]} chances of love."

您能否添加一个指向编译器的链接?(我推荐Ideone)
juniorRubyist 2015年

0

Python 2.7,161个字节

import string;r=raw_input;a,b=r(),r();print'%s and %s have %i out of %i chances of love'%(a,b,sum(map(lambda c:c in a and c in b,string.letters)),len(a)*len(b))

在这里测试:http : //ideone.com/jeoVgV

这是简化分数的版本:

import string,fractions;r=raw_input;a,b=r(),r();F=fractions.Fraction(sum(map(lambda c:c in a and c in b,string.letters)),len(a)*len(b))
print'%s and %s have %i out of %i chances of love'%(a,b,F.numerator,F.denominator)

不幸的是,这个人的分数是219-30 = 189 ...


1
您可以添加以下编译器:ideone.com/jeoVgV
juniorRubyist
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.