7 kyu
Find the nth Digit of a Number
4,491 of 12,610Goncalerta
Loading description...
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.
This comment has been deleted.
I am not sure how many edge cases can be there in a 7 kyu kata, but I know for sure that you could find some better words to express your dissatisfaction.
This comment has been deleted.
If you're having trouble solving this cata and you've come here, it's very likely that you missed this phrase from the description: 'counting from right to left' 😁
rust solution should return a
Lots of different ways in which this kata can be - and has been - solved. Cool! :)
I have passed all the tests cases but failed to attempt !
Hi and welcome to Codewars!
For future reference, please state which language you are working in - here, Swift it seems - so someone who knows that language can help you (I'm a Python noob so I can't look at the Swift tests).
In the mean time, please read this page on Troubleshooting - especially the part on printing the input which will allow you to see which test cases are causing you to fail:
https://docs.codewars.com/training/troubleshooting/#error-messages-and-printing-your-inputoutput
Maybe this will allow you to notice why your code is failing - large numbers, presence of 0, negative signs, etc??
Yes it's Swift. Thanks for writing this comment!
I solved it.
Aha nice one - good luck on your Swift journey!
This solution sometimes passes tests in JS, hence more tests should be added
added a fixed test with
nth = 1 && num > 0
, also bumped count of random tests from10
to100
I've managed to pass the test here in codewars but the attempt fails. Would there be someone willing to review my code?
sure
I forgot to mention what language I was using. I'm using Swift. Are you still able to review my code?
I was able to solve the issue I was having.
COBOL translation (author is inactive).
C translation (author gone)
Approved :)
I wonder how to do this, but from left to right?
This comment has been hidden.
Traceback (most recent call last): File "tests.py", line 3, in Test.describe("Basic tests") NameError: name 'Test' is not defined
This is what I get when i execute my code, is that my mistake or there is mistake in test cases?????????
For python
Switch to python version 3.8
its by default in version 3.8
still it shows that error
This comment has been hidden.
I think that there are some testcases for python that have been written wrongly. Does anyone else get this
No, it's a problem with your code, when in doubt, use Question label instead of Issue, also look at how many completions there already are for Python:
That will give you an idea if the problem is in the kata or in your code.
It's very interesting, why kata's author got 42 as "00042", but 5673 as "5673", not as "05673" - 1st example (findDigit(5673, 4) returns 5).
What do you think about it?
There is no difference between "5673" and "05673" for nth between 1 and 4.
Oh, sorry. Of course =)) I decided, that I should get 5th symbol, not 4th.
Mine is probably simplest to understand
Trying to do Find the nth Digit of a Number & the next to last example makes no sense given everything else about the description. Given that nth is defined in re a 1-based count, accessing the index of a number array, which by nature is 0-based, would seem to involved a zero-based count to i < nth. An value of nth < 0 yields "undefined," which is obviously not useful. 0 is not negative. Shouldn't the value returned of num = 65, nth = 0 be 5?
Please advise.
Re-ranked to 7 kyu
Not that this is that difficult, but seems like it should be a 7 kyu not 8
Change to:
In Rust
nth
is declared asusize
but negative numbers are passed.Yeah I'm unable to complete this because the tests won't even run.
Fixed. If you don't see the changes, click reset. It seems the problem was with the initial code, AurevoirXavier above proposed the fix.
We still have negative numbers I don't understand how is this fixed
Negative numbers are valid input, and if you see this inital code:
Instead of
It should work. Could you elaborate what's your problem?
Why findDigit(-456, 4) value is return 0?
The negative part is ignored and the number '4' has been declared. Since it says to count from right to left, we had to add a zero on the left side to accommodate for the nth value. So -456 become 0456. Therefore, 0 is the correct answer for the 4th position.
.
I've done about 5 Katas in my own IDE that run fine but everytime i try on this website i get EOF errors. What am i doing wrong
No one knows if you don't post your code. Since the user deleted their account, closing.
in Python quite some solutions would have a problem with the test below cause they ignor the case hiting the "-" at negative numbers pls add or corect me if I misunderstood the kata Test.assert_equals(find_digit(-5540, 5), 0)
Added test
findDigit(-456, 4)
to all languages (expecting 0).Hmmmmm, a simple mathematical solution seems best but everyone is playing around with conversions in Python.
And I also think this should be level 7. It is a bit more complicated than the usual level 8 kata.
Agreed. I see the same thing in C#. There are a lot of conversions in many of the solutions. However, as you pointed out there is a quite simple pure mathematical solution.
I think it should be 7 kyu, not 8.
This comment has been hidden.
For python there is not one solution accepted as best practices. So it seems a bit messy. But interesting nevertheless!
C# translation Kumited, please review and approve.
.
This comment has been hidden.
Rust Translation Kumited! Please Accept
Done
See issue with Rust above!
Not too difficult, but did remind me of some techniques I hadn't used in a while.
I would make it a bit more clear that a "0th" input counts as "nth not being positive", especially when a lot of languages tend to be 0-index based.
This seems a bit tricky for a 8 kyu, no?
This comment has been hidden.
this code does not pass the C++ tests anymore
This comment has been hidden.
Not a kata issue.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
The tests should exclude the
num
label. Change findDigit to:.
Not a kata issue.
CoffeeScript Translation Kumited! Please Accept :D
Done! :D
Thank you! :D
My code should pass there is no reason it should not. If anyone has passed this challange and would like to look at my solution please let me know
This comment has been hidden.
This comment has been hidden.
Oh great! I must have been getting a bit confused/over complicating it. It makes sense now! thanks
I think I am a bit confused by the test codes. The first one especially I am not sure it should be 5... shouldn't it be 3? Test.assertEquals(findDigit(5673, 4), 5); Test.assertEquals(findDigit(129, 2), 2); Test.assertEquals(findDigit(-2825, 3), 8);
I think the person who made the kata starts counting digits starting from the last digit. I have no idea why, I also found it confusing.
If that is the case, that changes it quite a bit! thank you!
I count the digits from right to left. I thought that was intuitive, because that is how numbers work. The first digit (units) appears at the most right, then you have at his left the ten's, and so on... When you add another digit, you add it at the left, not at the right (ex. 999+1 = 1000) so I think it makes sense to count them from right to left and not otherwise.
But do you think I should change the description to make this more clear?
Thanks for responding! I would probably add in a note about that as I normally do so from left to right.
Changed the 1st paragraph of the description to this:
The function
findDigit
takes two numbers as input,num
andnth
. It outputs the nth digit of num (counting from right to left).I hope it now becomes clearer. :)
I think you should use the words 'place value' in your description. First digit, nth digit are ambiguous. All you need to do is to add 'by the first digit we mean the digit with the lowest place value.'
Python translation submitted, thanks for your kata :)
Approved. :)
Ruby translation submitted, waiting for approval.
Approved.
This comment has been hidden.
Thank you for pointing that! Fixed.
Not working for me. My tests pass, I hit submit and get "Unknown error".
Ah, never mind, I was using Edge. Tried again from Chrome and it worked. (If you can't tell, I'm new around here...)
chrome or firefox work the best I believe.
Approved.
Had to re-publish it in order to avoid the publishing bug. The random tests should now be fixed. ;) More feedback will always be appreciated.
Much better, greenlit :D
This comment has been hidden.
ahhh my code does not pass it gives me an error after passing all tests. I am a beginner my code may not be very pythonic in nature but to pass this challenge I am sure it is flawless, IndexError out of range is what is being thrown
I too am having this issue. I pass all the test but get a STDERR about the index being out of range despite the numerous testing I did in XCode all came back positive. Not sure if this is a bug or if I am actually doing something wrong.
My code passes all the tests except the last also, with an IndexOutOfRangeException, which appears to be coming from the Sample Tests.