Lesson 5.2 · Write it
Remove first and last
A functionPython or Java9 tests, 6 of them hidden
On a phone? Put the lines in order instead
Write a function that returns a text value without its first character and without its last character.
texthas at least 3 characters. It contains only English letters, digits, spaces and punctuation.- Every other character is returned, in the same order.
Only the value your function returns is checked.
def remove_ends(text: str) -> str
Examples
Your function is tested on 9 cases: the 3 examples below, and 6 hidden ones that check the rules above.
- remove_ends("hello") should return "ell": h and o are removed, and the three letters between them stay (rule 2)
- remove_ends("abc") should return "b": three characters, the fewest allowed: one is left (rule 1)
- remove_ends("(done)") should return "done": the first and last characters here are brackets (rule 1)
public static String removeEnds(String text)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 9 cases: the 3 examples below, and 6 hidden ones that check the rules above.
- removeEnds("hello") should return "ell": h and o are removed, and the three letters between them stay (rule 2)
- removeEnds("abc") should return "b": three characters, the fewest allowed: one is left (rule 1)
- removeEnds("(done)") should return "done": the first and last characters here are brackets (rule 1)