In: Computer Science
IN PYTHON
Rondo Form is a type of musical structure, in which there is a recurring theme/refrain. Your job is to determine whether a given input is a valid Rondo Form or not. Here are the rules for valid Rondo forms:
Create a program which validates whether a given string is a valid Rondo Form.
Examples
ABACADAEAFAGAHAIAJA ➞ True ABA ➞ True ABBACCA ➞ False ACAC ➞ False A ➞ False ABBACCCADAEA ➞ True
Sample Input 1
ABBACADA
Sample Output 1
True
Approach :
1. If lenght of string is 1 then false as it can't have 2 A's
2. If first or last character is not A then False
3. Remove all A's from string and check if new string is sorted or not.
4. if there exist a index i such that new[i] > new [i +1] then string is not sorted else string is sorted
5. store frequency of each character in frequency array eg BBCCC should be [ 2,3]
6. Check if there exist a duplicate in frequency other than if it exist then ans is false else true
Below is implementation of above approach :
# input string
s=input()
# check length of string
n = len(s)
# if first or last character is not A then print false
# or lenght of string =1
if s[0] != 'A' or s[n-1] !='A' or n==1:
print("False")
else :
# new is a string containing 0 A
new=""
for character in s:
# if character is A then skip
if character=='A':
continue
new+=character
#check if new is sorted or not
is_sorted = 1
for idx in range(len(new)-1):
if new[idx] > new [idx+1]:
is_sorted = 0
break
if is_sorted == 0:
print("False")
else :
frequency_array = []
cur_lenght=1
#Build frequency array
for idx in range(1,len(new)):
if new[idx]==new[idx-1]:
cur_lenght+=1
else :
frequency_array.append(cur_lenght)
cur_lenght=1
if cur_lenght > 0:
frequency_array.append(cur_lenght)
# Use count array to check for duplicate
count = []
duplicate = 0
n=len(frequency_array)
for i in range(n+1):
count.append(0)
for element in frequency_array:
count[element]+=1
#check for duplicate
if count[element] > 1 and element!=1:
duplicate = 1;
break
if duplicate==0 :
print("True")
else :
print("False")
Below is screenshot of code along with the output