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.
More O(1) challenges
- The area of the platePython · JavaScript · operations · decimals
- The average without the worstPython · JavaScript · lists · indexes · decimals
- The bill with the tipPython · JavaScript · operations · decimals
- The boxes you needPython · JavaScript · operations · division
- The candy left in the bagPython · JavaScript · operations · division
- The cards I getPython · JavaScript · operations · division · conditionals