Skip to content
ExamSpine

Lesson 5.1 · Write it

Last character

A functionPython or Java7 tests, 4 of them hidden

On a phone? Put the lines in order instead

Write a function that returns the last character of a text value.

  1. text has at least one character. It contains only English letters, digits, spaces and punctuation.
  2. Return the last character as a text value of length 1. In Java that means a String, not a char.

Only the value your function returns is checked.

def last_char(text: str) -> str

Examples

Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.

  • last_char("hello") should return "o": the last of five letters (rule 2)
  • last_char("A") should return "A": a single character (rule 1)
  • last_char("go ") should return " ": a space is a character too, and here it is the last one

public static String lastChar(String text)

Checking whether Java answers can run here.

Examples

Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.

  • lastChar("hello") should return "o": the last of five letters (rule 2)
  • lastChar("A") should return "A": a single character (rule 1)
  • lastChar("go ") should return " ": a space is a character too, and here it is the last one
solution.pySolution.java