A,Buzz,C,D,E,Fizz,G


14

一个使用字符串的简单FizzBu​​zz。

给定

  • 1个字或词组(字符串)
  • 2个独特的角色

输出量

每个出现的第一个字符的单词或短语都用嘶嘶声代替,而第二个字符的每个单词或短语都以嗡嗡声代替

规则

  • Fizz和Buzz中的首字母必须保持大写
  • 对于其余的嘶嘶声和嗡嗡声,您必须匹配被替换字符的大小写(如果没有大小写,则保持小写)
  • 如果短语中没有给定的字符,则输出原始短语

测试用例

Given: Hello, h, l
Output: FIZZeBuzzBuzzo

Given: test, a, b
Output: test

Given: PCG rocks!,  , !
PCGFizzrocksBuzz

Given: This
Is
SPARTA!, 
, S
Output: ThiBuzzFizzIBuzzFizzBUZZPARTA!

Given: FizzBuzz, a, b
Output: FizzBUZZuzz

这是代码高尔夫球,因此以字节为单位的最短代码胜出!

注意

从技术上讲,处理换行案例(这是SPARTA!)是挑战的一部分。但是,我不会因为没有包含答案而作废,因为在某些语言中这是非常具有挑战性甚至是不可能的。


2
you must match the case of the replaced character并且Input is not case sensitive似乎相互矛盾。
格雷格·马丁

@GregMartin啊,谢谢,我试图说输入的2个字符区分大小写(例如A表示与a相同)
GracefulLemming

1
ThiBuzzFizzIBuzzFizzBUZZPARTA缺少最后的感叹号。
Arnauld

我们应该支持诸如此类的输入aa, a, f吗?预期的输出是多少?BUZZizzBUZZizzFizzFizz或两者都可以选择?
Arnauld

@Amauld基于最后一个测试用例,我想说它应该输出FizzFizz
基金莫妮卡的诉讼

Answers:


2

果冻,34 个字节

Œl=€⁹Œl¤ȧ€"“¡Ṭ4“Ụp»o"/ȯ"Œu⁹Œln$T¤¦

在线尝试!

怎么样?

Œl=€⁹Œl¤ȧ€"“¡Ṭ4“Ụp»o"/ȯ"Œu⁹Œln$T¤¦ - Main link: characters, string
Œl                                 - lowercase the characters
       ¤                           - nilad followed by link(s) as a nilad:
    ⁹                              -     right argument, the string
     Œl                            -     lowercase
  =€                               - equals (vectorises) for €ach (a list of 2 lists that identify the indexes of the string matching the characters regardless of case)
           “¡Ṭ4“Ụp»                - dictionary strings ["Fizz", "Buzz"]
          "                        - zip with
        ȧ€                         -     logical and (non-vectorising) for €ach (replace the 1s with the words)
                     /             - reduce with:
                    "              -     zip with:
                   o               -         logical or (vectorises) (make one list of zeros and the words)
                                   - implicit right argument, string
                       "           - zip with:
                      ȯ            -     logical or (non-vectorising) (replace the zeros with the original characters from the string)
                                 ¦ - apply...
                        Œu         -     uppercase
                                   - ...to the indexes (the words at indexes):
                                ¤  -     nilad followed by link(s) as a nilad:
                          ⁹        -         right argument, the string
                              $    -         last two links as a monad (i.e. the string on both sides):
                           Œl      -             lowercase
                             n     -             not equals (vectorises)
                               T   -         truthy indexes (the indexes of the capital letters in the string)

+1,感谢您使用换行符。这使得这是第一个完全正确的提交方式
GracefulLemming

6

Python 3中180个 174 168 160 152字节

from sys import*
J=''.join
L=str.lower
s,a,b=J(stdin).split(', ')
print(J('FBFBiuIUzzZZzzZZ'[L(k)==L(b)::2][k!=L(k)::2]*(L(k)in L(a+b))or k for k in s))

这只是Python 3 中Stephen回答的更高版本。这浪费了他42%的字节。Python 2可以在打印上节省一个字节,但这是进步的代价。这样可以正确处理换行符。

感谢Blckknight在输入上节省了8个字节。


1
这些fileinput模块的作用是什么?
caird coinheringaahing

@ ValyrioAccount,fileinput.input将覆盖内置输入法。它可以使用换行符输入。
本·弗兰克尔

2
怎么样from sys import*s,a,b=J(stdin).split(', ')保存一些字符?
Blckknght

4

Python,109个字节

lambda s,w:"".join([c,"Fizz","Buzz","BUZZ","FIZZ"][-~w.lower().find(c.lower())*-~(-2*c.isupper())]for c in s)

在线尝试!


将两个字符作为单个字符串

编辑:将测试用例添加到TIO链接,换行符也可以


2

Python 2中271,261个字节

import fileinput as f
a=''
for m in f.input():a+=m
a=a.split(', ')
l=L=list(a[0])
for i in range(0,len(a[0])):
 j,k=l[i].lower(),l[i].istitle()
 if j==a[1].lower():
  L[i]='FIZZ'if k else'Fizz'
 elif j==a[2].lower():
  L[i]='BUZZ'if k else'Buzz'
