Love Tester Code Golf


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.

เวสลีย์และพอลลี่มี 2 ตัวอักษรที่เหมือนกันyและlและความยาวของผลิตภัณฑ์ของพวกเขาคือ 6 * 5 = 30

การป้อนข้อมูล:

Bill
Jill

เอาท์พุท:

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

โบนัส

  • ลบ 30 ไบต์สำหรับการใช้เศษส่วนแบบง่ายกล่าวx out of yคืออยู่ในรูปแบบที่ลดลงอย่างสมบูรณ์

ลีดเดอร์บอร์ด:

การจัดอันดับจะถูกกำหนดโดยภาษา รหัสกอล์ฟสิ้นสุดในวันที่ 17 ตุลาคมเวลา 22:20 น. เวลาตามฤดูกาลของแปซิฟิก (แคลิฟอร์เนีย)

ตัวแทนรางวัล

  • คุณจะได้รับ 10 ตัวแทน (upvote) เนื่องจากอยู่ใน 5 อันดับแรก (ยกเว้นที่แรก)
  • คุณจะได้รับ 15 ตัวแทน (รายการที่ยอมรับ) สำหรับการเป็นที่หนึ่ง
  • คุณอาจได้รับรางวัลมากมายจากบุคคลอื่น

นี่คือตัวอย่างข้อมูลเพื่อสร้างทั้งกระดานผู้นำปกติและภาพรวมของผู้ชนะตามภาษา **

เพื่อให้แน่ใจว่าคำตอบของคุณปรากฏขึ้นโปรดเริ่มคำตอบด้วยหัวข้อโดยใช้เทมเพลต Markdown ต่อไปนี้:

# Language Name, N bytes

ที่Nมีขนาดของส่งของคุณ หากคุณปรับปรุงคะแนนของคุณคุณสามารถเก็บคะแนนเก่าไว้ในบรรทัดแรกโดยการตีพวกเขาผ่าน ตัวอย่างเช่น

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

** ข้อมูลกองจาก Sign That Word โดย Kslkgh


ขอแสดงความยินดีกับสิ่งต่อไปนี้:

  1. ผู้ชนะเดนนิส (Pyth)
  2. เดนนิส (CJam)
  3. NBZ (APL)
  4. molarmanful (JavaScript ES6)
  5. Alex A. (Julia)

3
สิ่งที่ส่งออกควรจะให้Aaron\nAhmad? หรือsamename\nsamename?
lirtosiast

3
ฉันไม่ชัดเจนว่าคุณจะนับตัวอักษรทั้งหมดอย่างไรเมื่อตัวอักษรซ้ำกัน ถ้าชื่อมีaและbของตัวอักษรบางทำนับเหล่านั้นmin(a,b)ซ้ำ?
xnor

ตามที่ @ xor พูดคุณจะนับตัวอักษรซ้ำ ๆ จากตัวอย่างที่สองดูเหมือนว่าคุณกำลังนับอักขระซ้ำแล้วซ้ำอีกดังนั้นหากตัวอย่างแรกกลับด้านคุณคาดหวังผลที่แตกต่างกันหรือไม่
Rnet

มันไม่เกี่ยวข้องกับความซับซ้อนของ kolmogorovทั้งหมดเหรอ?

คำตอบ:


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 APL , 94 91-30 = 61 ไบต์

โดยปกติแล้วการเล่นกอล์ฟ APL จะทำให้ได้รหัสที่กะทัดรัดกว่า แต่ไม่ซับซ้อนกว่าปกติ แต่ในกรณีนี้ฉันบันทึกตัวอักษรในรูปแบบที่น่าเกลียด:

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

,⍪'out of' 'chances of'สร้างตารางขนาด 2x2 (ซ้าย) และข้อความ (ขวา)
×/≢¨⍵ของความยาวที่
32|⎕UCS¨⍵กลมกลืนกันค่า UCS ทั้งบนและ
≢⊃∩/ล่างเป็นจุดตัดของชุดทั้งสอง
⊢÷∨/แบ่งการนับและผลิตภัณฑ์ที่มี GCD
,' love.',⍨∊ทำให้เป็นรายการที่เรียบง่ายและต่อท้ายรัก.
⍵,⍪'and' 'have'สร้างตารางชื่อ 2 × 2 (ซ้าย) และข้อความ (ขวา) เติม
∊' ',¨ช่องว่างให้แต่ละเซลล์ของตารางจากนั้นสร้างเป็นรายการ
1↓แบบง่าย ๆปล่อยพื้นที่ที่ไม่จำเป็นเริ่มต้น

ขอบคุณ ngn สำหรับ -3 ไบต์


ปรากฏว่านี่เป็นเพียงฟังก์ชั่นในขณะที่ OP ระบุอินพุตจาก STDIN และเอาท์พุทไปยัง STDOUT (เช่นโปรแกรมเต็มรูปแบบมากกว่าฟังก์ชั่น)
Alex A.

@AlexA APL ไม่มี STDIN 'Wesley' 'Polly'แต่สามารถออกพร้อมที่จะยอมรับสองชื่อในรูปแบบ หากคุณคิดว่าสิ่งนี้จะยุติธรรมมากขึ้นคุณสามารถผนวก(U + 2395) จนสุดบรรทัด (หลังจากนั้น}) และปรับคะแนนเป็น 65
Adám

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

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

อาจจะสั้นกว่านี้ได้หากไม่ได้รับโบนัส แต่ฉันต้องการแสดงให้เห็นถึงประเภทจำนวนตรรกยะของจูเลีย :)

Ungolfed:

# 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ที่มีสาขาnumและdenสอดคล้องกับเศษและส่วนตามลำดับ ประโยชน์ของการใช้ประเภทนี้ที่นี่คือ 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.";

Ex:

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

0

ทับทิม 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
นี่คือคอมไพเลอร์ที่คุณสามารถเพิ่ม: ideone.com/jeoVgV
juniorRubyist
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.