【每日一题1022】冗余编译器

:woman_mage:给定一个字符串,请编写一个函数,检测其中的字符,如果只出现1次则编译成"(“,如果出现多次则编译成”)"。忽略字母的大小写。

示例
输入:" din",输出:" ((("
输入:“Success”:,输出:" )())())"

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

def duplicate_encode(word: str) -> str:
    pass

assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("
def duplicate_encode(word: str) -> str:
    tmp = word.lower()
    result = ""
    for i in tmp:
        if tmp.count(i)==1:
            result = result + "("
        else:
            result = result + ")"
    return result
def duplicate_encode(word: str) -> str:
    return ''.join(['(' if word.lower().count(w) == 1 else ')' for w in word.lower()])


assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("
1 Like
def duplicate_encode(word: str) -> str:
    words = []
    duplicate_words = []
    result = ""
    for i in word:
        i = i.lower()
        if i not in words:
            words.append(i)
        else:
            duplicate_words.append(i)
    for i in word:
        i = i.lower()
        if i not in duplicate_words:
            result += "("
        else:
            result += ")"
    return result


assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("
def duplicate_encode(word: str) -> str:
    c=word.lower()
    for i in c:
        if c.count(i)>1:
            c=c.replace(i,')')
        else:
            c=c.replace(i,'(')
    return c        

assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("
def duplicate_encode(word: str) -> str:
    # 定义一个返回值变量
    result = ''
    # 忽略字母的大小写
    word = word.upper()
    # 遍历字符串中的每个字符
    for w in word:
        # 计算统计字符的出现次数
        count = word.count(w)
        if count > 1:
            # 出现多次则编译成")"
            result = result + ')'
        else:
            # 只出现1次则编译成"("
            result = result + '('
    # 返回编译后的字符串
    return result

assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("
def duplicate_encode(word: str) -> str:
    word = word.upper()
    newstr = ""
    for i in word:
        if word.count(i) > 1:
            newstr = newstr + ")"
        else:
            newstr = newstr + '('
    return newstr


assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("

貌似写的复杂了

def duplicate_encode(word):
    start = 0
    end = len(word) - 1
    while start < len(word) - 1:
        for index in range(end, start, -1):
            if word[start].lower() == word[index].lower():
                word = word.replace(word[start], ')')
                word = word.replace(word[index], ')')
        start += 1
    for index in range(0, len(word)):
        if word[index] != ')':
            word = word.replace(word[index], "(")
    return word


assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("

Python参考题解:

def duplicate_encode(word: str) -> str:
    word = word.lower()
    ct = ["(" if word.count(x)==1 else ")" for x in word]
    return "".join(ct)

assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("

思路:利用列表推导式,遍历字符串word,通过count方法统计所遍历的字符的出现次数,如果是1次则转成"(",如果是多次则转成")",一步到位,最后将列表元素通过字符串的join方法拼接起来。

知识点:

  1. 列表推导式
  2. 字符串常用方法:lower()count()join()

Java 参考题解:

public class DuplicateEncoder {
  static String encode(String word){
    word = word.toLowerCase();
    String result = "";
    for (int i = 0; i < word.length(); ++i) {
      char c = word.charAt(i);
      result += word.lastIndexOf(c) == word.indexOf(c) ? "(" : ")";
    }
    return result;
  }
}
def duplicate_encode(word: str) -> str:
    sL = list(word.lower())
    result =[]
    for item in sL:
        if sL.count(item) ==1:
           result.append("(")
        else:
            result.append(")")
    return ''.join(result)

assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("
def duplicate_encode(word: str) -> str:
    word1=word.lower()
    list1=[]
    for i in word1:
        if word1.count(i)==1:
            list1.append('(')
        else:
            list1.append(')')
    return ''.join(list1)


assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("

    public String duplicateEncode(String str) {
        String res = "";
        char[] chars = str.toLowerCase().toCharArray();
        for (Character c : chars) {
            res += str.indexOf(c) == str.lastIndexOf(c) ? "(" : ")";

        }
        return res;
    }
def duplicate_encode(word: str) -> str:
    s = word.lower()
    result = []
    for i in s:
        if s.count(i) == 1:
            result.append('(')
        else:
            result.append(")")
    return ''.join(result)

assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("
def duplicate_encode(words: str) -> str:
    result = []
    for word in words:
        if words.upper().count(word.upper()) > 1:
            result.append(")")
        else:
            result.append("(")
    return "".join(result)


assert duplicate_encode("din") == "((("
assert duplicate_encode("Success") == ")())())"
assert duplicate_encode("(( @") == "))(("