どうやってこのFizzBu​​zzになったのですか?


21

FizzBu​​zzはとても簡単です。この課題では、FizzBu​​zz文字列の長さを指定し、その文字列を生成した正の整数を指定する必要があります。

説明

これを分解するためにn、次のアルゴリズムによってFizzBu​​zz文字列が生成されます。

空の文字列で開始し、すべてi=1..n(包括的)に対して:

  1. 場合はiで割り切れる3とによって5、APPEND FizzBuzz文字列に。
  2. if i3appendで割り切れるだけFizzです。
  3. if i5appendで割り切れるだけBuzzです。
  4. iどちらでも割り切れない場合は、の10進表現を追加しますi

たとえばFizzBuzz(15)、次のとおりです。

12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz

あなたが与えられLength(FizzBuzz(n))、決定する必要がありnます。入力が正であり、常にFizzBu​​zz文字列の長さになると仮定することができます。

ルール

ソリューションは、完全にプログラムまたは標準的に受け入れ可能な言語の関数定義である場合があります。プログラム/関数は、引数を取り、標準的に受け入れられている方法で回答を返す場合があります。標準的な抜け穴は禁止されています。

入力は正で有効(FizzBu​​zz文字列の長さを表す)であり、言語でネイティブに表現できる最大の整数より小さいと仮定できます。

これはコードゴルフであるため、最短バイト数が勝ちます。

ここにいくつかの例があります

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

編集

最後のテストケースを修正しました。@SteadyBoxに感謝します。


ああ!...私は、再帰を実行しようとしましたが、私の数字はあまりにも大きかった
0WJYxW9FMN


3
@Totoこれはどのように重複していますか?
AdmBorkBork

1
@Totoこれはまったく重複していません。たぶん、重複とはどういう意味かを読んでください。
mbomb007

回答:


8

ゼリー 16  14 バイト

およびのためのより新しい言語機能)を使用して保存された2バイトµ€Ä+\

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

オンラインでお試しください!または、テストケースを参照してください。

どうやって?

1入力から入力までのすべての項目の長さのリストを作成し、追加により削減してから、リスト内の入力の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

C、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"がグローバル変数である場合、 "return i"も必要ですか?) gcc -lm
Rennex

@Rennex return i;は必要です。これは、グローバル変数を変更するだけではなく、コードゴルフで出力する標準的に受け入れられた方法であるためです。を使用することを検討しましたがlog10(i)+1、doubleおよびbackに変換するためにいくつかの問題が発生する可能性があると考えました(たとえばpow(i)、整数では信頼できない)。しかし、今ではそれintが表すことができるすべての正の値に対してうまく機能しているように見えるので、おそらくそれを使用することができます。(単純な値よりも大きい値では、int時々失敗することがありますが、ここでは重要ではありません。)
Steadybox

なるほど。このコードは初めてですが、質問のルールリンクを調べて、「関数は引数を変更するか、引数に書き込むことで出力される場合があります」と述べています。それは、少なくとも結果ポインタ引数を使用できることを意味しませんか?
Rennex

@Rennexええ、nポインタとして受け取り、最後に指す値を変更することはできますが、値を出力するにはコールサイトでより多くのコードが必要になるので、私にごまかすようなものです。
Steadybox

6

MATL31 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

Mathematica、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&

説明

少なくともinputと等しくなるまでFor増分nする標準ループ。興味深いのは、FizzBu​​zzシーケンスの-th項の長さをどのように計算するかだけですs := Length(FizzBuzz(n))#(n+1)

                ++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個まで!:-PIは、私たちのアプローチが今、より似ていると思います
ルイスメンドー

ああ、いい仕事だ!うん、それのように見える:)
B. Mehta

3

Javaの8、100の 97バイト

ゴルフ:

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;}

ゴルフをしていない:

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

JavaScript(ES6)、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

Javascript(ES6)、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

Python 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

Python、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}

簡単な(pythonのような)説明:

{                                } / 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

Java 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;}

これは@Snowmanの答えの最適化されたバージョンです


この最後の2つのテストケースで私のために戻って間違った結果:代わりに、100の75、代わりに1001の686

1

Groovy、76バイト

def f(n){i=0;for(s='';s.size()<n;)s+=++i%15<1?"1"*8:i%5<1||i%3<1?"1"*4:i;i;}

@Snowmanの回答とほぼ同じですが、Groovyのマジック/相違を使用してバイト数を削減します。


0

Perl 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

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