Zzub Zzif(反向Fizz嗡嗡声)


36

给定一小段嘶嘶声输出,其中所有数字均已删除,请以尽可能低的值填充正确的数字,以使嘶嘶声摘要正确。对于这一挑战的目的,fizzbuzz有3个和5个他们一贯的价值观,分别。

如果输入是fizzbuzz和空行的无效序列,则仅输出zzubzzif(带或不带换行)。

输入和输出可以是换行符分隔的行,也可以是任何适合您的语言的字符串数组格式。

您可以忽略大写,也可以随便做什么。

您需要选择处理一个或一个以上的这些:fizzbuzzfizz buzzbuzz fizz,等等,但你必须选择这些格式中的至少一个。

您可以假设所有输入都是fizzbuzz和空行的某些序列。

例子

输入:
嘶嘶声

输出:
2
fizz
4
输入:
嗡嗡声
嘶嘶声

输出:
buzz
fizz
7
输入:

嘶嘶声


输出:
13
14
fizzbuzz
16
17
输入:


输出:

1
输入:
嘶嘶声
嘶嘶声

输出:
zzubzzif
输入:


输出:
zzubzzif

Answers:


17

Java,205个字节

s->{o:for(int i=0,j,h=s.length;++i<16;){for(j=h;j-->0;)if(s[j].contains("fizz")^(i+j)%3<1||s[j].contains("buzz")^(i+j)%5<1)continue o;String z=i+"";for(j=1;j<h;)z+="\n"+(i+j++);return z;}return"zzubzzif";}

以a String[]作为输入并返回String

扩展的可运行代码:

public class C {

    static final java.util.function.Function<String[], String> f = s -> {
        o:
        for (int i = 0, j, h = s.length; ++i < 16;) {
            for (j = h; j --> 0;) {
                if (s[j].contains("fizz") ^ (i + j) % 3 < 1 ||
                    s[j].contains("buzz") ^ (i + j) % 5 < 1) {
                    continue o;
                }
            }
            String z = i + "";
            for (j = 1; j < h;) {
                z += "\n" + (i + j++);
            }
            return z;
        }
        return "zzubzzif";
    };

    public static void main(String[] args) {
        System.out.print(f.apply(new String[]{"fizz", "", "buzz"}));
    }
}

1
s / fizz / f,s / buzz / b
djechlin

10

C#,212个字节

我做了不可思议的事。我用一条goto语句打破了循环!

string[]R(string[]a){for(int i,l=a.Length,n=0;n++<15;){for(i=l;i-->0;)if(((n+i)%3<1?"fizz":"")+((n+i)%5<1?"buzz":"")!=a[i])goto z;for(i=l;i-->0;)a[i]=a[i]==""?""+(n+i):a[i];return a;z:;}return new[]{"zzubzzif"};}

这利用了序列必须在前15个元素内开始的事实。

缩进和可读性的新行:

string[]R(string[]a){
    for(int i,l=a.Length,n=0;n++<15;){
        for(i=l;i-->0;)
            if(((n+i)%3<1?"fizz":"")+((n+i)%5<1?"buzz":"")!=a[i])
                goto z;
        for(i=l;i-->0;)
            a[i]=a[i]==""?""+(n+i):a[i];
        return a;
    z:;
    }
    return new[]{"zzubzzif"};
}

C#没有break关键字吗?

2
@cat可以,但是会goto跳出内部循环并跳过外部循环的其余逻辑。
Hand-E-Food

我第一次听说gotoC#!甚至不知道C#拥有它!
sergiol

4

CJam,72个字节

"zzubzzif":MaqN/:Q,_F+{)_[Z5]f%:!MW%4/.*s\e|}%ew{:sQ1$A,sff&:s.e|=}=N*o;

如果必须打印zzubzzif,程序将退出并出现错误

使用Java解释器,可以关闭STDERR以抑制最终的错误消息。如果您在CJam解释器中尝试该程序,请忽略第一行之后的所有输出。

怎么运行的

