is_balanced

Run Settings
LanguagePython
Language Version
Run Command
def is_balanced(s): vowels = ["a", "e", "i", "o", "u"] firstHalfCounter = secondHalfCounter = 0 stringToCheck = s.casefold() length = len(s) isOdd = length % 2 > 0 mid = length // 2 firstHalf = stringToCheck[:mid] secondHalf = stringToCheck[mid:] if isOdd: secondHalf = stringToCheck[mid+1:] for i in range(len(firstHalf)): if firstHalf[i] in vowels: firstHalfCounter += 1 if secondHalf[i] in vowels: secondHalfCounter += 1 print(f"First half counter {firstHalfCounter} with {firstHalf}, second half counter {secondHalfCounter} with {secondHalf}") return firstHalfCounter == secondHalfCounter result = is_balanced("racecar") print(f"Is racecar a balanced vowels string? {result}") result = is_balanced("Lorem Ipsum") print(f"Is Lorem Ipsum a balanced vowels string? {result}") """ Given a string, determine whether the number of vowels in the first half of the string is equal to the number of vowels in the second half. """
Editor Settings
Theme
Key bindings
Full width
Lines