6 kyu
Calculate String Rotation
1,894 of 7,474obnounce
Loading description...
Strings
Arrays
Algorithms
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 felt pretty easy actually. key question to work out is what you're looping over, and how
In C, random tests failed once during cleanup after solution was passing, and passed again upon a retry:
Obviously both
0
and1
are correct, and I just got unlucky.edit: whenever the rotated random string is the same as the original, or when lengths are
0
or1
, then technically any rotation from0
toINT_MAX
(or infinity) is correct, and expected should then default to0
.edit: I mean any rotation whatsoever when there is only one repeated character as above, but a rotation by any multiple of
n
when the original and rotated strings are 2 or more repetitions of a sequence of lengthn
.the correct answer is
0
, of course. the random tests' logic was faulty. Thanks for the report, issue fixedNice!
Very interesting and challenging kata, compliments.
D translation
Go translation
C translation (author inactive)
Approved
Rust test case suggestion: Rust uses UTF-8 string encoding. Hence some characters could be longer than one byte.
Add a test case like:
assert_eq!(shifted_diff("daß", "ßda"), Some(1));
This is a 1 character shift but 2 byte shift because 1 character ß is two bytes in UTF-8: 0xC3,0x9F.
COBOL translation.
approved
JS: Node v14 should be used along with its appropiate assertion tools (Mocha + Chai). Refer to this and this
test.assert_equals(shifted_diff("hoop","pooh"), -1) I really don't understand why should the above test return '-1'..
All "pooh" rotations == [oohp, ohpo, hpoo, pooh]. So "hoop" != "pooh". Result: -1.
[JAVA] There should be a need of the user to check words that are different cases (case sensitivity). This is done for pyton but not for Java. thanks
It's not specified whether the rotation check should be case sensitive or not.
Fixed
Rust translation.
approved by someone
For the haskell version, I get a timeout on the sample tests, but it passes the actual tests fine and completes?
This comment has been hidden.
Op solved it closing ~~
Would be helpful if we could see all the tests.
You can if you completed the kata.
This comment has been hidden.
Swift translation kumited. Please review and approve.
Thanks.
All my tests are passing except (" "," ") (the last example test)is the only one coming back undefined. I don't understand why!
To me, that's actually rotating the first string BACKWARDS, not forward... But other than that, the exercise is nice.. :)
Yes, especially confusing considering in the example 'coffee' > 'eecoff' results in 2, but in the tests it expects 4 (going the opposite direction). I honestly feel like the description is asking us to go forward but the tests actually want us to go backward.
Is it possible to see what the test cases actually are ? I'm stuck on this seemingly easy kata and getting more and more frustrated about it. My algorithm works fine for almost all the cases but with each run some "new" weird test case pops up which is off the chain. Then I add more and more if statements to it to adjust it to this very case until the new one turns up at next run. The code is just getting overcomplicated with if's and debug outputs... (( Could you please specify exactly straight to the point in description what is "invalid rotation", which strings are not allowed etc. ?
<a href=https://www.codewars.com/kumite/585f1df56d36a5ee6e000268?sel=585f1df56d36a5ee6e000268> F# translation kumited
It isn't clear from the instructions that the rotations HAVE to be in the forward direction. If you rotate in the opposite direction, you can match in fewer rotations.
Fixed the wording, thanks.
some cases called shifted_diff, some others called shiftedDiff. please make them same.
It depends on which language to be used. Each language has its own way to name variables and functions.
I did with Python version of train. "Run Tests" calls shiftedDiff and some cases in "Submit" call shifted_diff Had to name two functions. shiftedDiff is called inside shifted_diff :)
Fixed this.
C#-Translation kumited!
https://www.codewars.com/kumite/579f7002cf1fa502db000bb5
Please check and approve!
All right. Thanks for approving.
Thanks for the translation.
You're welcome. I did other translations for your great katas in the last week. Approve if you like them. :-)
You could rephrase the description to: Write a function that receives two strings and calculates by how many characters we should shift the first string to match the second.
Yes, that's much more concise.
When I run the tests, they are all green, but when I push on submit it indicate a test fail?
This comment has been hidden.
The function names in test cases and final cases are different.
I found this bug when I submit my Python code.
^_^
not anymore
I guess the result of the last value used in test is wrong -> (" "," ", -1)
Which language? Was it one space or two spaces?
Haskell
Same problem with Java
Printing the input parameters on the server tests have funny results. The parameters 'first' and 'second' are tested twice, both containing a single space (" ") in both tests. The first test expects a zero (0), the second expects -1 as a return value.
Remember that these are spaces, and they are funnily rendered in HTML... check if both tests use exactly equal strings ;)
You should correct the tested method name(shiftedDiff) to match method name(shifted{underscore}diff) or vice versa. Unless adjusting the test code is part of the exercise. :)
I confirm: in python the test cases look for
shiftedDiff
while the final tests look forshifted_diff
. Please fixNaruto0 & anter69 are correct. The Python test cases should look like this:
PEP8: Function names should be lowercase, with words separated by underscores as necessary to improve readability.
mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.
Fixed this
Hi,
Test case works on my computer but not on the server. It tells me that I need to return -1 when I am returning 0, but in actual fact, I know I am dropping into the condition that returns -1 since I've attached print statements. Any ideas why?
Hi, I've nearly done it, but on test 11 i get this error "Expected: -1, instead got: 1..9", any ideas why? Cheers
Hello ! I've tried this in Java, and all work has expected when I run my solution on Eclipse and with the "Run Tests" button of this site. But When I click "Submit" tests doesn't work. I haven't modified the tests. What's happen please ?
This comment has been hidden.
The same here.
This comment has been hidden.
Python translation has been submitted!
shifted_diff or shiftedDiff??
JS translation kumited.
Thanks!