【每日一题20220302】小写元音

:mage:t2: 给定一个数字列表,请编写一个函数,判断其中的数字是否是小写元音字母(a, e, i, o, u)在ASCII表中的数字。如果是,则将对应数字转成对应的字符串格式的字母。最终返回翻译后的列表。

示例:
输入:[100,100,116,105,117,121]
输出:[100,100,116,"i","u",121]

题目来源:CodeWars:Is there a vowel in there?

def solution(nums: list) -> list:
    # your code here

assert solution([100,100,116,105,117,121]) == [100,100,116,"i","u",121]
assert solution([118,103,110,109,104,106]) == [118,103,110,109,104,106]
assert solution([118,117,120]) == [118, "u",120]
def solution(nums: list) -> list:
    # your code here
    words_list = ["a", "e", "i", "o", "u"]
    for i in range(len(nums)):
        for j in words_list:
            if nums[i] == ord(j):
                nums[i] = j
    return nums


assert solution([100, 100, 116, 105, 117, 121]) == [100, 100, 116, "i", "u", 121]
assert solution([118, 103, 110, 109, 104, 106]) == [118, 103, 110, 109, 104, 106]
assert solution([118, 117, 120]) == [118, "u", 120]
def solution(nums: list) -> list:
    # 0302 小写元音
    words = ['a', 'e', 'i', 'o', 'u']
    ord_list = [ord(word) for word in words]
    print(ord_list)
    res = []
    for num in nums:
        if num in ord_list:
            res.append(words[ord_list.index(num)])
        else:
            res.append(num)
    print(res)
    return res

assert solution([100,100,116,105,117,121]) == [100,100,116,"i","u",121]
assert solution([118,103,110,109,104,106]) == [118,103,110,109,104,106]
assert solution([118,117,120]) == [118, "u",120]

def solution(nums: list) -> list:
    match_list = ["a", "e", "i", "o", "u"]
    for i in range(len(nums)):
        if chr(nums[i]) in match_list:
            nums[i] = chr(nums[i])
    return nums

assert solution([100,100,116,105,117,121]) == [100,100,116,"i","u",121]
assert solution([118,103,110,109,104,106]) == [118,103,110,109,104,106]
assert solution([118,117,120]) == [118, "u",120]
def solution(nums: list) -> list:
    match_list = ["a", "e", "i", "o", "u"]
    for i in range(len(nums)):
        if chr(nums[i]) in match_list:
            nums[i] = chr(nums[i])
    return nums

def solution(nums: list) -> list:
    dict={97:"a",101:"e",105:"i",111:"o",117:"u"}
    nums = [nums[i] if dict.get(nums[i]) is None else dict.get(nums[i]) for i in range(len(nums))]
    return nums

assert solution([100,100,116,105,117,121]) == [100,100,116,"i","u",121]
assert solution([118,103,110,109,104,106]) == [118,103,110,109,104,106]
assert solution([118,117,120]) == [118, "u",120]
def solution(nums: list) -> list:
    s = ["a","e","i","o","u"]
    result = []
    for i in nums:
        if chr(i) in s:
            result.append(chr(i))
        else:
            result.append(i)
    return result

assert solution([100,100,116,105,117,121]) == [100,100,116,"i","u",121]
assert solution([118,103,110,109,104,106]) == [118,103,110,109,104,106]
assert solution([118,117,120]) == [118, "u",120]

def solution(nums: list) -> list:
   #ord将一个字符串转化为成它的整数值
   li = [ord(i) for i in ('a', 'e', 'i', 'o', 'u')]
   for j in nums:
      if j in li:
         #计算出此时j的索引值
         n = nums.index(j)
         #chr将一个整数转换为字符串
         nums[n] = chr(j)
   return nums
assert solution([100, 100, 116, 105, 117, 121]) == [100, 100, 116, "i", "u", 121]
assert solution([118, 103, 110, 109, 104, 106]) == [118, 103, 110, 109, 104, 106]
assert solution([118, 117, 120]) == [118, "u", 120]
def solution(nums: list) -> list:
    for i in range(len(nums)):
        if chr(nums[i]) in ['a', 'e', 'i', 'o', 'u']:
            nums[i] = chr(nums[i])
    return nums
    #return [chr(i) if chr(i) in 'aeiou' else i for i in nums ]
关闭