Menu
Coddy logo textTech

Word Analytics

Part of the Logic & Flow section of Coddy's R journey. Lesson 62 of 64.

challenge icon

Challenge

Hard

Complete word_stats(text). Lower-case the text and remove the characters ., ,, ! and ?, then split it into words at runs of spaces. Return four lines:

  • Words: with the number of words
  • Distinct: with the number of different words
  • Top: with the three most frequent words and their counts, such as the (3), and (2), bird (1), ordered by count from high to low and alphabetically among equal counts (fewer than three when the text has fewer words)
  • Longest: with the longest word and its length in brackets, the first one in the text when several tie

The supplied code reads the text and prints each returned line.

Try it yourself

word_stats <- function(text) {
  # Write your code here
  character(0)
}

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
cat(word_stats(input[1]), sep = "\n")

All lessons in Logic & Flow

Practice on your own: Online R compiler