Swap the ends
- O(1) · Trivial
- Free
- Python
- JavaScript
- strings
- indexes
Problem
Write a function that takes a text (with at least one letter) and returns that same text with its first letter and its last one swapped: with "cat" you return "tac".
If the text has a single letter there is nothing to swap: return it as it came.
Examples
"cat" → "tac"
"house" → "eoush"
Two letters
"ab" → "ba"
"green" → "nreeg"
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def swap_ends(text):
passJavaScript
function swapEnds(text) {
}Solve this challenge
It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.