📜  Python|接受包含所有元音的字符串的程序

📅  最后修改于: 2022-05-13 01:54:24.899000             🧑  作者: Mango

Python|接受包含所有元音的字符串的程序

给定一个字符串,任务是检查每个元音是否存在。如果元音以大写或小写形式出现,我们认为元音存在。即 'a', 'e', 'i'.'o', 'u' 或 'A', 'E', 'I', 'O', 'U' 。
例子 :

Input : geeksforgeeks
Output : Not Accepted
All vowels except 'o' are not present

Input : ABeeIghiObhkUul
Output : Accepted
All vowels are present

方法:首先,使用 set()函数创建一组元音。检查字符串的每个字符是否为元音,如果元音则添加到集合 s 中。退出循环后,检查集合 s 的长度,如果集合 s 的长度等于元音集合的长度,则接受字符串,否则不接受。
下面是实现:

Python3
# Python program to accept the strings
# which contains all the vowels
 
# Function for check if string
# is accepted or not
def check(string) :
 
    string = string.lower()
 
    # set() function convert "aeiou"
    # string into set of characters
    # i.e.vowels = {'a', 'e', 'i', 'o', 'u'}
    vowels = set("aeiou")
 
    # set() function convert empty
    # dictionary into empty set
    s = set({})
 
    # looping through each
    # character of the string
    for char in string :
 
        # Check for the character is present inside
        # the vowels set or not. If present, then
        # add into the set s by using add method
        if char in vowels :
            s.add(char)
        else:
            pass
             
    # check the length of set s equal to length
    # of vowels set or not. If equal, string is 
    # accepted otherwise not
    if len(s) == len(vowels) :
        print("Accepted")
    else :
        print("Not Accepted")
 
 
# Driver code
if __name__ == "__main__" :
     
    string = "SEEquoiaL"
 
    # calling function
    check(string)


Python3
def check(string):
    string = string.replace(' ', '')
    string = string.lower()
    vowel = [string.count('a'), string.count('e'), string.count(
        'i'), string.count('o'), string.count('u')]
 
    # If 0 is present int vowel count array
    if vowel.count(0) > 0:
        return('not accepted')
    else:
        return('accepted')
 
 
# Driver code
if __name__ == "__main__":
 
    string = "SEEquoiaL"
 
    print(check(string))


Python3
# Python program for the above approach
def check(string):
    if len(set(string.lower()).intersection("aeiou")) >= 5:
        return ('accepted')
    else:
        return ("not accepted")
 
 
# Driver code
if __name__ == "__main__":
    string = "geeksforgeeks"
    print(check(string))


Python3
#import library
import re
 
sampleInput = "aeioAEiuioea"
 
# regular expression to find the strings
# which have characters other than a,e,i,o and u
c = re.compile('[^aeiouAEIOU]')
 
# use findall() to get the list of strings
# that have characters other than a,e,i,o and u.
if(len(c.findall(sampleInput))):
    print("Not Accepted")  # if length of list > 0 then it is not accepted
else:
    print("Accepted")  # if length of list = 0 then it is accepted


Python3
# Python | Program to accept the strings which contains all vowels
 
 
def all_vowels(str_value):
 
    new_list = [char for char in str_value.lower() if char in 'aeiou']
 
    if new_list:
 
        dic, lst = {}, []
 
        for char in new_list:
            dic['a'] = new_list.count('a')
            dic['e'] = new_list.count('e')
            dic['i'] = new_list.count('i')
            dic['o'] = new_list.count('o')
            dic['u'] = new_list.count('u')
 
        for i, j in dic.items():
            if j == 0:
                lst.append(i)
 
        if lst:
            return f"All vowels except {','.join(lst)} are not presant"
        else:
            return 'All vowels are present'
 
    else:
        return "No vowels presant"
 
 
# function-call
str_value = "geeksforgeeks"
print(all_vowels(str_value))
 
str_value = "ABeeIghiObhkUul"
print(all_vowels(str_value))
 
# contribute by saikot


输出
Accepted

替代实施:

Python3

def check(string):
    string = string.replace(' ', '')
    string = string.lower()
    vowel = [string.count('a'), string.count('e'), string.count(
        'i'), string.count('o'), string.count('u')]
 
    # If 0 is present int vowel count array
    if vowel.count(0) > 0:
        return('not accepted')
    else:
        return('accepted')
 
 
# Driver code
if __name__ == "__main__":
 
    string = "SEEquoiaL"
 
    print(check(string))
输出
accepted

替代实现 2.0:

Python3

# Python program for the above approach
def check(string):
    if len(set(string.lower()).intersection("aeiou")) >= 5:
        return ('accepted')
    else:
        return ("not accepted")
 
 
# Driver code
if __name__ == "__main__":
    string = "geeksforgeeks"
    print(check(string))
输出
not accepted

替代实现 3.0(使用正则表达式):

使用 compile() 为“字符不是 a、e、i、o 和 u”编译正则表达式。
使用 re.findall() 获取满足上述正则表达式的字符串。
根据结果打印输出。

Python3

#import library
import re
 
sampleInput = "aeioAEiuioea"
 
# regular expression to find the strings
# which have characters other than a,e,i,o and u
c = re.compile('[^aeiouAEIOU]')
 
# use findall() to get the list of strings
# that have characters other than a,e,i,o and u.
if(len(c.findall(sampleInput))):
    print("Not Accepted")  # if length of list > 0 then it is not accepted
else:
    print("Accepted")  # if length of list = 0 then it is accepted
输出
Accepted

替代实现 4.0(使用数据结构):

Python3

# Python | Program to accept the strings which contains all vowels
 
 
def all_vowels(str_value):
 
    new_list = [char for char in str_value.lower() if char in 'aeiou']
 
    if new_list:
 
        dic, lst = {}, []
 
        for char in new_list:
            dic['a'] = new_list.count('a')
            dic['e'] = new_list.count('e')
            dic['i'] = new_list.count('i')
            dic['o'] = new_list.count('o')
            dic['u'] = new_list.count('u')
 
        for i, j in dic.items():
            if j == 0:
                lst.append(i)
 
        if lst:
            return f"All vowels except {','.join(lst)} are not presant"
        else:
            return 'All vowels are present'
 
    else:
        return "No vowels presant"
 
 
# function-call
str_value = "geeksforgeeks"
print(all_vowels(str_value))
 
str_value = "ABeeIghiObhkUul"
print(all_vowels(str_value))
 
# contribute by saikot
输出
All vowels except a,i,u are not presant
All vowels are present