mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-11 09:01:44 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
2.1 KiB
2.1 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6814d93d516e86b171929de5 | Python Challenge 1: Vowel Balance | 29 | python-challenge-1 |
--description--
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.
- The string can contain any characters.
- The letters
a,e,i,o, andu, in either uppercase or lowercase, are considered vowels. - If there's an odd number of characters in the string, ignore the center character.
--hints--
is_balanced("racecar") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertTrue(is_balanced("racecar"))`)
}})
is_balanced("Lorem Ipsum") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertTrue(is_balanced("Lorem Ipsum"))`)
}})
is_balanced("Kitty Ipsum") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertFalse(is_balanced("Kitty Ipsum"))`)
}})
is_balanced("string") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertFalse(is_balanced("string"))`)
}})
is_balanced(" ") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertTrue(is_balanced(" "))`)
}})
is_balanced("abcdefghijklmnopqrstuvwxyz") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertFalse(is_balanced("abcdefghijklmnopqrstuvwxyz"))`)
}})
is_balanced("123A#b!E&*456-o.U") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertTrue(is_balanced("123A#b!E&*456-o.U"))`)
}})
--seed--
--seed-contents--
def is_balanced(s):
return s
--solutions--
def is_balanced(s):
vowels = set("aeiouAEIOU")
n = len(s)
half = n // 2
first_half = s[:half]
second_half = s[-half:]
def count_vowels(sub):
return sum(1 for char in sub if char in vowels)
return count_vowels(first_half) == count_vowels(second_half)