Menu
CoddyTech

Word Analytics

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 77 of 78.

challenge icon

Challenge

Hard

Write the function analyze_text(text) above _ready(). It analyzes the words of a text and returns a dictionary. The given _ready() reads one line of text, calls your function and prints the result.

Prepare the words first:

  • Words are case-insensitive: Hello and hello are the same word, so work in lowercase
  • Remove these characters from the text: . , ! ? : ; " ( )
  • Words are separated by spaces, and an empty text has no words

Return a dictionary with three keys, in this order:

  • "unique_count": the number of different words
  • "repeated_words": an array of the words that appear more than once
  • "palindromes": an array of the different words that read the same backwards, such as level or a

Each word appears once in an array, and both arrays are sorted alphabetically. For the text below:

Madam saw a racecar. Dad said hello hello to mom.

the output is:

{ "unique_count": 9, "repeated_words": ["hello"], "palindromes": ["a", "dad", "madam", "mom", "racecar"] }

Try it yourself

extends Node

func analyze_text(text: String):
	# Write your code here
	pass


func _ready():
	var text := OS.read_string_from_stdin()
	print(analyze_text(text))

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler