7 kyu
Merge overlapping strings
829 of 1,619syeo66
Loading description...
Algorithms
Strings
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 hidden.
Yes, the issue resutls from your code misunderstanding the task. The only overlap that this Kata is interested in is between the end of the first string and the start of the other. A string overlapping in the middle of the other doesn't count. As you've noticed,
merge_strings('xabc', 'ab')
is expected to returnxabcab
(since no overlap on the edges), and notxabc
. Consult the Kata description further if in doubtThis comment has been hidden.
The
ISSUE
tag is reserved for critical flaws with the tests. 718 people have solved this Kata in JS before just fine, so, when in doubt, please use theQUESTION
tag instead. As for your question, the code inside your loop needs correction. Instead of focusing on the complicated test that fails, focus on the other more simple test that the solution fails on:f: abcd s: dabcd
returnsabcd
for you when it should returnabcdabcd
. This is a byproduct of the method you use to calculateindexInSecond
.Ok. Thanks for the advice. An take your time to let me know. I tried with all cases and this the only one that fails.
But I will do what you suggest me.
2 день решаю, не прохожу всего 6 тестов. И често, я как то не уверен, что эта ката подходит для 7 кю, мне кажется она сложней.
C# Translation
Approved.
Rust translate
approved
Nice Kata! Took me two hours to solve this :)
Please clarify the description: "... when they are an exact match, and appending the second string to the first if they are not. NOTE: The strings may be empty."
Because these cases are not really clear from the given examples. Thank you!
Python translation: https://www.codewars.com/kumite/64039fef90c4da004b93a824?sel=64039fef90c4da004b93a824
Accepted.
The "more test" tests in JS and TS use
assert
instead ofassert.equal
and thus don't really work.Oh, that's an embarassing oversight. Thanks for reporting that. Fixed.
There are 2 incorrect JS solutions that pass all current tests, but fail to handle the following edge cases:
The tests should be added to all languages (and perhaps other tests with empty strings too). UPD: and
mergeStrings('abaa', 'aba'), 'abaaba'
Added to Haskell.
Good catch, thanks. (added to JS)
Haskell translation
Pretty sure I've seen this task before, probably by myjinxin2015. I'm surprised no-one has come up with a duplicate. I've tried searching but can't seem to get the right keywords.
I was surprised as well, but the
Similar kata
section did not come up with anything.Even if it turns out to be a duplicate, this one at least has the more descriptive title. :D
To be honest: I was surprised as well.
It's very similar to that one: https://www.codewars.com/kata/5518503beec78768170001c0
(though I prefer this one, better explained, I enjoyed doing it!).
they are almost the same except the other one requires you to try the strings in reverse as well, so I simply did this, so... yeah
So, what would happen in those cases when 2 Katas are similar but one is stale since probably more than a year (and both in Beta)? 🤔
it's a bit random... If no other duplicate is found then the first that would become approvable could be approved, and the other retired... Supposedly... However, the current policy wouldn't admit both.
https://www.codewars.com/kata/5ce969ab07d4b7002dcaa7a1 Looks pretty similar. Not sure if it counts as a duplicate.
Missing fixed test cases
mergeStrings('xabc', 'ab')
andmergeStrings('abc', 'ab')
.At least one invalid solution has several bugs, which are not caught.
Thanks. Added those exact cases. Hope this is okay for you.
Perfectly OK. :]
Sometimes (very rarely) random tests generate wrong test cases, e.g:
Oh, right. Good catch.
So, I hopefully fixed this one by just (admittedly a lazy solution) replacing the overlap in b with an empty string. While doing this I realized my own solution does not work when strings are repeated before the overlap and at the start of the overlap (like 'abcNANAd' 'NAdl'). Oh, the shame...
The better solution is to use a correct reference solution.
For this kata, generating specifically built test cases with predefined expected values has problems which can be very subtle, thus hard to prevent ( or even find ). Don't do this to yourself.
( What if
overlap
occurs twice inb
? )Yes, you're right. However thanks to the random tests I even found my reference implementation was flawed. Will adjust the tests.
If your reference implementation is flawed, people will be all over you, very quickly, because they can't pass the random tests.
One way to prevent this situation is having different example and reference solutions. This is not always possible, but whenever you can do it, it can be extremely useful. ( Ask me how I know .. :P )
If you can construct tests with expected values, it's a good way to not even depend on a reference solution. But you have to be very, very sure
expected
will always be correct.Just adjusted the random tests to use a reference implementation.