लव टेस्टर कोड गोल्फ


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 अक्टूबर को रात 10:20 बजे प्रशांत डेलाइट टाइम (कैलिफ़ोर्निया) में समाप्त होता है

रेप्स प्राइज

  • आपको शीर्ष 5 में रहने के लिए 10 प्रतिनिधि (एक अपवोट) प्राप्त होगा (पहले स्थान को छोड़कर)।
  • पहले स्थान पर रहने के लिए आपको 15 प्रतिनिधि (स्वीकृत प्रविष्टि) प्राप्त होंगे।
  • आपको किसी अन्य व्यक्ति से इनाम भी मिल सकता है।

यहां एक नियमित लीडरबोर्ड और भाषा द्वारा विजेताओं का अवलोकन दोनों उत्पन्न करने के लिए एक स्टैक स्निपेट है। **

यह सुनिश्चित करने के लिए कि आपका उत्तर दिखाई दे रहा है, कृपया अपना उत्तर शीर्षक मार्कडाउन टेम्पलेट का उपयोग करके शीर्षक के साथ शुरू करें:

# Language Name, N bytes

Nआपके प्रस्तुत करने का आकार कहां है। यदि आप अपने स्कोर में सुधार करते हैं, तो आप पुराने अंकों को हेडलाइन में रख सकते हैं , उनके माध्यम से स्ट्राइक करके। उदाहरण के लिए:

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

** Kslkgh द्वारा साइन वर्ड से स्निपेट स्टैक करें


निम्नलिखित के लिए बधाई:

  1. विजेता डेनिस (पायथ)
  2. डेनिस (CJam)
  3. NBZ (APL)
  4. शोकाकुल (जावास्क्रिप्ट ईएस 6)
  5. एलेक्स ए। (जूलिया)

3
आउटपुट किसके लिए होना चाहिए Aaron\nAhmad? या samename\nsamename?
lirtosiast

3
मैं स्पष्ट नहीं हूँ कि जब आप पत्र दोहराते हैं तो आप कुल अक्षरों की गिनती कैसे करते हैं। यदि नाम aऔर bकुछ अक्षर के हैं, तो क्या वे min(a,b)दोहराए जाते हैं?
xnor

जैसा कि @xor कहता है, आप बार-बार पत्र कैसे गिन रहे हैं? दूसरे उदाहरण से, ऐसा प्रतीत होता है कि आप बार-बार दोहराए जाने वाले वर्णों की गिनती कर रहे हैं, इसलिए यदि पहले उदाहरण को उलट दिया गया है, तो क्या आप एक अलग परिणाम की उम्मीद करते हैं?
Rnet

क्या यह कोलमोगोरोव-जटिलता से पूरी तरह असंबंधित है ?

जवाबों:


1

पायथ, 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

दिल्लोग एपीएल , 94 91-30 = 61 बाइट्स

आमतौर पर एपीएल गोल्फिंग कोड में परिणाम होता है जो अधिक कॉम्पैक्ट होता है - लेकिन सामान्य से अधिक जटिल नहीं - लेकिन इस मामले में मैं बदसूरत तरीकों से चार्ट को बचाता हूं:

{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 (केवल एक फ़ंक्शन के बजाय एक पूर्ण कार्यक्रम) के लिए आउटपुट।
एलेक्स ए।

@AlexA। APL में STDIN नहीं है, लेकिन प्रारूप में दो नामों को स्वीकार करने के लिए संकेत जारी कर सकता है 'Wesley' 'Polly'। अगर आपको लगता है कि यह अधिक उचित होगा, तो बेझिझक (U + 2395) को लाइन के बहुत अंत तक (बाद में }), और स्कोर को 65 पर समायोजित करें।
Adám

2

जावास्क्रिप्ट ईएस 6, 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.`

"प्यार" के लिए इतना ... मैं वास्तव में कम बाइट्स के साथ कर सकता था।

फ़ायरफ़ॉक्स में कोड स्निपेट चलाएँ।


2
ऐसा प्रतीत होता है कि यह केवल एक फ़ंक्शन है, जबकि OP STDIN से इनपुट निर्दिष्ट करता है और STDOUT (केवल एक फ़ंक्शन के बजाय एक पूर्ण कार्यक्रम) के लिए आउटपुट।
एलेक्स ए।

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

यह शायद बोनस के लिए नहीं जाने से कम किया जा सकता है , लेकिन मैं जूलिया के तर्कसंगत नंबर को दिखाना चाहता था। :)

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क्रमश: अंश और हर के लिए इसी। इस प्रकार का उपयोग करने का लाभ यह है कि जूलिया हमारे लिए कटौती करता है; हमें स्वयं अंश को कम करने की चिंता करने की आवश्यकता नहीं है।


2

सीजेएम, 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

दिल्लोग एपीएल, 84-30 = 54 बाइट्स

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

यह एक ट्रेन है , जो अडम के जवाब से प्रेरित है ।

×/≢¨ लंबाई का उत्पाद

{+/⌊⌿+⌿↑1↑¨⍨-32|⍉⎕ucs↑⍵} प्रेम की संभावना

(,÷v)जीसीडी द्वारा विभाजित संघटन; उपरोक्त दो अभिव्यक्तियों के बीच, यह अंश को कम करता है

⊢, नामों को प्रस्तुत करना

,¨⍨ दाईं ओर के मूल्यों के साथ बाईं ओर तारों को फेरबदल करता है

समतल करना


"प्यार की संभावना" विस्तार से गणना: {+/⌊⌿+⌿↑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

परीक्षण , test2


0

जावा 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

रूबी, 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 सलाह देते हैं)
जूनियर रूबिस्ट

0

पायथन 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
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.