ラブテスターコードゴルフ


9

2つの名前に共通する文字の総数を数え、その長さの積を見つけるプログラムを作成し、「愛のテスター」として機能します。

条件:1:1の回答(3つのうち3つなど)の出力が得られない場合があります。

入力

STDINまたは最も近い代替からの2つの名前。

出力

x大文字と小文字を区別せずに、2つの名前に共通する文字の総数として計算します。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.

ウェズリーとポリーには2文字の共通のがyありl、長さの積は6 * 5 = 30です。

入力:

Bill
Jill

出力:

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

ボーナス

  • 単純化された小数を使用するにx out of yは、30バイトを減算します。つまり、完全に縮小された形式です。

リーダーボード:

ランキングは言語によって決定されます。コードゴルフは10月17日午後10時20分(太平洋夏時間)(カリフォルニア)に終了します。

担当者賞

  • あなたはトップ5(最初の場所を除く)にいるための10の担当者(賛成票)を受け取ります。
  • あなたは最初の場所であるために15の担当者(受け入れられたエントリ)を受け取ります。
  • また、他の人から賞金がもらえるかもしれません。

これは、通常のリーダーボードと言語別の勝者の概要の両方を生成するスタックスニペットです。**

回答が確実に表示されるようにするには、次のMarkdownテンプレートを使用して、見出しから回答を始めてください。

# Language Name, N bytes

N提出物のサイズはどこですか。スコアを向上させる場合、古いスコアを打ち消すことで見出しに残すことができます。例えば:

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

** KslkghによるSign That Wordのスタックスニペット


おめでとうございます。

  1. Winner Dennis(Pyth)
  2. デニス(CJam)
  3. NBZ(APL)
  4. モルモルフル(JavaScript ES6)
  5. アレックスA.(ジュリア)

3
出力の目的はAaron\nAhmad何ですか?またはsamename\nsamename
リルトシアスト2015年

3
文字が繰り返されるときに、どのようにして共通の文字の総数を数えているのかわかりません。名前が持っている場合ab、いくつかの手紙を、それらのためのカウントを行うことmin(a,b)を繰り返し?
xnor

@xorが言うように、繰り返し文字をどのように数えますか?2番目の例から、繰り返される文字を数えているように見えるので、最初の例が逆になっている場合、別の結果が予想されますか?
Rnet

それはコルモゴロフの複雑さとはまったく無関係ですか?

回答:


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がありませんが、形式で2つの名前を受け入れるようにプロンプ​​トを出すことができます'Wesley' 'Polly'。これがより公平であると思われる場合は、行の最後(の後)に(U + 2395)を追加して}、スコアを65に調整してください。
AdámOct

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.")

これはおそらくボーナスを獲得しないことで短くなる可能性がありますが、私はジュリアの有理数の型を自慢して見せたかったのです。:)

非ゴルフ:

# 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で連結された連結。上記の2つの式の間で、これにより分数が減少します

⊢, 名前を追加する

,¨⍨ 左側の文字列を右側の値でシャッフルします

平らにする


「愛のチャンス」の計算の詳細: {+/⌊⌿+⌿↑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

testtest2


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

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
追加できるコンパイラは次のとおり
juniorRubyist
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.