Платформа: CodeWars
Название задачи: up AND down (вверх и вниз)
Ссылка на задачу: https://www.codewars.com/kata/56cac350145912e68b0006f0
Сложность: 6 kyu
Уже решили (На момент написания статьи): 3 119 из 16 402
Тэги: Основы, струны
Оригинальное описание задачи:
Don't be afraid, the description is rather long but - hopefully - it is in order that the process be well understood.
You are given a string s made up of substring s(1), s(2), ..., s(n) separated by whitespaces. Example: "after be arrived two My so"
Task
Return a string t having the following property:
length t(O) <= length t(1) >= length t(2) <= length t(3) >= length t(4) .... (P)
where the t(i) are the substring of s; you must respect the following rule:
at each step from left to right, you can only move either already consecutive strings or strings that became consecutive after a previous move. The number of moves should be minimum.
Let us go with our example:
The length of "after" is greater than the length of "be". Let us move them ->"be after arrived two My so"
The length of "after" is smaller than the length of "arrived". Let us move them -> "be arrived after two My so"
The length of "after" is greater than the length of "two" ->"be arrived two after My so"
The length of "after" is greater than the length of "My". Good! Finally the length of "My" and "so" are the same, nothing to do. At the end of the process, the substrings s(i) verify:
length s(0) <= length s(1) >= length s(2) <= length s(3) >= length (s4) <= length (s5)
Hence given a string s of substrings s(i) the function arrange with the previous process should return a unique string t having the property (P).
It is kind of roller coaster or up and down. When you have property (P), to make the result more "up and down" visible t(0), t(2), ... will be lower cases and the others upper cases.
arrange("after be arrived two My so") should return "be ARRIVED two AFTER my SO"
Notes:
- The string
"My after be arrived so two" has the property (P) but can't be obtained by the described process so it won't be accepted as a result. The property (P) doesn't give unicity by itself.
- Process: go from left to right, move only consecutive strings when needed.
- For the first fixed tests the needed number of moves to get property (P) is given as a comment so that you can know if your process follows the rule.
Пояснение задачи:
Дана строка `s`, состоящая из подстрок `s(1), s(2), ..., s(n)`, разделенных пробелами.
Пример: "after be arrived two My so"
Задача
Вернуть строку `t`, удовлетворяющую следующему свойству:
`length t(O) <= length t(1) >= length t(2) <= length t(3) >= length t(4) ... (P)`
где `t(i)` — это подстроки из `s`;
должны соблюдаться следующие правила:
на каждом шаге от левого к правому концу строки можно перемещать только уже смежные подстроки или те, которые стали смежными после предыдущего перемещения. Количество таких перемещений должно быть минимальным.
Пример:
Для строки "after be arrived two My so" процесс может выглядеть следующим образом:
1. Длина "after" больше длины "be", поэтому переставляем их -> `"be after arrived two My so"`
2. Длина "after" меньше длины "arrived", поэтому переставляем их -> `"be arrived after two My so"`
3. Длина "after" больше длины "two", поэтому переставляем их -> `"be arrived two after My so"`
4. Длина "after" больше длины "My", но они уже смежные, поэтому ничего не меняем -> `"be arrived two after MY so"`
5. Длина "MY" и "so" равны, поэтому ничего не меняем -> `"be arrived two after My SO"`
Таким образом, после выполнения всех шагов подстроки `s(i)` будут удовлетворять условию (P):
"arrange("after be arrived two My so") должна вернуть "be ARRIVED two AFTER MY SO""
Примечания:
- Строка `"My after be arrived so two"` также удовлетворяет условию (P), но не может быть получена с помощью описанного процесса, поэтому она не будет принята как результат. Условие (P) само по себе не гарантирует уникальность результата.
- Процесс: двигаемся слева направо, перемещаем только смежные подстроки, когда это необходимо.