xy_balance¶
Requirements:
access a single character
loop through a string (while)
if, elif, else
We’ll say that a String is xy-balanced if for all the 'x'
chars in the string, there exists a 'y'
char somewhere later in the string. So "xxy"
is balanced, but "xyx"
is not. One 'y'
can balance multiple 'x'
s. Output True
if the given string is xy-balanced. Output False
otherwise.
This exercise was taken from codingbat.com and has been adapted for the Python language. There are many great programming exercises there, but the majority are created for Java.