मैं इस FizzBuzz के साथ कैसे समाप्त हुआ?


21

FizzBuzz इतना सरल है, शर्त लगाएं कि आप इसे पीछे की तरफ कर सकते हैं। इस चुनौती में, आपको FizzBuzz स्ट्रिंग की लंबाई दी जाएगी और उस स्ट्रिंग को बनाने वाले सकारात्मक पूर्णांक को देना होगा।

विवरण

इसे नीचे तोड़ने के लिए n, निम्न एल्गोरिथम द्वारा एक FizzBuzz स्ट्रिंग उत्पन्न की जाती है।

एक खाली स्ट्रिंग के साथ शुरू करें और, हर i=1..n(समावेशी) के लिए:

  1. यदि iद्वारा 3और से विभाज्य है 5, FizzBuzzतो स्ट्रिंग में जोड़ें ।
  2. अगर iसिर्फ 3एपेंड करके विभाज्य है Fizz
  3. अगर iसिर्फ 5एपेंड करके विभाज्य है Buzz
  4. यदि iन तो विभाज्य है, तो दशमलव प्रतिनिधित्व को जोड़ दें i

तो उदाहरण FizzBuzz(15)के लिए निम्नलिखित है:

12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz

आपको दिया जाएगा Length(FizzBuzz(n))और निर्धारित किया जाना चाहिए n। आप मान सकते हैं कि इनपुट सकारात्मक है और हमेशा कुछ FizzBuzz स्ट्रिंग की लंबाई होने वाली है।

नियम

आपका समाधान किसी भी मानक रूप से स्वीकार्य भाषा में एक पूरा कार्यक्रम या फ़ंक्शन परिभाषा हो सकता है। आपका कार्यक्रम / समारोह किसी भी मानक रूप से स्वीकृत तरीके से तर्क और रिटर्न जवाब ले सकता है । मानक खामियों को मना किया जाता है।

आप यह मान सकते हैं कि इनपुट सकारात्मक और वैध है (कुछ FizzBuzz स्ट्रिंग की लंबाई का वर्णन करता है) और यह आपकी भाषा में मूल रूप से सबसे बड़े पूर्णांक सूचक से छोटा है।

यह कोड गोल्फ है, इसलिए सबसे कम बाइट-काउंट जीतता है।

उदाहरण

यहाँ कुछ उदाहरण मामले हैं

Length(FizzBuzz(n)) -> n
1                   -> 1
6                   -> 3
15                  -> 6
313                 -> 100
3677                -> 1001

संपादित करें

अंतिम परीक्षण का मामला। धन्यवाद @SteadyBox


अरे! मैंने पुनरावृत्ति करने की कोशिश की लेकिन मेरी संख्या बहुत बड़ी थी ...
0WJYxW9FMN


3
@ कैसे यह एक डुप्लिकेट है?
AdmBorkBork

1
@ यह सब डुप्लिकेट में नहीं है। हो सकता है कि आपको डुप्लिकेट होने के मतलब पर पढ़ना चाहिए।
mbomb007

जवाबों:


8

जेली ,  16  14 बाइट्स

2 बाइट्स के )लिए µ€और Äके लिए और अधिक हाल ही में भाषा सुविधाओं का उपयोग कर बचाया+\

3,5ḍS×4oDL$)Äi

इसे ऑनलाइन आज़माएं! या परीक्षण मामलों को देखें।

कैसे?

1इनपुट से हर आइटम की लंबाई की एक सूची बनाता है , इसके अलावा कम करता है और फिर सूची में इनपुट का एक-आधारित सूचकांक पाता है। (इसका अर्थ अमान्य इनपुट परिणाम भी है 0, "सूची में नहीं")।

3,5ḍS×4oDL$)Äi - Main link: theLength
           )    - perform the chain to the left for each (€) in
                     implicit range from 1 to the input and
                     pass the result into the monadic chain (µ) to the right
