Lesson 5.4 · Write it
Characters at even indexes
A functionPython or Java9 tests, 6 of them hidden
On a phone? Put the lines in order instead
Write a function that returns the characters at the even indexes of a text value, joined in the order they appear.
- Indexes start at 0, so the even indexes are 0, 2, 4 and so on.
textcontains only English letters, digits, spaces and punctuation.textcan be empty. Empty text gives empty text back.
Only the value your function returns is checked.
def every_second(text: str) -> str
Examples
Your function is tested on 9 cases: the 3 examples below, and 6 hidden ones that check the rules above.
- every_second("planet") should return "pae": indexes 0, 2 and 4 of six characters (rule 1)
- every_second("abc") should return "ac": an odd length: the last character is at index 2, which is even
- every_second("") should return "": empty text (rule 3)
public static String everySecond(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.
- everySecond("planet") should return "pae": indexes 0, 2 and 4 of six characters (rule 1)
- everySecond("abc") should return "ac": an odd length: the last character is at index 2, which is even
- everySecond("") should return "": empty text (rule 3)