""" 5kyu - Readability is King

We will use the Flesch–Kincaid Grade Level to evaluate the readability of a piece of text. 
This grade level is an approximation for what schoolchildren are able to understand a piece of text. 
For example, a piece of text with a grade level of 7 can be read by seventh-graders and beyond.

The way to calculate the grade level is as follows:

(0.39 * average number of words per sentence) + (11.8 * average number of syllables per word) - 15.59

Write a function that will calculate the Flesch–Kincaid grade level for any given string. 
Return the grade level rounded to two decimal points.

Ignore hyphens, dashes, apostrophes, parentheses, ellipses and abbreviations.

Remember that the text can contain more than one sentence: code accordingly!

HINT: Count the number of vowels as an approximation for the number of syllables, but count groups of vowels as one 
(e.g. deal is one syllable). Do not count y as a vowel!

Example

"The turtle is leaving." ==> 3.67

The average number of words per sentence is 4 and the average number of syllables per word is 1.5. 
The score is then (0.39 * 4) + (11.8 * 1.5) - 15.59 = 3.67 """

import re
from itertools import groupby


def flesch_kincaid(text):
    sentences = syllables = words = 0
    # for sentence in re.split(r'[?!.]', text):
    for sentence in re.split(r'[?!.]', text.upper()):
        if not sentence:
            return round(0.39 * (words / sentences) + 11.8 * (syllables / words) - 15.59, 2)
        for word in sentence.split():
            # syllables += sum(syl for syl, _ in groupby(1 if ch in 'AEIOUaeiou' else 0 for ch in word))
            syllables += sum(syl for syl, _ in groupby(1 if c in 'AEIOU' else 0 for c in word))
            words += 1
        sentences += 1


q = flesch_kincaid("The turtle is leaving."), 3.67
q
q = flesch_kincaid("A good book is hard to find."), -1.06
q
# remember to use the average number of word BY SENTENCE; below: 2 sentences
q = flesch_kincaid("To be or not to be. That is the question."), -0.66
q