3,5            - 3 paired with 5: [3,5]
   ḍ           - divides?  for a multiple of 15 [1,1]; sum = 2; times 4 = 8
    S          - sum       for a multiple of  5 [0,1]; sum = 1; times 4 = 4
     ×4        - times 4   for a multiple of  3 [1,0]; sum = 1; times 4 = 4
                           for none of those    [0,0]; sum = 0; times 4 = 0
          $    - last two links as a monad
        D      -     to decimal digit list
         L     -     length - e.g. 313 -> [3,1,3] -> 3
       o       - logical or: replace a 0 with the decimal length, keep the 4s and 8s
            Ä  - reduce with addition: e.g. [1,1,4,1, 4, 4, 1, 1, 4, 4, 2, 4, 2 ,2, 8]
                                         -> [1,2,6,7,11,15,16,17,21,25,27,31,33,35,43]
             i - index of theLength in that list (e.g. 15 is at index 6)

11

सी, 81 78 बाइट्स

l,i;f(n){for(l=i=0;l<n;l+=++i%3?i%5?snprintf(0,0,"%d",i):4:i%5?4:8);return i;}

68 बाइट्स यदि आप को doubleवापस करने और बदलने में कोई आपत्ति नहीं है :

l,i;f(n){for(l=i=0;l<n;l+=++i%3?i%5?log10(i)+1:4:i%5?4:8);return i;}

क्या "वापसी i" की आवश्यकता तब भी है, जब "i" एक वैश्विक चर है? -) और आप उस लॉन्ग स्निप्रफ कॉल को log10 (i) +1 से बदल सकते हैं, यदि वह संकलन करता है और अनुमति दी जाती है ... तो यह मेरे साथ काम करता है gcc
-lm

@ रेनेक्स की return i;आवश्यकता है, क्योंकि यह कोड गोल्फ में आउटपुट के लिए एक मानक रूप से स्वीकृत तरीका है जबकि केवल एक वैश्विक चर को संशोधित करना नहीं है। मैंने उपयोग करने पर विचार किया log10(i)+1, लेकिन मुझे लगा कि डबल और बैक में परिवर्तित होने के कारण इसमें कुछ समस्याएं हो सकती हैं (जैसे pow(i)पूर्णांक के साथ विश्वसनीय नहीं है)। अब ऐसा लगता है कि यह सभी सकारात्मक मूल्यों के लिए ठीक काम करता है जो एक intप्रतिनिधित्व कर सकते हैं, इसलिए मैं शायद इसका उपयोग कर सकता हूं। (एक साधारण से बड़े मूल्यों के साथ int, यह कभी-कभी विफल हो सकता है, लेकिन यहां कोई फर्क नहीं पड़ता।)
15

हम्म ठीक है। मैं इस कोड गोल्फ के लिए नया हूं, लेकिन मैंने प्रश्न में नियमों के लिंक को देखा, और यह कहता है कि "कार्य उनके तर्कों को संशोधित करके या तर्क को लिखने के लिए आउटपुट कर सकते हैं"। इसका मतलब यह नहीं है कि कम से कम एक परिणाम सूचक तर्क का इस्तेमाल किया जा सकता है?
रेनेक्स

@ रेनेक्स हाँ, मुझे लगता है कि मैं nएक पॉइंटर के रूप में ले सकता हूं और फिर अंत में इंगित होने वाले मूल्य को संशोधित कर सकता हूं , लेकिन मूल्य को प्रिंट करने में सक्षम होने के लिए कॉल साइट पर अधिक कोड की आवश्यकता होगी, इसलिए ऐसा लगता है मुझे धोखा देने जैसा है।
स्टेडीबॉक्स

6

MATL , 31 28 27 बाइट्स

