Home > Software engineering >  Print result based on first character of sub-string in python
Print result based on first character of sub-string in python

Time:09-18

I want to count the number of sub-string within a string in python based on the 1st character (vowel/consonants) of the sub-string. For example, in the string 'Banana', 'an' is a sub-string that started with a vowel 'a', and it appears 2 times, so I want to count it twice. Same for the other combination ('ana'/ 'anan'...etc). And the sub-string can be repeated, meaning 'an' is also shown in 'ana', so it would be counted twice.

But I could only count the number of sub-string (vowel) appears in the string, is there a way to count the number of sub-string based on the 1st character of sub-string? Plus it should be repetitive like the 'an' and 'ana' case discussed above. Currently my code is as below. Thanks a lot!!

string = input() #User input a string with vowel and consonant
def minion_game(string):
    count_v = 0 #count the number of sub-string started with vowel
    count_c = 0 #count the number of sub-string started with consonant
    for i in range(len(string)):
        if string[i] == 'a' or string[i] == 'e' or string[i] == 'i' or string[i] == 'o' or string[i] == 'u':
            count_c  = 1
        else:
            count_v  = 1
    return count_v, count_c

CodePudding user response:

I think you should separate your problem into two parts:

  1. What are the substrings, you want to count?
  2. How many times does each substring occur

For the first part, search each vowel in a string and return every substring starting with the vowel by itself and then increasing the length until it reaches end of string. Here is an example implementation for this:

def create_substrings(string):
    l = len(string)
    for vowel in "aeiou":
        pos = 0
        while pos != -1:
            pos = string.find(vowel, pos)
            if pos != -1:
                # Found vowel at position pos
                for i in range(pos, l):
                    # yield a string starting at pos and ending at i
                    yield string[pos:i 1]
                pos  = 1

for substr in create_substrings("bananaicecream"):
    print(substr)

Part 2 should be implemented similarly (using a loop and str.find) and the function be called with every substr returned by the first function.

CodePudding user response:

It looks like you are wanting to individually count every possible substring that starts with a vowel or with a consonant but not include the individual vowels and consonants themselves. It's possible that creating a list of substrings would be more useful than just counting them, so here is an approach that creates two lists: one of all possible consonant substrings and one of all possible vowel substrings, and then it returns the counts you are looking for based upon the number of elements in each list:

def minion_game(userInput):
    string = str.lower(userInput)#convert input to lowercase string
    count_v = []
    count_c = []
    for i in range(2):# run loop twice, once to find vowels, and once to find consonants
        keyRange = "aeiou"
        if i > 0:
            keyRange = "bcdfghjklmnpqrstvwxyz"
        for keyLetter in keyRange:#create substring lists
            letter = 0
            while letter != -1:
                letter = string.find(keyLetter, letter)
                if letter != -1:
                    for num in range(letter, len(string)):
                        if len(string[letter:num 1])>1:
                            if i == 0:
                                count_v.append(string[letter:num 1])
                            else:
                                count_c.append(string[letter:num 1])
                    letter  = 1
                
    print "All vowel substrings: " str(count_v)
    print "Total count of vowel substrings: " str(len(count_v))
    print "All consonant substrings: " str(count_c)
    print "Total count of consonant substrings: " str(len(count_c))
    return (len(count_v), len(count_c))
userInput = "Banana"
print minion_game(userInput)

Here is the output for this code:

All vowel substrings: ['an', 'ana', 'anan', 'anana', 'an', 'ana']
Total count of vowel substrings: 6
All consonant substrings: ['ba', 'ban', 'bana', 'banan', 'banana', 'na', 'nan', 'nana', 'na']
Total count of consonant substrings: 9
(6, 9)
  • Related