print''.join(L)

在线尝试!

哇,这真是个笨蛋!事实证明python不会接受多行输入,因此fileinput必须使用。

编辑:现在应该通过所有情况:)


用换行符来肯定+1来处理案件!没有多少人能够合并这一点
GracefulLemming

您可以通过执行以下操作来节省3个字节import fileinput as f...for m in f.input():
GracefulLemming

@Caleb感谢您的建议!现在应该可以通过所有测试了
斯蒂芬

2

MATLAB / 倍频程106个 102 111字节

@(a,b,c)regexprep(a,num2cell([lower([b c]) upper([b c]) '1234']),{'2','4','1','3','FIZZ','Fizz','BUZZ','Buzz'})

这可能可以进一步优化。

它使用简单的Regex替换。但是,需要一个中间步骤,首先将输入字符替换为数字。这样,如果包含第二个输入替换字母FizzFizz则在执行下一个正则表达式时不会替换。

当然,这假设输入中没有数字。但是考虑到问题是输入是单词还是短语,我认为这是可以接受的假设。

该代码将正确处理输入中的新行。

您可以在线尝试!


2

击4.4 + GNU sed的,70个 228 222 227字节

IFS=;alias e=echo;K=`sed $([[ $2 != '
' ]]&&e "s/${2,}/Fizz/g;s/${2^}/FIZZ/g"||:)$([[ $3 != '
' ]]&&e ";s/${3,}/Buzz/g;s/${3^}/BUZZ/g"||:)<<<"$1"`;[[ $2 = '
'||$3 = '
' ]]&&e ${K//$'\n'/`[[ $2 = '
' ]]&&e Fizz||e Buzz`}||e "$K"

alias e=echo如果在Bash 4.3或更低版本中引用,显然会引发错误,显然是TIO使用的版本。因此,为了进行测试,下面的TIO测试套件中给出了更长且等效的Bash 4.3代码。这通过了所有测试用例,所以很好。

在线尝试!


@thedarkwanderer现在应该了。抱歉,修复花了一些时间。
R. Kap's

2

JavaScript(ES6),92个字节

将输入作为字符串和两个字符组成的数组。支持换行符。

f=(s,[a,b],r='Fizz')=>a?f(s.replace(RegExp(a,'gi'),m=>m<'a'?r.toUpperCase():r),[b],'Buzz'):s

测试用例



1

GNU sed,135 + 1(r标志)= 136字节

默认情况下,sed脚本的执行次数与输入行数相同。为了处理多行输入,我使用循环将所有可能的其余行追加到第一行,而无需开始新的循环。

:r
$!N
$!br
s:, (.), (.):;\u\1FIZZ;\l\1Fizz;\u\2BUZZ;\l\2Buzz:
s:^:,:
:
s:,(.)(.*;\1)(...)(.):\3,\4\2\3\4:
s:,(.):\1,:
/,;.F/!t
s:,.*::

在线尝试!

第4行上使用的替换表必须按照确切的顺序排列,即大写形式的“ Fizz”和“ Buzz”。这是因为sed正则表达式.*在表查找过程中使用是贪婪的。如果当前需要替换的字符不是字母(无大小写),则需要小写字符串(最后匹配)。

由于sed没有数据类型,因此我使用字符定界符来迭代字符串。它将标记我的当前位置,并在循环中从左向右移动。幸运的是,我可以使用,它,因为它是输入数据定界符。

说明:

:r                                                 # reading loop
$!N                                                # append next input line
$!br                                               # repeat till EOF
s:, (.), (.):;\u\1FIZZ;\l\1Fizz;\u\2BUZZ;\l\2Buzz: # create replacement table
s:^:,:                                             # append my string delimiter
:                                                  # main loop
s:,(.)(.*;\1)(...)(.):\3,\4\2\3\4:                 # apply char replacement, if any
s:,(.):\1,:                                        # shift delimiter to right
/,;.F/!t                                           # repeat till end of string
s:,.*::                                            # print only the final string

0

Haskell,114个字节

u=Data.Char.toUpper
p[f,b]x|f==u x="Fizz"|b==u x="Buzz"|2>1=[x]
q x y|u y<y=x|2>1=u<$>x
r x=concatMap$q=<<p(u<$>x)

r将fizz和buzz字符作为2元素列表作为第一个参数,并将输入字符串作为第二个参数。换行符和unicode应该适当地处理,尽管不幸的是该功能不是合计的(允许无效输入保存5个字节)。


u=Data.Char.toUpper据我所知仅在ghci中有效。在标准的Haskell中,您需要import。但是,您应该可以使用=<<代替来保存一些字节concatMap
Laikoni '17

0

Mathematica,94个字节

a=ToLowerCase;b=ToUpperCase;StringReplace@{a@#->"Fizz",b@#->"FIZZ",a@#2->"Buzz",b@#2->"BUZZ"}&

匿名函数。将两个字符串作为输入并返回一个函数,该函数将一个字符串作为输入并返回一个字符串作为输出作为输出。必须以格式调用prog["c1", "c2"]["s"],其中"s"是目标字符串,"c1""c2"是两个字符。可能会打得更远。

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.