"zzubzzif" e# Push that string.
:Ma        e# Save it in M and wrap in in an array.
qN/:Q      e# Split the input into lines and save in Q.
,_F+       e# Count the lines and add 15 to a copy of the result.
{          e# For each integer I between 0 and lines+14:
  )_       e#   Increment I and push a copy.
  [Z5]     e#   Push [3 5].
  f%       e#   Map % to push [(I+1)%3 (I+1)%5].
  :!       e#   Apply logical NOT to each remainder.
  MW%4/    e#   Push ["fizz" "buzz"].
  .*       e#   Vectorized string repetition.
  s\       e#   Flatten the result and swap it with I+1.
  e|       e#   Logical OR; if `s' pushed an empty string, replace it with I+1.    
}%         e#
ew         e# Push the overlapping slices of length "lines".
{          e# Find; for each slice:
  :s       e#   Cast its elements to string (e.g., 1 -> "1").
  Q1$      e#   Push the input and a copy of the slice.
  A,s      e#   Push "0123456789".
  ff&      e#   Intersect the slice's strings' characters with that string.
  :s       e#   Cast the results to string. This replaces "fizz", "buzz"
           e#   and "fizzbuzz" with empty strings.
  .e|      e#   Vectorized logical OR; replace empty lines of the input
           e#   with the corresponding elements of the slice.
  =        e#   Check the original slice and the modified input for equality.
}=         e# Push the first match or nothing.
           e# We now have ["zzubzzif"] and possibly a solution on the stack.
N*         e# Join the topmost stack item, separating by linefeeds.         
o          e# Print the result.
;          e# Discard the remaining stack item, if any.

1

Perl,140个字节

$f=fizz;$b=buzz;@a=<>;@x=map{s!\d!!gr.$/}@s=map{$_%15?$_%3?$_%5?$_:$b:$f:$f.$b}(++$c..$c+$#a)while$c<15&&"@x"ne"@a";print$c<15?"@s":zzubzzif

说明

  1. @a 是输入线的数组
  2. while循环内部
  3. @s 具有生成的嘶嘶声序列
  4. @x与相同,@s但用空字符串替换数字,并在每个元素上添加新行(以与匹配@a
  5. $c 是从1到15的计数器
  6. 循环运行直到@x变得与输入相同@a
  7. 在循环外部,@s根据是否$c在极限范围内,输出为或zzufzzib

1

Python,176字节

可能会做的更好,但首先尝试高尔夫。提示赞赏:)

压缩

def f(a):l=len(a);g=lambda n:'fizz'*(n%3<1)+'buzz'*(n%5<1);r=range;return next(([g(x)or x for x in r(i%15,i%15+l)]for i in r(1,16)if all(g(i+x)==a[x]for x in r(l))),g(0)[::-1])

原版的

def f(a):
  l = len(a)
  g = lambda n: 'fizz'*(n%3<1)+'buzz'*(n%5<1)
  r = range
  return next(
    (
      [g(x) or x for x in r(i % 15,i % 15 + l)]
      for i in r(1,16)
      if all(
        g(i + x) == a[x] for x in r(l)
      )
    ),
    g(0)[::-1]
  )

欢迎来到PPCG!标准格式指导原则是在标题的开头加标题,以便读者知道该语言和页首横幅摘要可以正确解析您的答案。例如,#Java, 205 Bytes作为答案的第一行。
AdmBorkBork 2015年

1
尝试用以下命令替换lambda:'fizz'*(n%3<1)+'buzz'*(n%5<1)然后可以删除f和b分配。(f+b)[::-1]然后可以调用已知的“ fizzbuzz”,例如g(15)[::-1]
Todd

谢谢你的建议!降至183!我认为g(0)也可以保存另一个字节:)
arcyqwerty 2015年

1
python 2允许在没有歧义的情况下在关键字之前删除一些空格:例如..] for 可以写成..]for。应用这个技巧,您将获得177个字符
节食者2015年

您能否包括下一次编写代码时假定的输入。例如,表单的列表 ['','fizz','']如果比较输入列表与列表comp而不是使用all(),还有一些其他要删除的内容if a==[g(i + x) for x in r(l)]
Todd 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.