【每日一题0726】元音排序

:woman_mage:请编写一个函数,接收一个字符串列表作为它的参数,然后对每个字符串进行处理,并根据字符串中可能包含的单个最长的连续元音子字符串的长度进行排序(按下行顺序排序)。

字符串可能包含字母、数字、特殊字符、大写字母、小写字母、空格,并且可能(通常)有多个连续元音子字符串。我们只对输入列表中每个字符串中单个最长的元音子字符串感兴趣。

示例:

str1 = "what a beautiful day today"
str2 = "it's okay, but very breezy"

当对字符串进行排序时,str1将首先排序,因为它最长的连续元音“ eau”子字符串长度为3,而 str2最长的连续元音“ ee”子字符串长度为2。

如果列表中的两个或多个字符串具有相同长度的最大子字符串,那么这些字符串应该保持它们在原列表中的顺序。

题目难度:中等
题目来源:codewars

def sort_strings_by_vowels(words:list) -> list: 
    pass
    
assert sort_strings_by_vowels(["aa","eee","oo","iiii"]) == ["iiii","eee","aa","oo"]
assert sort_strings_by_vowels(["ioue","ee","uoiea"]) == ["uoiea", "ioue","ee"]
assert sort_strings_by_vowels(["AIBRH","","YOUNG","GREEEN"]) == ["GREEEN","AIBRH","YOUNG",""]

def sort_strings_by_vowels(list1):
a = [‘a’,‘e’,‘i’,‘o’,‘u’, “A”, “E”, “I”, “O”, “U”]
dicts={}
s=
for i in list1:
b =0
for j in i:
if j in a:
b += 1
dicts[i]=b
dict_new=sorted(dicts.items(),key=lambda x:x[1],reverse=True)
for z in dict_new:
s.append(z[0])
return s

def getMax(s: str):
    """
    获取字符串最大元音数
    :param s: 字符串
    :return: 最大元音数
    """
    r = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    slow = 0
    fast = 0
    res = []
    while slow <= fast and (fast < len(s)):
        if s[slow] in r and s[fast] in r:
            fast += 1
        else:
            res.append(fast - slow)
            slow = fast
            fast += 1
    else:
        res.append(fast - slow)
    return max(res)


def sort_strings_by_vowels(words: list) -> list:
    """
    获取重新排序的字符串列表
    :param words: 字符串列表
    :return: 按最大原因数升序排列的字符串列表
    """
    s = {}
    for i in words:
        s[i] = getMax(i)
    s = sorted(s.items(), key=lambda x: x[1], reverse=True)
    return [i[0] for i in s]


assert sort_strings_by_vowels(["aa", "eee", "oo", "iiii"]) == ["iiii", "eee", "aa", "oo"]
assert sort_strings_by_vowels(["ioue", "ee", "uoiea"]) == ["uoiea", "ioue", "ee"]
assert sort_strings_by_vowels(["AIBRH", "", "YOUNG", "GREEEN"]) == ["GREEEN", "AIBRH", "YOUNG", ""]
def sort_strings_by_vowels(words: list) -> list:
    len_list = []
    result = []
    for i in words:
        len_list.append([i,len_vowels(i)])
    list = sorted(len_list, key=lambda x:x[1],reverse = True)
    for i in list:
        result.append(i[0])
    return result

def len_vowels(str):
    list = ['a','e','i','o','u','A','E','I','O','U']
    flag = False
    len = 0
    max = -1
    for i in str:
        if i in list and flag == False:
            flag = True
            len = len + 1
        elif i in list and flag == True:
            len = len + 1
        elif i not in list and flag == True:
            flag = False
            if max < len :
                max = len
    if max < len :
        max = len
    return max

str1 = “what a beautiful day today”
str2 = “it’s okay, but very breezy”
list=[str1,str2]
def sort_strings_by_vowels(list):
dict={}
for i in list:
dict.update({(len_aeiou(i)):i}) #字典添加元素进去,不能用赋值
return sorted(dict.items(),key=lambda x:x[1],reverse=True)

def len_aeiou(str):
list_demo=[“a”,“e”,“i”,“o”,“u”,“A”,“E”,“I”,“O”,“U”]
flage=False
len=1
for i in str:
if i in list_demo:
if flage:
len+=1
else:
flage=True
if i not in list_demo :
flage=False
return len

if name == ‘main’:
print(sort_strings_by_vowels(list))

def sort_strings_by_vowels(words: list) -> list:
    # 列表中每个字符串含最长连续元音字符数
    s_len = []
    for s in words:
        s = s.lower()
        # 每个字符串可能有多个连续元音子字符串,最后取最大值
        l = 0
        l_list = []
        for i in s:
            if i in "aeiou":
                l += 1
            else:
                l_list.append(l)
                l = 0
        l_list.append(l)
        s_len.append(max(l_list))
    # 将2个列表转为字典格式
    w_dict = dict(zip(words, s_len))
    # 倒序
    result = sorted(w_dict.items(), key=lambda x: x[1], reverse=True)
    return [x[0] for x in result]


assert sort_strings_by_vowels(["aa", "eee", "oo", "iiii"]) == ["iiii", "eee", "aa", "oo"]
assert sort_strings_by_vowels(["ioue", "ee", "uoiea"]) == ["uoiea", "ioue", "ee"]
assert sort_strings_by_vowels(["AIBRH", "", "YOUNG", "GREEEN"]) == ["GREEEN", "AIBRH", "YOUNG", ""]
def sort_strings_by_vowels(words: list) -> list:
    counts = []
    for word in words:
        count = 0
        if word:
            for letter in word:
                if letter.upper() in "AEIOU":
                    count += 1
        counts.append(count)
    # 合并 key和对应元音字母出现次数
    res = list(zip(words, counts))
    # 按count值降序排序
    res.sort(key=lambda x: x[1], reverse=True)
    # 取出key转成列表
    return list(list(zip(*res))[0])


