Skip to content
ExamSpine

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.

  1. Indexes start at 0, so the even indexes are 0, 2, 4 and so on.
  2. text contains only English letters, digits, spaces and punctuation.
  3. text can 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)

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)
solution.pySolution.java