Retired
Filling an array (part 1) (retired)
14,820 of 27,531RobinKnipe
Loading description...
Arrays
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.
I think this should be 7kyu.
This comment has been hidden.
Anyway, this kata has been retired
same problem here, new learner so feeling stupid
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/162.
Please join the discussion to help us identify duplicate kata and retire them.
Yes, this would be good to identify any duplicate kata. My next challenge is the Sudoku solver. As you can see here, I already knew how to do what was done. Any new programmers please be very careful here, make sure to watch these "deplicate" kata very closely, especially the higher ranked kata. They are very tough to spot, but once you know where they are hiding, then report them right away, they are very sneaky.
Kata already retired as duplicate.
The function is incorrectly typed out of the gate on TypeScript. It spits out this error, independently of whatever I try to do in the function:
TSError: ⨯ Unable to compile TypeScript: test.ts:6:12 - error TS2554: Expected 1 arguments, but got 0.
6 expect(arr()).to.be.an('array'); ~~~~~
solution.ts:1:21 1 export const arr = (n: number[]): number[] => { ~~~~~~~~~~~ An argument for 'n' was not provided. test.ts:10:12 - error TS2554: Expected 1 arguments, but got 0.
10 expect(arr()).to.deep.equal([]); ~~~~~
solution.ts:1:21 1 export const arr = (n: number[]): number[] => { ~~~~~~~~~~~ An argument for 'n' was not provided. test.ts:14:16 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'number[]'.
14 expect(arr(4)).to.deep.equal([0,1,2,3]);
You missed this part:
There is even a corresponding sample test case:
expect(arr()).to.deep.equal([]);
You're right hobovsky, didn't pay attention to that. Thank you!
ArgumentCountError : Too few arguments to function arr(), 0 passed in /workspace/default/tests/ExampleTest.php on line 10 and exactly 1 expected Edit: this is in the PHP version
Your code should handle that.
Disregard, this is clearly not python. Facepalm
NASM translation lacks completely specifications (input, output, size of the registers, etc.), see the post just below. Moreover it is not possible to have optional parameter in this language, so the kata requirements cannot be respected. This translation should not have been accepted.
how does that work in NASM ?
It doesn't.
feel free to open it as an issue then. either description / setup comments should be updated, or the NASM version should be retired (dunno if it's possible)
AFAIK it is not possible to retire a concrete language once it has been approved. So yes, this is an issue, and probably this kata is a mere duplicate of some other kata that doesn't require to set a default parameter. NASM version lacks the needed specification, which register holds the input, what is the register(s) controlled for output, how the array has to be allocated... I guess the size should be stored somewhere too (like in C)...
Scala translation
Approved by someone
D translation
Approved!
Excelennt for beginners! Thanks
This comment has been hidden.
We don't know which language you are using, we don't know what you are doing and how. Probably you don't catch it where it is needed. Please be more specific if you really want to be helped.
My bad. Sorry.
TypeError: missing 1 required positional arguments
I keep running into this error for one basic test case but the rest work perfectly fine. I'm running it in Python
That's your code's problem, not a kata issue.
but after that will not go for other tests
This comment has been hidden.
Translation for Python with fixing the issue below
Approved!
This comment has been hidden.
Got the right outputs, whats wrong with my code? def arr(n): if n > 0: a = list(range(0,n)) print(a) elif n == 0: a = list(0) print(a) else: a = list() print(a)
You shouldn't print the result, you should return it
This comment has been hidden.
I agree with that; I've made a fork with fixing that problem
fixed
This is a valuable lesson in default arguments. Thanks.
I suggest "N.B. The parameter is optional…" This solves the grammar problem of a capital letter after a colon "Note: The parameter…"
Wouldn't the kata read better without the first paragraph? Especially since the arrays in two of the test examples are actually empty…
I'm not completely convinced by the title that this kata is really about filling an array, again especially because of the empty array cases.
This comment has been hidden.
Consider looking up "default arguments". Cheers. You could also check other questions. Naturally you aren't the only one fooled by that problem!
Coffeescript translation
Crystal translation
Julia translation
Julia and CS approved. See remark on translation page for Crystal.
This comment has been hidden.
Please take a look at other already existing threads before starting a new one
Hi. You can add a default value for the parameter in the function definition. Example:
def arr(n=0)
. What that's doing is basically saying "n is equal to the value passed when calling the function. But, if no value is passed, then use 0 instead".For Clojure the title is a little misleading. Its asking for a list, not an array. Though granted you're covering multiple languages so its hatrd to catch them all
got below exception on python 3.6, is there something i missed?
Traceback (most recent call last): File "main.py", line 2, in import codewars_test as test ModuleNotFoundError: No module named 'codewars_test'
Change language version to 3.8
This comment has been hidden.
Swift translation kumited. Plase review and approve :)
Ruby translation kumited :) please review and approve ~~
Approved.
This comment has been hidden.
arr() missing 1 ..."n"
give me a little hint of a solution.
I try "try/except" however it doesnt work.
Have no idea how to resolve.
It is not at all clear what is exactly your question, but there is no issue with the kata.
TypeError: arr() missing 'n'
You need to handle this test case, where there are no arguments passed in:
test.assert_equals(arr(), [])
you can alter the method's n parameter to get it to work. I looked up how to check if there is no argument passed in a function and you can use the keyword "None" to check if arguments were passed. Hope that helps without giving too much of an answer away.
I had this problem too.. some time you should watch the topic "Default arguments in Python"
ArgumentCountError : Too few arguments to function arr(), 0 passed in /workspace/default/tests/ExampleTest.php on line 5 and exactly 1 expected
Does Kobayashi Maru ring a bell for you? Sometimes, you need to decide it you want to accept the defaults ;-)
Pyhton question and i still a newbie:
def arr(n): # [ the numbers 0 to N-1 ] array=[] if n==None: array=[] else: for m in range(n): array.append(m) return array
TypeError: arr() missing 1 required positional argument: 'n' i don't not how to solve it since the console ask for a instruction for 'no n given'
Check the posts below, it's been asked and answered before (some posts are flagged as spoilers and you'll not be able to see them, but there are some others that are visible to you, both question and answer). I could give you the direct answer, but by doing what I suggested you'll not only solve your problem with this kata, it'll help you in other katas too, and we'll avoid answering the same each time the question pops up.
It seems you solved it, don't forget to close the question, cheers.
clojure translation for review:
https://www.codewars.com/kumite/5fb5c5965e7a7c0012d76c21?sel=5fb5c5965e7a7c0012d76c21
def arr(n): x = [] if n == 0: return [] elif n is None: return [] else: for i in range(n): x.append(i) return x ''' Traceback (most recent call last): File "/workspace/default/src/codewars-test/codewars_test/test_framework.py", line 111, in wrapper func() File "tests.py", line 7, in basic_tests test.assert_equals(arr(), []) TypeError: arr() missing 1 required positional argument: 'n'
''' I don't understand the error with my code above.
You need to handle the case when no argument is given.
doesn't "elif n is None:" cover that tho?
This comment has been hidden.
Good morning, go following error when trying to solve it in python: Traceback (most recent call last): File "/workspace/default/src/codewars-test/codewars_test/test_framework.py", line 111, in wrapper func() File "tests.py", line 7, in basic_tests test.assert_equals(arr(), []) TypeError: arr() missing 1 required positional argument: 'n' Seems like the test has an error - as i understand it is not solveable by a non moderator. Thank you for you help! Best regards, Leon
Not an issue, check default arguments.
Python question: def arr(n): list=[] i = 0 if not n: return ([]) else: while i < n: list.append(i) i = i+1 return (list)
any suggestions on why im getting an error: TypeError: arr() missing 1 required positional argument: 'n' Could be a simple mistake, as I'm new to Python
Check some of the older comments below this question. Lots of people ran into the same problem with their code that you are having. Read the older comments and you should be able to find the solution.
'Looks like you've solved it'
This comment has been hidden.
Doesn't states that you can modify the signature of the method, anyway, great Kata!
"Too few arguments to function arr(), 0 passed in /workspace/default/tests/ExampleTest.php on line 10 and exactly 1 expected"
Error in tests, cant complete it;
Duplicate issue, see jonesmat's post below. And you can still solve the problem (if your code handle that case):
Unexpected exception raised I think this is an error in the excercise
If you are not sure, you should not raise an
issue
. The kata runs fine in your language (python), and please note it has over 2K solutions. If you can prove it has anissue
, provide the test case, expected output, your output, and why the problem arises.It is indeed an issue. It should be fixed. The error comes from the test, not your code. You can modify your code to accomodate, but it really should be fixed in the kata's tests.
If in the function definition you write "def arr(n=0):" instead of "def arr(n):" then it will run just fine.
But this is clearly out of the scope of the exercise, as one should not have to modify the function that's given to one in such exercises...
This comment has been hidden.
Not a kata issue, the function's name in javascript should be
arr
.Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
Thanks !!
This comment has been hidden.
Did u return the array? Printing the array doesn't do anything ;)
remove "print(f"{m})" and replace it with "return m"
This comment has been hidden.
Handling that is part of the task, as explained in the comments below.
This comment has been hidden.
If you read the error logs on the left, you'll see the error:
TypeError: arr() missing 1 required positional argument: 'n'
This means your code needs to handle cases when no argument is provided.
yeah i saw that, what i cant understand is how my else case doesnt execute the nul array when no params are given. thanks for your input
Since this is 8kyu, I think I can give some hints. Try searching the web for 'default arguments'. I hope that helps :D
lol thanks a bunch. I got it now
This comment has been hidden.
This comment has been hidden.
N is provided as a positive number so you dont have to worry about it
This comment has been hidden.
While it's true that description does not mention it, sample tests seem to suggest that call without parameters should also be valid. Your solution should handle invocation in form
array = arr()
correctly (yes, it can be done).See this post: https://www.codewars.com/kata/571d42206414b103dc0006a1/discuss#5f4c6ccacecf9e002fb12fee for some more details.
This comment has been hidden.
You changed the name of the function. It should be called
arr
, you changed it tocreateArray
, and usedarr
as a name for something else.Oh I'm sorry I didn't realise we where bound to the names. Thanks so much for the quick reply. It passed the test now :)
The test "arr()" fails due to not passing a required parameter into the arr(n) function. Please remove this test or indicate in the test description that the parameter should be optional (this is an 8kyu level test and is going to cause beginners issues).
Updated the description as most languages have such test.
This comment has been hidden.
Your function name is wrong.
(Also not a issue with the problem)
Thank you
This comment has been hidden.
Tests are fine, problem is with your code.
Description lacks the requirement of function being callable without params though. This needs to be added, just sample test is not enough.
What is the point of this test: test.assert_equals(arr(), []) ??????
I can't change main idea. Only translate. Probably this test show your understanding default arguments.
If you want to include testing for default arguments, or optional arguments, that's fine; but in that case it might be a good idea to mention this in the instructions...
PHP translation is ready! Please check and approve. Also approve python translation with new framework above
Below you mean. Whoops, closed on accident. Author has to approve it.
Anyone able to approve this new python translation with the new test framework included?
It's already been approved.
test.assert_equals(arr(), []) I don't understand the above. I get an error that says
"Traceback (most recent call last): File "tests.py", line 6, in test.assert_equals(arr(), []) TypeError: arr() missing 1 required positional argument: 'n'"
How am i suppose to write an condition for a case that has no argument? Is there a way in python to access variables that have no values? Thanks
This comment has been hidden.
This comment has been hidden.
@voodoonissen What error message does your compiler return when you use an optional argument? I may be mistaken, but I'm 99% sure a
TypeError
is only thrown when a function or operator is used in a way it isn't designed for. The above test given by @valkyrie testsarr()
, with no arguments. Sincearr()
is not designed for this, aTypeError
is thrown. But with an optional argument, it is defined for this type of call. So it does not throw aTypeError
. Where are you saying I'm making a mistake?Another thing is, calling
arr()
is equivalent to callingarr(None)
(correct me if I'm wrong), which is why your solution works. However, if you wanted to setn
to some value other than zero as a default, the code would be a lot more complicated. So, in my opinion, using a default or optional argument is best practice in general.Python version should use the new test framework, and import it together with solution explicitly.
https://www.codewars.com/kumite/5f478b4918330d0024171dd1?sel=5f47fee11d026a000f1af759 -> Approve this plz with new test framework.
updated to 3.8, so closing ~~
Python translation is ready! Please check and approve
Cheers, they've been approved!
@RobinKnipe why did you ignore the comment on the translation and approve it anyway?
I will use new framework. Like you recommend
TypeScript translation is ready! Please check and approve
nice kata, thx author.
This comment has been hidden.
Hi. The initial solution shows uses an array function, but you are free to define the function anyway you like. For exxample, all of the following declarations of
arr
are equivalent for the purposes of this kata:You can use whichever of the above styles you feel most comfortable with. To clear up the confusion about the return, in an arrow function (or lambda), the return is just whatever follows the arrow. So in the example solution, the return is a blank array
[]
. Here are some more examples:I hope that stirs you in the right direction.
Thank you. My problem was that I tried to get value of N, and it was 'undefined'. However in the fist test it is really undefined. I didn't expect it.
Ah yes I see what you mean. That's a fairly common test case. Don't forget you can always perform some debugging by logging the input:
If you're ever not sure what's happening in a test, that can help!
Hi, I made a C# Translation for this Kata, please review and approve.
.
I would consider adding an example in the instructions section.
Example added - thanks for your feedback!
Resolved
More Katas with arrays! =)
OK then! Try part 2...
http://www.codewars.com/kata/filling-an-array-part-2
Hasn't a kata like this been created before?
Naturally.
yupp
The description for the third sample test is incorrect:
it('should return 0 to 9 when called with 10',_=>Test.assertSimilar(arr(4), [0,1,2,3]));
Thanks for your feedback Matt - good spot!