Question

In: Computer Science

IN PYTHON Rondo Form is a type of musical structure, in which there is a recurring...

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:

  • Rondo forms always start and end with an A section.
  • In between the A sections, there should be contrasting sections (of arbitrary length) notated as B (or BB, BBB, BBBBB, etc), then C, then D, etc... No letter should be skipped.
  • There shouldn't be any repeats in the sequence (such as ABBACCA).

Create a program which validates whether a given string is a valid Rondo Form.

  • Inputs will be given as all uppercase.
  • For the purpose of this problem, accept ABA (ACA, ADA, etc) as valid Rondo Forms. In other words, we allow a single section to be repeated in this problem; however, double, triple, etc. sections can NOT be repeated (per 3rd bullet above).

Examples

ABACADAEAFAGAHAIAJA ➞ True ABA ➞ True ABBACCA ➞ False ACAC ➞ False A ➞ False ABBACCCADAEA ➞ True

Sample Input 1

ABBACADA

Sample Output 1

True

Solutions

Expert Solution

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


Related Solutions

disscuss the type of cells which form retinal pigment epithelium.
disscuss the type of cells which form retinal pigment epithelium.
Which type of organizational structure does Saudi Aramco currently have? Functional structure, divisional structure, or matrix...
Which type of organizational structure does Saudi Aramco currently have? Functional structure, divisional structure, or matrix structure? Provide evidence. What organizational structure does it use to govern its foreign market operation (International Division Structure, Worldwide (3-dimensional) Matrix, Worldwide Product Division Structure, or something else)? (Questions do not belong to a specific case, just on the organizational structure Saudi Aramco currently has.)
The operations manager of a musical instrument distributor feels that demand for a particular type of...
The operations manager of a musical instrument distributor feels that demand for a particular type of guitar may be related to the number of YouTube views for a popular music video by the popular rock group Marble Pumpkins during the preceding month. The manager has collected the data shown in the following table: (can be done in excel) YouTube Views (1000s) Guitar Sales 30 8 40 11 70 12 60 10 80 15 50 13 Graph the data to see...
Which statement does NOT describe DNA structure? The phosphates and sugars of the nucleotides form the...
Which statement does NOT describe DNA structure? The phosphates and sugars of the nucleotides form the “backbone” of the double helix. The two complementary strands are held together by hydrogen bonds. The nitrogen bases of the nucleotides project towards the center of the helix. The 5-carbon sugar in each nucleotide is ribose.
An element crystallizes into a structure which may be described by a cubic type of unit...
An element crystallizes into a structure which may be described by a cubic type of unit cell 7. having one atom in each corner of the cube and two atoms on one of its face diagonals. If the volume of this unit cell is 24 x 10-24 cm3 and density of the element is 7.20 gm/cm3, calculate number of atoms present in 200 gm of the element
What is the main difference between a U-form organisational structure and an M-form organisational structure?
What is the main difference between a U-form organisational structure and an M-form organisational structure?
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
#2. The operations manager of a musical instrument distributor feels that demand for a particular type...
#2. The operations manager of a musical instrument distributor feels that demand for a particular type of guitar may be related to the number of YouTube views for a popular music video by the popular rock group Marble Pumpkins during the preceding month. The manager has collected the data shown in the following table: YouTube Views (1000s) Guitar Sales 30 8 40 11 70 12 60 10 80 15 50 13 a. Graph the data to see whether a linear...
For this activity, select a recurring quantity from your OWN life for which you have monthly...
For this activity, select a recurring quantity from your OWN life for which you have monthly records at least 2 years (including 24 observations in the dataset at least). This might be the cost of a utility bill, the number of cell phone minutes used, or even your income. If you do not have access to such records, use the internet to find similar data, such as average monthly housing prices, rent prices in your area for at least 2...
For this activity, select a recurring quantity from your OWN life for which you have monthly...
For this activity, select a recurring quantity from your OWN life for which you have monthly records at least 2 years (including 24 observation in dataset at least). This might be the cost of a utility bill, the number of cell phone minutes used, or even your income. If you do not have access to such records, use the internet to find similar data, such as average monthly housing prices, rent prices in your area for at least 2 years...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT