【每日一题20220225】恢复单词

:woman_mage: 已知一个原始英文单词,给定一个字符串列表,其中的元素基本是这个原始单词的镜像,也就是除了个别字母丢失外基本相同,丢失的字母用*号表示。

我们的任务是,编写一个函数,接收这个字符串列表,并恢复出原始字符串。如果无法确定字母,那么使用#表示。

示例:
输入: ["a*cde","*bcde","abc*e"],输出: "abcde"
输入: ["a*c**", "**cd*", "a*cd*"],输出: "a#cd#"

题目难度:中等
题目来源:CodeWars:Assemble string

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

assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"
def solution(words: list) -> str:
    b_list = ["#" for x in range(len(words[0]))]
    for text_word in words:
        for i in range(len(words[0])):
            if text_word[i] != "*":
                b_list[i] = text_word[i]
            else:
                continue

    str_word = "".join(x for x in b_list)
    return str_word

assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"
1 个赞
def solution(words: list) -> str:
    # your code here
    m = ["#" for x in range(len(words[0]))]
    for i in range(0, len(words)):
        for j, h in enumerate(words[i]):
            if m[j] == '*'or m[j] == '#':
                if h == '*':m[j] = '#'
                else:m[j] = h
    return ''.join(i for i in m)
def solution(words: list) -> str:
    result = []
    for i in range(len(words[0])):
        if words[0][i] != '*':
            result.append(words[0][i])
        elif words[1][i] != '*':
            result.append(words[1][i])
        elif words[2][i] != '*':
            result.append(words[2][i])
        else:
            result.append('#')
    return ''.join(result)

assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"
def solution(words: list) -> str:
    # your code here
    list_word = ["#" for o in range(len(words[0]))]
    for i in words:
        for j in range(len(i)):
            if i[j] != "*":
                list_word[j] = i[j]
    words_str = "".join(list_word)
    return words_str


assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"
def solution(words: list) -> str:
   res = ["#" for x in range(len(words[0]))]
    for word in words:
        for i in range(len(res)):
            if word[i] != '*':
                res[i] = word[i]

    res = ''.join(i for i in res)
    print(res)
    return res

assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"
def solution(words: list) -> str:
    words_list = []
    for i in range(len(words[0])):
        for j in range(len(words)):
            if words[j][i] != '*':
                words_list.append(words[j][i])
                break
            if words[len(words)-1][i] == '*':
                words_list.append('#')
                break
    return ''.join(words_list)
def solution(words:list) -> str:
    result = []
    j = 0
    flag = 1
    while flag:
        for i in range(len(words)):
            while j < len(words[i]):
                if words[i][j].isalpha():
                    result.append(words[i][j])
                    j += 1
                else:
                    break
        if len(result) < len(words[0]):
            result.append("#")
        j += 1
        if j > len(words[i]):
            flag = 0
    return "".join(result)

assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"
def solution(words: list) -> str:
    new_words = ['#'] * len(words[0])
    for word in words:
        for k, v in enumerate(word):
            if (v != "*"):
                if(new_words[k]== "#"):
                    new_words[k] = v
    return "".join(new_words)

assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"
import re
def solution(words):
    if words == []:
        return ''
    else:
        indexs = [i.start() for i in re.finditer('\*', words[0])]
        datas = [x for x in words[0]]
        for i in range(1, len(words)):
            for ind in indexs:
                if words[i][ind] != '*':
                    datas[ind] = words[i][ind]
        _datas = ''.join(datas)

        return re.sub('\*','#', _datas)

def solution(words: list)->str :
    list_result = ['#' for i in range(len(words[0]))]
    for i in words:
        for j in range(len(i)):
            if i[j]!='*':
                list_result[j]=i[j]
    return ''.join(list_result)
def get_work(words: list) -> str:
    # 找出单词中包含*最少的单词;
    my_dict = {}
    for i in words:
        my_dict[i.count("*")] = i
    new_list = sorted(my_dict.items(), key=lambda x: x[0])
    new_word = [i[1] for i in new_list]
    word_str = list(new_word[0])
    j = 0
    max_ = 0
    for i in new_word[0]:
        if i == "*":
            for a in words:
                if a[j] != "*":
                    word_str[j] = a[j]
                    max_ += 1
                    if max_ == new_word[0].count("*"):
                        return "".join(word_str)
        j += 1
    word_str = "".join(word_str)
    return word_str.replace("*", "#")
    def solution(words: list) -> str:
        word = list(words[0])
        for i in words:
            for j in range(len(i)):
                if i[j] != '*':
                    word[j] = i[j]
        for i in range(len(word)):
            if word[i] == "*":
                word[i] = '#'
        return ''.join(word)
def solution(words: list) -> str:
    # your code here
    entire_word=[]
    for i in range(len(words[0])):
        for j in words:
            if j[i] != '*':
                entire_word.append(j[i])
                break
        else:
            entire_word.append('#')
    return ''.join(entire_word)

def solution(words: list) -> str:
    new_word = ''
    for item in zip(*words):
        for i in range(len(item)):
            if item[i] != '*':
                new_word += item[i]
                break
        else:
            new_word += '#'
    return new_word
def solution(words: list) -> str:
    result = ""
    for idx in range(len(words[0])):
        for i, word in enumerate(words):
            if word[idx] != "*":
                result += word[idx]
                break
            if i == len(words) - 1:
                result += "#"
    return result


assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"

def solution(words: list) → str:
arr = [‘#’ for i in range(len(words[0]))]
for word in words:
for i in range(len(word)):
if ‘#’ not in arr:
return ‘’.join(arr)
if word[i] != ‘*’ and arr[i] == ‘#’:
arr[i] = word[i]
return ‘’.join(arr)

assert solution([“acde", “bcde", "bcde"]) == “abcde”
assert solution(["a
c
*”, “*cd”, "a
cd*”]) == “a#cd#”
assert solution([“", "”, “", "”]) == “######”

def solution(words: list) -> str:
    res_list=[a for a in len(words[0])*'*']
    for i in range(len(words)):
        for j in range(len(words[i])):
            if words[i][j].islower():
                res_list.pop(j)
                res_list.insert(j,words[i][j])
            else:
                if not res_list[j].islower():
                    res_list.pop(j)
                    res_list.insert(j,'#')
    return ''.join(res_list)

assert solution(["a*cde", "*bcde", "*bcde"]) == "abcde"
assert solution(["a*c**", "**cd*", "a*cd*"]) == "a#cd#"
assert solution(["******", "******", "******", "******"]) == "######"