15
1
The challenge involve simply toggling a string within another string.
Explanation
If the toggle string is a substring of the main string, remove all instances of the toggle string from the main string; otherwise, append the toggle string at the end of the main string.
Rules
- All string are composed of printable ASCII characters
- The function should take two parameters: the main string and the toggle string.
- The main string can be empty.
- The toggle string cannot be empty.
- The result should be a string, which can be empty.
- The shortest answer wins.
Examples
function toggle(main_string, toggle_string){ ... }
toggle('this string has 6 words ', 'now')
=> 'this string has 6 words now'
toggle('this string has 5 words now', ' now')
=> 'this string has 5 words'
Tests cases
'','a' => 'a'
'a','a' => ''
'b','a' => 'ba'
'ab','a' => 'b'
'aba','a' => 'b'
'ababa', 'aba' => 'ba'
And once it goes to the main namespace, you guys ask all sorts of questions – Leaky Nun – 2016-05-20T13:17:43.350
2@KennyLau It was in the sandbox for all of 3 hours. The recommendation is 2 days. – Morgan Thrapp – 2016-05-20T13:18:32.757
9
The recommendation is actually 72 hours. The main page has much more visibility than the Sandbox, so more comments are guaranteed here. That said, this isn't a bad challenge, just has a few rough edges.
– AdmBorkBork – 2016-05-20T13:19:49.5102So you replace all non-overlapping instances? – Suever – 2016-05-20T13:26:05.133
You can break quite a few solutions with the test-case:
'abc.', '.'
, since quite a lot of them use some sort of regex. – Jakube – 2016-05-20T15:48:47.1931@Jakube Yes, I should limit this to letters and number I think. – nobe4 – 2016-05-20T16:20:18.990
1No, I think allow nonalphanumerics: it's more challenging that way. – msh210 – 2016-05-22T07:35:42.920
Is the order of the function parameters fixed (
main_string
as first andtoggle_string
as second parameter) or it is allowed to change the order? – Heiko Oberdiek – 2016-05-22T15:04:04.907Yes, but you can propose your solution anyway, just for the challenge :) – nobe4 – 2016-05-22T15:05:13.657