assert sort_strings_by_vowels(["aa", "eee", "oo", "iiii"]) == ["iiii", "eee", "aa", "oo"]
assert sort_strings_by_vowels(["ioue", "ee", "uoiea"]) == ["uoiea", "ioue", "ee"]
assert sort_strings_by_vowels(["AIBRH", "", "YOUNG", "GREEEN"]) == ["GREEEN", "AIBRH", "YOUNG", ""]

练习

def sort_strings_by_vowels(words: list) -> list:
    vowels_set = {'a', 'e', 'i', 'o', 'u'}
    words_with_max_contiguous_vowels_num_dict = dict()
    # 先统计每个单词或句子的最大连续元音数
    for word in words:

        max_contiguous_vowels_num = 0
        current_max_contiguous_vowels_num = 0
        for c in word.lower():
            if c in vowels_set:
                current_max_contiguous_vowels_num += 1
                if current_max_contiguous_vowels_num > max_contiguous_vowels_num:
                    max_contiguous_vowels_num = current_max_contiguous_vowels_num
            else:
                current_max_contiguous_vowels_num = 0
        words_with_max_contiguous_vowels_num_dict[word] = max_contiguous_vowels_num

    # 随便选一个排序算法,这里是冒泡
    words_length = len(words)
    for i in range(words_length-1, 0, -1):
        for j in range(i):
            if words_with_max_contiguous_vowels_num_dict[words[j]] < words_with_max_contiguous_vowels_num_dict[words[j + 1]]:
                words[j],  words[j + 1] = words[j + 1],  words[j]

    return words



assert sort_strings_by_vowels(["aa", "eee", "oo", "iiii"]) == ["iiii", "eee", "aa", "oo"]
assert sort_strings_by_vowels(["ioue", "ee", "uoiea"]) == ["uoiea", "ioue", "ee"]
assert sort_strings_by_vowels(["AIBRH", "", "YOUNG", "GREEEN"]) == ["GREEEN", "AIBRH", "YOUNG", ""]
    def sort_strings_by_vowels(words: list) -> list:
        vowel = "aeiouAEIOU"
        words_lis = []
        for i in words:
            key = 0
            key_max = 0
            for j in i:
                if j in vowel:
                    key += 1
                    if key_max < key:
                        key_max = key
                else:
                    key = 0
            words_lis.append((i,key_max))
        words_lis.sort(key=lambda x: x[1],reverse=True)
        return [i[0] for i in words_lis]
def sort_strings_by_vowels(words:list) -> list:
    resoult = {}
    for i in words:
        resoult[i] = get_max(i)
    # items() 方法的遍历:items() 方法把字典中每对 key 和 value 组成一个元组,并把这些元组放在列表中返回
    # reverse = True 降序 , reverse = False 升序(默认)
    resoult = sorted(resoult.items(), key=lambda x: x[1], reverse=True)
    return [i[0] for i in resoult]




def get_max(my_string: str):
    """
    获取字符串中出现连续的最大元音数
    :param s: 字符串
    :return: 最大元音数
    """
    temp = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']

    a = 0
    # 定义一个列表all_list接收元音所有连续出现的值;
    all_list = []
    # 定义一个变量get_max,用于接收元音每一次连续的值;
    get_max = 0
    while a < len(my_string):
        if my_string[a] in temp:
            get_max += 1
        else:
            all_list.append(get_max)
            get_max = 0
        a += 1
    # 取出列表中的最大值
    return max(all_list)
import re
from itertools import groupby
from operator import itemgetter


def sort_strings_by_vowels(words):
    vowels_letter = list('aeiouAEIOU')


    word_indexs = {}
    for word in words:
        indexs = [i for letter in vowels_letter for i in range(len(word)) if word[i] == letter]
        indexs.sort()
        word_indexs[word] = indexs
  

    results_dict = {}
    for word,indexs in word_indexs.items():
        max_lenth = []
        if indexs != []:
            for k, g in groupby(enumerate(indexs), lambda x: x[1] - x[0]):
                max_lenth.append(len(list(map(itemgetter(1), g))))

            results_dict[word] = max(max_lenth)
        else:
            results_dict[word] = 0
   
    return list(dict(sorted(results_dict.items(),key=lambda x: x[1], reverse=True )).keys())

def sort_strings_by_vowels(words: list) -> list:
    words.sort(key=lambda x:len(x),reverse=True)
    return words
def sort_strings_by_vowels(words: list) -> list:
    dic={}
    base_list=['a', 'e', 'i', 'o', 'u']
    for word in words:
        word_list=[]
        word1=word.lower()
        for i in range(len(word1)):
            if word1[i] in base_list:
                if not word_list:
                    word_list.append([word1[i]])
                    continue
                if word1[i] in base_list and word1[i - 1] in base_list:
                    word_list[-1].append(word1[i])
        if not word_list:
            dic[word] = 0
        else:
            dic[word] = max([len(i) for i in word_list])
    return [i[0] for i in sorted(list(dic.items()), key=lambda x: x[1], reverse=True)]


assert sort_strings_by_vowels(["aa", "eee", "oo", "iiii"]) == ["iiii", "eee", "aa", "oo"]
assert sort_strings_by_vowels(["ioue", "ee", "uoiea"]) == ["uoiea", "ioue", "ee"]
assert sort_strings_by_vowels(["AIBRH", "", "YOUNG", "GREEEN"]) == ["GREEEN", "AIBRH", "YOUNG", ""]