ฉันจะจบลงด้วย FizzBuzz นี้ได้อย่างไร


21

FizzBuzz นั้นง่ายมากคุณสามารถเดิมพันย้อนหลังได้ ในการท้าทายนี้คุณจะได้รับความยาวของสตริง FizzBuzz และจะต้องให้จำนวนเต็มบวกที่สร้างสตริงนั้น

ลักษณะ

ในการแยกส่วนนี้สตริง FizzBuzz สำหรับnจะถูกสร้างขึ้นโดยอัลกอริทึมต่อไปนี้

เริ่มต้นด้วยสตริงว่างและสำหรับทุกi=1..n(รวม):

  1. ถ้าiหารด้วย3และโดยการ5ผนวกFizzBuzzสตริง
  2. หากiเป็นเพียงหารด้วยผนวก3Fizz
  3. หากiเป็นเพียงหารด้วยผนวก5Buzz
  4. ถ้าหารด้วยค่าผนวกแทนทศนิยมของii

ตัวอย่างเช่นFizzBuzz(15)มีดังต่อไปนี้:

12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz

คุณจะได้รับและจะต้องตรวจสอบLength(FizzBuzz(n)) nคุณอาจสันนิษฐานว่าอินพุตนั้นเป็นค่าบวกและจะเป็นความยาวของสตริง FizzBuzz บางตัวเสมอ

กฎระเบียบ

โซลูชันของคุณอาจเป็นโปรแกรมที่สมบูรณ์หรือคำนิยามฟังก์ชันในภาษาที่ยอมรับได้มาตรฐานใด ๆ โปรแกรมของคุณ / ฟังก์ชั่นอาจจะใช้เวลาในการขัดแย้งและกลับคำตอบใด ๆ ในทางที่ได้รับการยอมรับ standardly ช่องโหว่มาตรฐานเป็นสิ่งต้องห้าม

คุณอาจคิดว่าอินพุตนั้นเป็นค่าบวกและถูกต้อง (อธิบายถึงความยาวของสตริง FizzBuzz บางอัน) และมีขนาดเล็กกว่าจำนวนเต็มที่มากที่สุดซึ่งสามารถแสดงได้ในภาษาของคุณ

นี่คือรหัสกอล์ฟเพื่อชัยชนะที่สั้นที่สุดนับไบต์

ตัวอย่าง

นี่คือตัวอย่างบางกรณี

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

แก้ไข

แก้ไขกรณีทดสอบล่าสุด ขอบคุณ @SteadyBox


โอ๊ะ! ฉันพยายามทำการสอบถามซ้ำ แต่ตัวเลขของฉันใหญ่เกินไป ...
0WJYxW9FMN

ที่เกี่ยวข้อง ที่เกี่ยวข้อง
Digital Trauma

3
@Toto สิ่งนี้ซ้ำกันได้อย่างไร
AdmBorkBork

1
@Toto นี่ไม่ได้ซ้ำกันเลย บางทีคุณควรอ่านสิ่งที่เป็นวิธีการที่ซ้ำกัน
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

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

จำเป็นต้องใช้ "return i" หรือไม่เมื่อ "i" เป็นตัวแปรโกลบอล? -) และคุณสามารถแทนที่การเรียกใช้ snprintf ที่ยาวด้วย log10 (i) +1 หากคอมไพล์นั้นและได้รับอนุญาต ... มันใช้ได้กับฉันด้วย gcc -lm
Rennex

@ เรนเน็กซ์return i;เป็นสิ่งจำเป็นเนื่องจากเป็นวิธีที่ได้รับการยอมรับมาตรฐานในการเขียนโค้ดกอล์ฟในขณะที่การแก้ไขตัวแปรทั่วโลกไม่ได้เป็นเพียง ฉันพิจารณาการใช้log10(i)+1แต่ฉันคิดว่าอาจทำให้เกิดปัญหาเนื่องจากการแปลงเป็นสองเท่าและย้อนกลับ (เช่นpow(i)ไม่น่าเชื่อถือกับจำนวนเต็ม) ดูเหมือนว่าตอนนี้มันจะทำงานได้ดีสำหรับค่าบวกทั้งหมดที่ a intสามารถแทนได้ดังนั้นฉันอาจใช้มันได้ (ด้วยค่าที่มากกว่าแบบธรรมดาintก็สามารถทำได้บางครั้งก็ล้มเหลว แต่นั่นก็ไม่สำคัญอะไร)
Steadybox

อื่อโอเค. ฉันใหม่สำหรับกอล์ฟรหัสนี้ แต่ฉันดูที่ลิงค์กฎในคำถามและมันบอกว่า "ฟังก์ชั่นอาจส่งออกโดยการแก้ไขข้อโต้แย้งของพวกเขาหรือเขียนข้อโต้แย้งออกไป" ไม่ได้หมายความว่าอย่างน้อยสามารถใช้อาร์กิวเมนต์ตัวชี้ผลลัพธ์ได้
Rennex

@ Renex ใช่ฉันเดาฉันสามารถใช้nเป็นตัวชี้และจากนั้นเพียงปรับเปลี่ยนค่าที่ชี้ไปที่จุดสิ้นสุด แต่จะต้องมีรหัสเพิ่มเติมที่ไซต์การโทรเพื่อให้สามารถพิมพ์ค่าจึงรู้สึก นิดหน่อยชอบโกงฉัน
Steadybox

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

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&

คำอธิบาย

มาตรฐานForห่วงซึ่งเพิ่มขึ้นnจนเป็นอย่างน้อยเท่ากับการป้อนข้อมูลs := Length(FizzBuzz(n)) #สิ่งที่น่าสนใจเพียงอย่างเดียวคือวิธีที่ฉันคำนวณความยาวของ(n+1)คำที่ -th ของลำดับ FizzBuzz

                ++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

ใช้แนวคิดเดียวกันกับโซลูชัน Jelly ของ Jonathan Allen

ลองใช้กับmatl.suever.net !


ลงไปที่ 28 ตอนนี้! : -PI คิดว่าแนวทางของเราคล้ายกันมากขึ้นแล้วตอนนี้
Luis Mendo

อ่าทำได้ดีมาก! ใช่มันดูเหมือน :)
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;}

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

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


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

ฟังก์ชั่นวนซ้ำ จะต้องมีการ จำกัด การเรียกซ้ำเพิ่มขึ้นสำหรับผลใด ๆ ที่สูงกว่า 1,000

คำอธิบาย:

# 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

k, 33 ไบต์

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

คำอธิบายสั้น ๆ (python-ish):

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

ยินดีต้อนรับสู่ Programming Puzzles & Code Golf! เพื่อให้คุณรู้ว่าการลงคะแนนถูกสร้างโดยอัตโนมัติโดยผู้ใช้ชุมชนเมื่อคำตอบถูกแก้ไข ผมคิดว่านี่เป็นข้อผิดพลาด
Dennis


1

Ruby, 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}

เดิมทีฉันกำลังหลีกเลี่ยงmonstrosityโอเปอร์เรเตอร์ที่ซ้อนกันและลดลงถึง 69 bytes:

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


สิ่งนี้ส่งคืนผลลัพธ์ที่ไม่ถูกต้องสำหรับฉันในกรณีทดสอบสองครั้งสุดท้าย: 75 แทน 100 และ 686 แทน 1001

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

Japté , 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.