6 kyu
Custom FizzBuzz Array
699 of 3,664ogryzek
Loading description...
Arrays
Logic
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Missing fixed and random tests such as
num_1 = 6
&&num_2 = 10
inRuby which will invalidate such solutions
JS which will invalidate such solutions
thanks for this.. i still dont know why my solution is correct.. just followed your instructions.. :)
I'm doing the rust version and I really don't understand this.
As I understand it the 4 paramters to the function means the defaults should be overwritten.
So if the string_one argument is given it should be checked if it's a multiple of num_one (if parameter is given, otherwise 3). The same for string_two.
What I don't understand is how the argument in brackets is passed on to the function - and how to retrieve it in the function
I recently passed this in Rust so here's my attempt at an aswer.
The solution setup has given us a macro like such:
The tests call this macro. This macro prevides default values to function paramaters. So your function will allways get all four arguments. Code as if you always get valid values for
string_one
,string_two
,num_one
andnum_two
. The macro will handle giving the default values when the argument is missing.I don't know if any of that makes any sense, but I solved this by just coding the missing function as it is given.
🛄
Thank you for your reply.
I guess it's me who don't understand how macros work. If I look at the line from the description
when I look at the macro it calls my function with
what does the [80] then mean in this context? From looking at the expected return I see I'm not to return for all the numbers from 1 to 100, but rather the number after the bracketed one I have no clue how/where I'm to find this parameter.
The output of your function is
Vec<String>
, one can index a vector like this:so
fizz_buzz_custom("What's ", "up?", 3, 7)[80]
computes the whole vector, but indexes it with 80, so the 81th element of the resulting vector is retrieved.aha - ok, thanks :-)
I think this kata has a problem. In Javascript, I pass the first two custom tests but the third says Expected: "FizzBuzz", Instead: 15. But according to my code and the output, it gives "FizzBuzz". I think the test is having an issue.
There is no problem in that test. Read this
You are right. I had to read troubleshoot document and reread instructions. I didn't see the words "Fizz", "Buzz", 3, and 5 by default!
This comment has been hidden.
Don't use global variables.
This worked perfectly, thank you! Could you elaborate on why I shouldn't use global variables?
They keep their values between tests for instance.
This comment has been hidden.
Python:
initial test cases work fine but random tests don't work even though the expected and actual output match. I even compared both via the command line diff command and no differences, not sure why test cases are failing.
moving on as I've wasted a lot of time on this exercise trying to get random tests to work that look to be correct.
Hi,
Not an issue, everything works fine. There is a problem with your code. Generic message below, about what is cosidered an issue on CW:
Seems you're "rather new" to cw, so here are some general guidelines about the comments:
Issue
: problem in the kata itself (description, wrong tests, wrong internal solution...)Suggestions
: well, I guess that part is clearQuestion
anything else that is related to you having a problem solving a kata -> that's you, currently.When you post issues:
When you post a question: well, most of the above apply too x)
When you post code, use proper github markdown, so that it's readable.
And for your current problem, did you check what are the inputs/outputs to try to debug your code on your own? And are you sure you're returning the answer and not just printing it to the console?
cheers
Yes I've done a lot of testing with my own code and with test data (my own and kata provided)
output below for TEST and ATTEMPT
It would be better if you post your code because I passed the kata in Python without problems, check the link in Blind4Basics's post about markdown and mark your post as having spoiler content.
This comment has been hidden.
@bbirkinbine: you should not post solutions on publicly available repos, it's forbidden :/
Wasn't aware, changed that repo to private.
This is terribly confusing. Are we supposed to include cases for any number of inputs? Say the function call was fizz_buzz_custom('Hey', 8). Then is string_two still "buzz" and num_two still 5?
I'm not sure what is confusing about it. If the first two arguments have default values of
"Fizz"
and"Buzz"
then passing in"Hey"
and8
would replace them. This will work for weakly typed languages, but not for strongly typed ones.It will also be different if you are using named parameters versus not, which the original kata languages did not.
To answer your question though, "Say the function call was
fizz_buzz_custom('Hey', 8)
. Then is string_two still "buzz" and num_two still 5?" the kata description states:So, order matters... at least with the languages the kata originally supported.
Add edge case e.g.
n1 = 3, n2 = 6
No sample tests in JS.
I originally left out sample tests, as I felt you should be writing your own, if you want them.
Thoughts?
That's never a good idea for regular kata. It's gonna cause newcomers to raise issues where they could be easily avoided. All other languages so far seem to have sample tests too, making JS an outlier. I'm not sure if sample tests is a requirement (formally), but nowadays you won't see a kata get approved without them.
Python:
since the random test process (eg. testing the whole returned result) isn't the same than the one for the fixed tests (eg testing one item from the result), a discrepency from the description arises:
so, I went on, returning a tuple, but it ofc fails the random tests while it's perfectly valid according to the description.
=> either add a note somewhere, or change the way the random tests are testing the results, or at the very least, add some fixed tests (both in the test suite and the sample tests) testing an actual list, to show that it's not "just" a sequence that is expected.
Thanks. I added to the description for the python version: The function must return the secuence in the form of a python list.
Python translation
Hi,
see above, plz.
Ruby does not have random tests.
Missing sample tests
Fixed in Ruby.
description >= confusing ? return 'pass'
How do we solve this problem, if we don't know how that array index is being passed into the function? It's not an argument. I guess I'm a little confused.
The function returns an array.
I've come up with instructions on this Kata that I feel are more clear.
Create a method that returns an array of 100 items.
The method can accept up to 4 arguments. The first two are strings and the last two are integers. The defaults are as follows (e.g. use these if values are not passed for an argument):
If the array index is divisible by the first integer argument (NumOne), push the first string (StringOne) to the array.
If the array index is divisible by the second integer argument (NumTwo), push the second string (StringTwo) to the array.
If the array index is divisible by both the first and second integer arguments, push the concatanated strings to the array.
If the array index is not divisible by either of the first and second integer arguments, push the index number to the array.
Not array index but values of array.
This kata is misleading in yet another way. The way that I usually approach these is to run the examples, and if the examples pass, then I submit an "attempt". But in this case, "Run examples" apparently always says that your code fails. This is my output:
Time: 417ms Passed: 0 Failed: 1
Since I couldn't figure out what was wrong with my code, I submitted anyway, and it passed.
Please read the docs in the detailed answer provided to your previous "gripe" with this kata. If your code submissions passes, you'll have a solution to view (which you currently do not).
There is nothing missing from the description, examples, or test cases that would prevent you from being able to complete this kata.
Best of luck.
I found the instructions for this challenge to be completely confusing. It wasn't at all clear what was being asked for. For example:
"Create a method/function that returns an array of numbers from 1 to 100."
Does that mean an array of length 100? Or does it mean an array whose elements are numbers from 1 to 100?
For another example, the instructions say: "If no arguments are passed, the value at the index of the array for the number should be 'Fizz' if it is divisible by 3, 'Buzz' if divisible by 5, 'FizzBuzz' if it divisible by both 3 and 5, or the number if it is not divisible by 3 or 5."
What does "the index of the array for the number" mean? I assume it just means that if x is the array, then x[i] should be equal to 'Fizz' if i is divisible by 3, etc. But the examples don't show that. It seems to show that if i+1 is divisible by 3, ...
For another example, the instructions don't actually say what is supposed to be done with the arguments. The instructions say: "If no arguments are passed, ...", but it doesn't say what to do with the arguments if they are passed. The instructions say "The first and second arguments are strings, which should be 'Fizz' and 'Buzz' by default. The third and fourth arguments are integers and and should be 3 and 5 by default." But there is no indication of how to use the arguments, so I'm not sure what it means to use defaults for the values.
Finally, the test that is performed when you click "Run Examples" doesn't actually do anything. So are we supposed to write our own test? I have no idea what the program is supposed to do, so how could I possibly write a test to see if it does it correctly?
Is this challenge supposed to be preparation for a job as a programmer, when you often have no idea what the customer actually wants?
When I first wrote this, I probably listened to more feedback than I should have.
Q: Does that mean an array of length 100? Or does it mean an array whose elements are numbers from 1 to 100? A: I believe this at one point said, "an array whose elements are numbers from 1 to 100."
Q: What does "the index of the array for the number" mean? I assume it just means that if x is the array, then x[i] should be equal to 'Fizz' if i is divisible by 3, etc. But the examples don't show that. It seems to show that if i+1 is divisible by 3, ... A: If the number in the returned array is divisible by n.
Q: I'm not sure what it means to use defaults for the values. A: The Mozilla Developer Network has some nice documents explaining default arguments (parameters)
Q: So are we supposed to write our own test? A: If you'd like to test, yes. There are docs for CodeWars testing.
Q: I have no idea what the program is supposed to do, so how could I possibly write a test to see if it does it correctly? A: All the required information is included in the description and the examples. I suggest focusing on the examples, as they explicitly state exactly what the program should do.
Try not to get frustrated. Perhaps try the original FizzBuzz kata first, then come back to this one.
Please let me know if you have any further questions.
But the instructions say that under certain circumstances, the value of the area should be the string "Fizz" or "Buzz" or "FizzBuzz". Those are not numbers from 1 to 100. My question about default values is that the instructions don't say what the arguments are used for. They only say what the function is supposed to do if there are no arguments.
Here's what I think you're asking for: fizzBuzzCustom(stringOne, stringTwo, numOne, numTwo) returns the result of starting with the array [1, 2, ..., 100], and replacing each number in the array by stringOne if it is divisible by numOne (but not divisible by numTwo), replacing it by stringTwo if it is divisible by numTwo (but not numOne), and replacing it by the concatenation of stringOne and stringTwo if it's divisible by both. If a number in the array is divisible by neither numOne nor numTwo, leave it unchanged.
If fewer than 4 arguments are supplied, use the default values of "Fizz" for stringOne, "Buzz" for stringTWo, 3 for numOne and 5 for numTwo.
I'm seeking clarification on the Instructions. • Should the array alement always be a number if the index is not divisible by 3 or 5? • When should only one string be included when two are passed as function arguments?
• Should the array alement always be a number if the index is not divisible by 3 or 5? Create a method/function that returns an array of numbers from 1 to 100.
• When should only one string be included when two are passed as function arguments? I'm not quite sure what you're asking. When should only one string be included where?
The last two sentences in the description explain the order of arguments and their default values: The first and second arguments are strings, which should be 'Fizz' and 'Buzz' by default. The third and fourth arguments are integers and and should be 3 and 5 by default.
Please let me know if you feel you need further clarification that isn't explicit in the definition and/or the examples.
If I'm understanding correctly, there would never just be an array of numbers. The array consists of numbers and strings. If the index is divisible by 3 or five, there will be a string. If it isn't, then there will be a number. The array will contain 100 items. Is my understanding correct?
I see now why I was confused on the string to use when strings are passed as arguments. In the instructions, an example test passes 'Hey' and 'There' as arguments. Then, coincidentally, the full suite test has a test that returns 'HeyThere'. Because we're not provided with the suite tests, I can only assume that 'HeyThere' was passed as the first string argument and that this was not a result of concatanating 'Hey' and 'There'. Is this correct?
Should update test description, "should return 'Fizz' at the index of a number divisible by 3 and 5". It should read, "should return 'FizzBuzz' at the index of a number divisible by 3 and 5".
Which language?
Javascript
Good catch. I don't seem to be able to edit the kata. Allow collaborators is checked though, so if you're able to, please feel free.
Super confused about what the two number parameters are supposed to do exactly. Can somebody please clarify what we're supposed to do with them? Thanks!
The numbers determine which numbers in the array returned have been replaced by the words, based on divisibility. If no arguments are given, the defaults are equivalent to
fizz_buzz_custom('Fizz', 'Buzz', 3, 5)
.In the examples given,
fizz_buzz_custom[44]
returns "FizzBuzz" because the 45th element of an array of numbers from 1 to 100 is 45, and 45 is divisible by both of the default numbers (3 and 5).If we pass in different arguments, for example
fizz_buzz_custom("What's ", "up?", 3, 7)[2]
, this should return the 3rd element in the array of 1 to 100, where numbers in that array which are divisible by 3 have been replaced by "What's ", numbers in that array which are divisible by 7 are replaced by "up?", and numbers which are divisible by both are replaced with "What's up?". Since the 3rd element in an array of 1 through 100 is 3, which is divisible by 3 (the 3rd argument), this will return"What's "
.Does that help to clarify?
That really helped me, Thanks!
Nice kata!
def fizz_buzz_custom(string_1 = "Fizz", string_2 = "Buzz", num_1 = 3, num_2 = 5) p number end
fizz_buzz_custom[28]
I'm having some trouble understanding this kata, how does one pass an argument outside of the method parentheses # fizz_buzz_custom[number from 1..100]?
Hi kparekh01,
The square-bracket notation is the index of the array that the
fizz_buzz_custom
method returns.So, for example, if I have an array,
letters = ["a", "b", "c"]
, I can callletters[1]
which will return"b"
. If I have a method calledgive_me_letters
that returns an array of letters, such as:Then, I can treat the method
give_me_letters
the same way, as it returns the same array. So,give_me_letters[1]
will return"b"
.This is unrelated to passing custom numbers into the method as arguments which are used for the range. I hope it helps to answer your question, without being a spoiler.
Gotchya, thanks for making it clear.
Spent 10 minutes trying to just understand the insructions. Not going to waste anymore time on this after seeing that everybody else is having the same problem.
Hi pgrennin,
The description did go through some changes early on, as per user feedback.
Would you care to offer a more descriptive criticism that may help us understand which part(s) of the description you are struggling with?
Oh hey, didn't know it was updated. I was unclear that the first and second string parameters are supposed to replace fizz and buzz.
It is possible to solve this without using neither Array nor even Hash. If it REALLY have to return an array there should be a test for it!
What a mess in the description!
glad i'm not the only one.
Pretty confusing description. It took me longer to understand the question than to solve it. I'll re-edit this post, and try to rephrase the description. This puzzle is fun but if we can't understand the instructions, it really ruins the fun.
I'm getting a "Test Didn't Pass: Unknown Error" whenever I try to run my tests. Even with blank tests, and not altering the starting code at all. Doesn't seem like that should happen. :/
What language?
Great kata idea, nice job!
Terrible instructions, I'm not sure what is required.
Ok, so it's just a bog standard fizzbuzz function. It's the examples that make it so confusing. I still can't get it to work - I'm getting 'Unknown error' I try to submit.
Do you have spaces between operators in your code? I kept getting "Unknown error" until I changed a bit of code from "i - 1" to "i-1". I have no idea why (Javascript quirk, Codewars quirk, or some other quirk).
same for me "Unknown error", Tests pass
This was a problem on our end that has now been fixed. Sorry for the inconvenience.
This comment has been hidden.
This should be fixed now. Sorry for the inconvenience.
It may be that I am really new to this, but I have NO idea what I am being asked to do. The grammar/punctuation makes it kind of incomprehensible. Can we get this re-worded? After attempting it one time and failing I read the failure errors to be able to deduce what is actually being asked.
I don't even know what this kata is asking me to do, Jesus! Can't you fix the instructions?
Description updated
This comment has been hidden.
LOL only now I realized this kata was much easier than I thought. I read the description so fast that I didn't notice the size constraint and ended up creating a "infinite array" solution.
Anybody did that too?
This kata seems clear and the test cases are well-described. Nice work!
Thank you! wthit56 gave some invaluable feedback which definitely helped clarify the description!
Cheers,
I still find it very confusing, but I won't skip it.
This comment has been hidden.
The description is pretty confusingly written here. There are no "replace"s necessary, and you only explain what you're actually meant to be doing right at the end. It would be easier to understand if you wrote everything in order, making everything as clear as possible and hilighting with
back-ticks
to show the names of arguments and such. This will really help the readability of this kata.A fun exercise, though, once you get your head around it.
Hi,
Thanks for the feeback!
I rewrote parts of the description. Let me know if that helps the readability or if it still needs some reworking.
Cheers
Saying
an array of FizzBuzz
and then describing it is still an odd way of putting things. You could just say "Write a function that returns an array. The values in the array should number from1
to100
. If the number is divisible by theaNumber
, the value should beaString
. Same forbNumber
andbString
. If the number is divisible by bothaNumber
andbNumber
, the value should beaString
, withbString
added onto the end." This adequately describes the expected result and how it should be constructed.Don't use terms like "a FizzBuzz"; unless you already know what that is, it only serves to confuse the reader.
Also, you could give the names of arguments as shown in the stub solution. This will help the coder understand which arguments go where. Also, you could change the names of these arguments to better describe what they represent.
aNumber
aString
bNumber
bString
might be a good option. Also, putting them in this order can make it easier to associate the two values for each term.You're getting there, though. ^^
Right! I was totally assuming that everyone would be familiar with FizzBuzz, but I suppose that won't always be the case.
I've made some changes based on your suggestions. Let me know what you think, and thanks so much for the great feedback.
Cheers,
Cool. That's much better.
I would tweak it to not use "Fizz" and "Buzz" in the explanation at all, though. Also, using 3 and 5 to describe the algorithm is confusing, as those values may not be used at all. Instead, describe the arguments, with their names, and use those to describe the functionality.
Also:
'Fizz' and 'Buzz' by default
isn't very clear. Say that the first two arguments, ("name of first argument" and "name of second argument") should be strings. If they are not provided, you should use "Fizz" and "Buzz" as their values, respectively.I've added an example to hopefully make it clearer that 'Fizz', 'Buzz', 3, and 5 are the intended default arguments.
Of course, I happen to know what I intended before even creating this so your feedback is invaluable. Do you think this example makes it explicit enough, or would rewording/additional explanation make this less ambiguous?
I think explicit would be better, I'm afraid. Also, because arrays use zero-based indices, the examples are going to be pretty hard to understand no matter what you do. Maybe have
fizzBuzzCustom[44] // (the number 45) returns 'FizzBuzz' (45 is divisible by 3 and 5)
or something?I really like this suggestion, and have added that to the examples.
I also tried rewording the description to make it more explicit.
Thanks again for all the great suggestions!
Let me know what you think.
Cheers,
Cool. I would also add the actual number that would have gone there, to be clear what is going on. But it's not completely necessary, I guess.
Also,
fizzBuzzCustom[15]
is still incorrect.I'd say you still need an arguments listing. Give us the names of the arguments; this will link up the values with the parameters in the reader's head.
Also,
should be Fizz and Buzz by default
is confusing. It implies that the argument values provided will be Fizz and Buzz, if nothing else. But this is something the coder needs to do, so explicitly say something like "ifaString
is not provided, you should use 'Fizz' instead", and so on.Description updated
This comment has been hidden.
Hi OverZealous,
Thank you for the feedback. I haven't been able to find this test for "Happy", "Cow", 13, 27. Maybe I am missing something?
This comment has been hidden.
Hi OverZealous,
Thank you! I was looking at the Ruby, and didn't see it at first. I actually had 5 and 3 hard coded in my solution, and somehow the other tests didn't fail!
Updated.
OK, it appears to be fixed.
Tests are passed now. I think the only thing left is to clean up the example given in the kata description:
Awesome!
Updated, thanks!
The last of the two-parameter tests needs a small tweak. As is,
Test Failed: expected: fizz_buzz_custom('lol', 'dude')[92] to return 'dude', instead got: lol
; 93 is only divisible by three, so the expected output should be 'lol'.Thanks again for the always excellent and helpful feedback mlabrum. I think we finally have this one ready.
Cheers,
Tests are just wrong...
I have exactly the same output as zishe.
Thanks for the feedback, I think I've fixed the problem. Please try again, and sorry for any frustration.
Cheers,
For the first failure message, the problem is the number associated with
fizz\_buzz\_custom[89]
is 90 (since we're counting from 1), which is divisible by 15. Therefore, the value offizz\_buzz\_custom[89]
should be "FizzBuzz" instead of "Buzz". I think the other incorrect failure messages are similar small errors in the expectations.I had a problem with my original solution where I had + instead of &&, which was giving me the wrong test cases, but I have since resolved this. I believe it should work now as expected.
I still got an error :
Test::Error: expected: fizz_buzz_custom('lol', 'dude')[92] to return 'dude', instead got: lol
93 is supposed to be divisible by 3, then the first word (lol) should be used right ?
Yes, you're absolutely right. My problem was in the solution I had something like 93 % (3 && 5) instead of 93 % (3 * 5). This should now be resolved.
Still wrong. 93 is not divible by 5, so only lol not loldude.
Maybe I didn't publish the change? Not sure, but I believe this should be resolved now. I've just re-published.
I just realized the problem. I had 5, 3 instead of 3, 5 in my original solution. Thank you very much for the feedback. I'll be more careful with my next Kata before publishing.
Thank you for the feedback :)
Nope, we gone from : Test::Error: expected: fizz_buzz_custom('lol', 'dude')[92] to return 'dude', instead got: lol to : Test::Error: expected: fizz_buzz_custom('lol', 'dude')[92] to return 'loldude', instead got: lol
Your test is still wrong.
Yes, I just fixed the test failure messages. I hope this finally works!
Sorry for any frustration and thank you so much for your feedback.
Cheers
Tests are horrible.
fizz_buzz_custom[89] to return 'Buzz'
- if it's 90 of course it multiples 15, and 89 not multiplies both 3 or 5.fizz_buzz_custom('lol', 'dude')[87] to return 'loldude'
- srsly?Thank you for the feedback,
Your test cases showed me I had some problems with my code, and I refactored.
Sorry for any frustration, and thanks so much.
Cheers,
Are the indices in the example correct? If FizzBuzz, by default, is an array counting from 1 to 100, it should be
fizz_buzz_custom[14]
instead offizz_buzz_custom[15]
that returns "Buzz", correct?Could the expected values be added to the failure messages? I'm getting about 2/3 of the test cases to pass, and since the output is the same for each of them, I'm not sure what's going wrong.
Yes, you're totally right!
I updated the tests to give more meaningful messages, and fixed the counter to start from 1 rather than 0.
Thanks so much for the feedback!