【每日一题20220215】消除连续重复

:woman_mage: 给定一段包含英文单词的文本,其中每个单词之间使用空格分隔,我们的任务就是消除其中的出现连续重复的单词,返回最终的结果字符串。

示例:
输入:alpha beta beta gamma,输出:alpha beta gamma

题目难度:简单
题目来源:CodeWars:Remove consecutive duplicate words

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

assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
from itertools import groupby

def solution(s: str) -> str:
    # your code here
    return ' '.join(k for k, _ in groupby(s.split()))
2 个赞
def solution(s) -> str:
    str_list = s.split(' ')
    return ' '.join(
        str_list[:1] + [str_list[i] for i in range(len(str_list)) if i > 0 and str_list[i - 1] != str_list[i]])


assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
def solution(s) -> str:
    s1 =s.split(' ')
    s2 = []
    for i in s1:
        if i not in s2:
            s2.append(i)
    str = ' '.join(s2)
    return str
def solution(words: str) -> str:
    # 将字符串转换为列表
    words_list = words.split(' ')
    # 复制列表
    words_result = [x for x in words_list]
    for i in range(len(words_list)-1):
        if words_list[i] == words_list[i+1]:
            # 列表中删除第一个等于words_list[i]的元素
            words_result.remove(words_list[i])
    # 返回结果,列表转换为字符串
    return ' '.join(words_result)

assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
def solution(mystr:str) -> str:
    list_new = []
    list1 = mystr.split(" ")
    list_new.append(list1[0])
    i = 0
    while i <= len(list1)-2:
        if list1[i] != list1[i+1]:
            list_new.append(list1[i+1])
        i=i+1
    return " ".join(list_new)


assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"

要求只消除连续重复的,那如果words=“beta alpha beta beta gamma”,结果就不对了

1 个赞
def solution(s: str) -> str:
    s_list = s.split(" ")
    res = []
    for i in range(len(s_list) - 1):
        if s_list[i] == s_list[i + 1]:
            continue
        else:
            res.append(s_list[i])
    else:
        res.append(s_list[-1])
    return " ".join(res)

谢谢指出错误

def solution(words: str) -> str:
    i = 0
    # 将字符串转换为列表
    words_list = words.split(' ')
    while i < len(words_list) - 1:
        if words_list[i] == words_list[i + 1]:
            del words_list[i + 1]
            continue
        else:
            i += 1
    # 返回结果,列表转换为字符串
    return ' '.join(words_list)


assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("beta alpha beta beta gamma") == "beta alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
def solution(string) -> str:
    datas = string.split(' ')
    n = len(datas)
    for i in range(n - 1, 0, -1):
        if datas[i] == datas[i - 1]:
            del datas[i]
    return ' '.join(datas)


assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
def solution(sen: str) -> str:
    words = sen.split()
    reduced_words = []
    for word in words:
        if word not in reduced_words:   reduced_words.append(word)
    return ' '.join(reduced_words)
    # your code here


assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"

def solution(words:str) -> str:
    # your code here
    words_list = words.split(" ")
    new_words = [words_list[0]]
    i, j = 0, 0
    for _ in range(len(words_list)-1):
        j += 1
        if words_list[i] != words_list[j]:
            new_words.append(words_list[j])
            i = j
    return " ".join(new_words)

assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
def solution(line:str) -> str:
    words = line.split(' ')
    new_line = []
    for w in words:
        if len(new_line)==0 or w != new_line[-1]:
            new_line.append(w)

    return ' '.join(new_line)

assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
assert solution("hogwarts is is is the best best is world") == "hogwarts is the best is world"
def solution(str_words: str) -> str:
    res_list = []
    for words in str_words.split(' '):
        if words not in res_list:
            res_list.append(words)
    return ' '.join(res_list)


assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("hogwarts is is is the best best") == "hogwarts is the best"
def solution(text) -> str:
    new_text = text.split()
    i = 0
    while i < len(new_text)-1:
        if new_text[i] == new_text[i+1]:
            del new_text[i]
        else:
            i = i+1
    print('new_text:',new_text)
    return ' '.join(new_text)

assert solution("a b b b c d d d d") == "a b c d"
assert solution("alpha beta beta gamma") == "alpha beta gamma"
assert solution("is hogwarts is is is the best best") == "is hogwarts is the best"