Prosty FizzBuzz wykorzystujący ciągi znaków.
Dany
- 1 słowo lub fraza (ciąg)
- 2 unikalne postacie
Wynik
Słowo lub fraza z każdym wystąpieniem pierwszego znaku zastępowanego przez fizz i każdego drugiego znaku zastępowanego przez szum
Zasady
- Pierwsza litera zarówno w Fizz, jak i Buzz musi pozostać wielka
- W przypadku pozostałych wyrazów „fizz” i „brzęczenie” musisz dopasować wielkość liter zamienionego znaku (jeśli nie ma, to małymi literami)
- Jeśli podane znaki nie znajdują się w frazie, wypisz oryginalną frazę
Przypadki testowe
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
To jest code-golf, więc wygrywa najkrótszy kod w bajtach!
Uwaga
Techniczne podejście do sprawy nowej linii (To jest SPARTA!) Jest częścią wyzwania. Nie unieważnię jednak odpowiedzi za jej nieuwzględnienie, ponieważ w niektórych językach jest to bardzo trudne lub wręcz niemożliwe.
you must match the case of the replaced character
iInput is not case sensitive
wydają się zaprzeczać sobie nawzajem.ThiBuzzFizzIBuzzFizzBUZZPARTA
brakuje ostatniego wykrzyknika.aa, a, f
? Jaka byłaby oczekiwana wydajność?BUZZizzBUZZizz
,FizzFizz
czy oba są dozwolone według naszej opcji?FizzFizz
.Odpowiedzi:
Galaretka , 34 bajty
Wypróbuj online!
W jaki sposób?
źródło
Python 3 ,
180174168160 160bajtówThis is just a more golfed version of Stephen's answer, in Python 3. This chips away 42% of his bytes. Python 2 would save one byte on the print, but such is the price of progress. This handles newlines properly.
Thanks to Blckknight for saving 8 bytes on input.
źródło
fileinput
modules used for?from sys import*
ands,a,b=J(stdin).split(', ')
to save a few characters?Python, 109 bytes
Try it online!
Takes the two characters as a single string
Edit: Added testcase to TIO link, newline works too
źródło
Python 2,
271, 261 bytesTry it online!
Wow this one was a doozie! It turns out python won't accept multi-line inputs so
fileinput
must be used.edit: should pass all cases now :)
źródło
import fileinput as f
...for m in f.input():
MATLAB/Octave,
106102111 bytesThis could probably be optimised further.
It uses a simple Regex replacement. However an intermediate step is required by replacing the input characters with numbers first. This is so that if the second input replace letter was contained in
Fizz
that theFizz
doesn't then get replaced when the next regex is performed.This of course assumes there are no numbers in the input. However given the question says the input is a word or phrase I feel that this is an acceptable assumption.
The code will handle new lines in the input correctly.
You can Try it online!
źródło
Bash 4.4 + GNU sed,
70228222227 bytesApparently
alias e=echo
throws an error if referenced in Bash 4.3 or below, the version TIO is apparently using. Therefore, the longer and equivalent Bash 4.3 code is given in the below TIO test suite for the sake of testing. This passes all of the test cases, so that is nice.Try it online!
źródło
JavaScript (ES6), 92 bytes
Takes input as a string and an array of two characters. Supports newlines.
Test cases
Show code snippet
źródło
Pyth - 25 bytes
Test Suite.
źródło
"Hl", Hello
does not capitalise the output correctly and"hL", Hello
does not substitute thel
s.GNU sed, 135 + 1(r flag) = 136 bytes
By default, a sed script is executed as many times as there are input lines. To handle multi-line input, I use a loop to append all possible remaining lines to the first, without starting a new cycle.
Try it online!
The replacement table used on line 4, needs to be in that exact order, i.e. 'Fizz' and 'Buzz' after their upper-case forms. This is because the sed regex
.*
, used during the table lookup, is greedy. If the current char needed to be replaced is not a letter (no case), then the lowercase string is needed (matched last).Since sed has no data types, I use a character delimiter to iterate a string. It will mark my current position and in a loop I shift it from left to right. Fortunately, I can use
,
for this, since it is the input data delimiter.Explanation:
źródło
Haskell, 114 bytes
r
takes the fizz and buzz characters as a 2 element list as the first argument, and the input string as the second argument. Newlines and unicode should be handled appropriately, although the function is unfortunately not total (allowing for invalid inputs saved 5 bytes).źródło
u=Data.Char.toUpper
only works in ghci as far as I know. In standard Haskell you need theimport
. However you should be able to save some bytes by using=<<
instead ofconcatMap
.Mathematica, 94 bytes
Anonymous function. Takes two strings as input and returns a function which takes a string as input and returns a string as output as output. It must be called in the format
prog["c1", "c2"]["s"]
, where"s"
is the target string and"c1"
and"c2"
are the two characters. Could probably be golfed further.źródło