`@:tI5h!\XJA)VXznJ~z4*+G-}@

इसे ऑनलाइन आज़माएं!

व्याख्या

`        % Do...while
  @:     %   Push array [1 2 ...k], where k is iteration index
  t      %   Duplicate  
  I5h!   %   Push column vector [3; 5]
  \      %   Modulo, with broadcast. Gives 2 × k matrix
  XJ     %   Copy into clipboard J
  A      %   Row vector that contains true for columns that contain two nonzeros
  )      %   Index with that vector. This keeps numbers that are non-fizz/buzz
  V      %   Convert to string. This inserts spaces between numbers
  Xzn    %   Number of nonspace characters
  J      %   Push 2 × k matrix resulting from modulo operation again
  ~z     %   Number of zeros
  4*     %   Multiply by 4. Gives number of characters corresponding to fizz/buzz
  +      %   Add
  G-     %   Subtract input. This is the loop condition: exit if 0
}        % Finally (execute right before exiting loop)
  @      %   Push current iteration index
         % End (implicit)
         % Display (implicit)

4

गणितज्ञ, 67 बाइट्स

(For[n=s=0,s<#,s+=Tr[4Boole[{3,5}∣++n]]/. 0:>IntegerLength@n];n)&

यह मेरे प्रारंभिक समाधान की तुलना में तेज़ और छोटा दोनों है:

1//.x_/;Sum[Tr[4Boole[{3,5}∣n]]/. 0:>IntegerLength@n,{n,x}]!=#:>x+1&

या इसे छोटा करने की मेरी बेताब कोशिश:

(s=0;1)//.x_/;(s+=Tr[4Boole[{3,5}∣x]]/. 0:>IntegerLength@x)!=#:>x+1&

व्याख्या

मानक Forलूप जो कि इनपुट के कम से कम बराबर होने nतक बढ़ाता s := Length(FizzBuzz(n))है #। एकमात्र दिलचस्प बिट यह है कि मैं (n+1)FizzBuzz अनुक्रम के -th अवधि की लंबाई की गणना कैसे करता हूं

                ++n                           Preincrement n
          {3,5}∣                              Test for divisibility by 3 and 5 (returns a list)
    Boole[         ]                          Convert True to 1 and False to 0
   4                                          Multiply by 4
Tr[                 ]                         Sum
                     /.                       Replace
                        0                     0 (leading space is necessary or it thinks we are dividing by 0.0)
                         :>                   with
                           IntegerLength@n    the number of digits in n

3

MATL, 31 30 28 बाइट्स

:tI5h!\~s4*t~b10&YlkQ*+YsG=f

जोनाथन एलन के जेली समाधान के समान विचार का उपयोग करता है।

इसे matl.suever.net पर आज़माएं !


28 से नीचे अब! : -मैं समझता हूं कि हमारे दृष्टिकोण अब अधिक समान हैं
लुइस मेंडो

आह, अच्छा काम! हाँ, ऐसा लगता है :)
बी। मेहता

3

जावा 8, 100 97 बाइट्स

golfed:

l->{int i=0;for(String s="";s.length()<l;)s+=++i%15<1?"12345678":i%5<1||i%3<1?"1234":i;return i;}

Ungolfed:

import java.util.function.*;

public class HowDidIEndUpWithThisFizzBuzz {

  public static void main(String[] args) {
    for (final int[] data : new int[][] { { 1, 1 }, { 6, 3 }, { 15, 6 },
        { 313, 100 }, { 3677, 1001 } }) {
      final int fizzBuzzLength = data[0];
      final int expected = data[1];
      final int actual = f(l -> {
        int i = 0;
        for (String s = ""; s.length() < l;) {
          s += (++i % 15 < 1 ? "12345678" : (i % 5 < 1 || i % 3 < 1 ? "1234" : i));
        }
        return i;
      } , fizzBuzzLength);
      System.out.println("Length(FizzBuzz(n)) -> " + fizzBuzzLength);
      System.out.println("Expected            -> " + expected);
      System.out.println("Actual              -> " + actual);
      System.out.println();
    }

  }

  private static int f(IntFunction<Integer> function, int fizzBuzzLength) {
    return function.apply(fizzBuzzLength);
  }
}

आउटपुट:

Length(FizzBuzz(n)) -> 1
Expected            -> 1
Actual              -> 1

Length(FizzBuzz(n)) -> 6
Expected            -> 3
Actual              -> 3

Length(FizzBuzz(n)) -> 15
Expected            -> 6
Actual              -> 6

Length(FizzBuzz(n)) -> 313
Expected            -> 100
Actual              -> 100

Length(FizzBuzz(n)) -> 3677
Expected            -> 1001
Actual              -> 1001

2

जावास्क्रिप्ट (ईएस 6), 62 57 बाइट्स

f=(n,k=0)=>n?f(n-(++k%3?k%5?`${k}`.length:4:k%5?4:8),k):k

परीक्षण के मामलों


एक ही लंबाई के साथ वैकल्पिक अभिव्यक्ति (!(++k%3)+!(k%5)<<2||`${k}`.length):।
नील

2

जावास्क्रिप्ट (ईएस 6), 56 बाइट्स

f=(x,s=i=0)=>s[x]?i:f(x,s+[++i%3?i%5?i:1e3:i%5?1e3:1e7])
<!-- snippet demo: -->
<input list=l oninput=console.log(f(this.value))>
<datalist id=l><option value=1><option value=6><option value=15><option value=313><option value=3677></datalist>


2

पायथन 3, 78 बाइट्स

f=lambda i,n=1,s=0:~-n*(s==i)or f(i,n+1,s+(4*((n%3<1)+(n%5<1))or len(str(n))))

पुनरावर्ती कार्य। आवश्यकता होगी 1000 से ऊपर के किसी भी परिणाम के लिए पुनरावृत्ति की सीमा बढ़े।

स्पष्टीकरण:

# i = length of final string
# n = current number in sequence, starting with 1
# s = length of current string, starting with 0
f=lambda i,n=1,s=0: \

# if s==1, this will evaluate to n+1, which is NOT 0, and will return
# else, it will evaluate to (n+1)*0, and trigger the second half of the OR clause
~-n*(s==i)or \

# recursively call the next iteration, with the next number in the sequence
f(i,n+1, \ 

# increase s by 4 if Fizz or Buzz, 8 if FizzBuzz, or len(n) if number
s+(4*((n%3<1)+(n%5<1))or len(str(n))))

1

पायथन, 93 बाइट्स

def g(n,c=0,a=[4,0]):
 while n:c+=1;s=a[c%3>0]+a[c%5>0];s+=(s<1)*len(str(c));n-=s
 return c

1

k, 33 बाइट्स

{1+&x=+\{(#$x;4;8)+/~3 5!'x}'1+!x}

संक्षिप्त (अजगर-ईश) स्पष्टीकरण:

{                                } / function(x):
                             1+!x  /   array from 1 to x, inclusive
                            '      /   for y in array:
        {                  }       /     function(y):
         (#$x;4;8)                 /       yield [ len(str(y), 4, 8 ][
                  +/~3 5!'x        /         sum([not(y mod 3), not(y mod 5)])
                                   /       ]
      +\                           /   cumulative sum of result of for loop
 1+&x=                             /   get index of x in cumulative sum, add one

Kmac 2016.06.28 का उपयोग कर उदाहरण:

 f:{1+&x=+\{(#$x;4;8)+/~3 5!'x}'1+!x}
 ,/f'1 6 15 313 3677
1 3 6 100 1001

प्रोग्रामिंग पहेलियाँ और कोड गोल्फ में आपका स्वागत है! जैसा कि आप जानते हैं, उत्तर को संपादित किए जाने पर समुदाय उपयोगकर्ता द्वारा स्वचालित रूप से डाउनवोट कर दिया गया था। मैं इसे एक बग मानता हूं
डेनिस


1

माणिक, 69 66 बाइट्स

->n{i=0;(i+=1;n-=i%3>0?i%5>0?i.to_s.size: 4:i%5>0?4:8)while n>0;i}

मूल रूप से, मैं नेस्टर्ड टर्नरी ऑपरेटर की विकृति से बच रहा था और 69 बाइट्स के लिए नीचे उतर गया:

->n{i=0;(i+=1;n-=(x=[i%3,i%5].count 0)>0?4*x:i.to_s.size)while n>0;i}

1

जावा 8, 95 93 बाइट्स

l->{int j=0,i=0;for(;j<l;)j+=++i%15<1?8:i%3<1||i%5<1?4:Math.floor(Math.log10(i)+1);return i;}

यह @ स्नोमैन के उत्तर का अनुकूलित संस्करण है


यह अंतिम दो परीक्षण मामलों पर मेरे लिए गलत परिणाम देता है: 100 के बजाय 75, और 1001 के बजाय


0

पर्ल 6 , 55 52 बाइट्स

{1+first $_,:k,[\+] map {4*($_%%3+$_%%5)||.chars},1..*}

{(0,{my \i=++$;$_+(4*(i%%3+i%%5)||i.chars)}...$_)-1}

इसे ऑनलाइन आज़माएं!

यह काम किस प्रकार करता है

{                                                  }  # A lambda.
  0                                                   # Start with 0.
   ,{                                     }           # Use the iteration formula...
     my \i=++$;                                       #   Fetch current index.
               $_+(                      )            #   Last element plus:
                   4*(i%%3+i%%5)                      #     Fizz/Buzz/FizzBuzz length,
                                ||i.chars             #     or number length.
                                           ...$_      # ...until the input is reached.
 (                                              )-1   # Sequence length minus 1.

0

जाप , 20 बाइट्स

@µ35ìx_XvZÃ*4ªXìÊ}f1

कोशिश करो

@µ35ìx_XvZÃ*4ªXìÊ}f1     :Implicit input of integer U
@                        :Function taking an integer X as argument
 µ                       :  Decrement U by
  35ì                    :    Digit array of 35
     x                   :    Reduce by addition
      _                  :    After passing each Z through the following function
       XvZ               :      Is X divisible by Z?
          Ã              :    End reduce
           *4            :    Multiply by 4
             ª           :    Logical OR with
              Xì         :      Digit array of X
                Ê        :      Length
                 }       :End function
                  f1     :First integer >=1 that returns a falsey value (i.e, 0) when passed through that function


0

सी (जीसीसी) , 68 बाइट्स, stdout-स्पामिंग

  • इस दृष्टिकोण के लिए H.PWiz को धन्यवाद , छह बाइट्स की बचत।
f(n,j,r){for(r=j=0;!r;r=!n*j)n-=++j%3?j%5?printf("%d",j):4:j%5?4:8;}

इसे ऑनलाइन आज़माएं!


सी (जीसीसी) , 74 बाइट्स

f(n,j,r){for(r=j=0;!r;r=!n*j)n-=++j%3?j%5?snprintf(0,0,"%d",j):4:j%5?4:8;}

इसे ऑनलाइन आज़माएं!


0

05AB1E , 17 बाइट्स

Lε35SÖ4*OygM}.¥sk

इसे ऑनलाइन आज़माएं या सभी परीक्षण मामलों को सत्यापित करें

स्पष्टीकरण:

L          # Create a list in the range [1, (implicit) input]
           #  i.e. 15 → [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
 ε         # Map each value to:
  35S      #  Push 35 as digit list: [3,5]
     Ö     #  Check if the current value is divisible by these (1 if truthy; 0 if falsey)
      4*   #  Multiply both by 4
        O  #  And take the sum of that
           #   i.e. 2 → [0,0] → [0,0] → 0
           #   i.e. 9 → [1,0] → [4,0] → 4
           #   i.e. 10 → [0,1] → [0,4] → 4
           #   i.e. 15 → [1,1] → [4,4] → 8
  yg       #  Push the current value again, and pop and push it's length
           #   i.e. 2 → 1
           #   i.e. 15 → 2
  M        #  And then push the largest value on the stack
           #   i.e. 0 and 1 → 1
           #   i.e. 8 and 2 → 8
 }.¥       # After the map: undelta the list (starting from 0)
           #  i.e. [1,1,4,1,4,4,1,1,4,4,2,4,2,2,8]
           #   → [0,1,2,6,7,11,15,16,17,21,25,27,31,33,35,43] 
    sk     # Swap to get the (implicit) input, and get its 0-based index in the list
           #  i.e. 15 → 6
           # (after which the result is output implicitly)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.