【问题标题】:Checking for duplicate letters within lists by using a histogram function使用直方图函数检查列表中的重复字母
【发布时间】:2019-10-23 01:18:13
【问题描述】:

我正在尝试完成我的作业,但在整合所需的直方图函数时遇到了困难。

这是我必须使用的代码:

alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]

def histogram(s):
     d = dict()
     for c in s:
          if c not in d:
               d[c] = 1
          else:
               d[c] += 1
     return d

我需要编写一个名为has_duplicates() 的函数,它接受一个字符串参数,如果字符串有任何重复字符,则返回True。否则,它应该返回False

通过使用上面的histogram() 函数创建直方图来实现has_duplicates()。不要使用教科书中给出的has_duplicates() 的任何实现。相反,您的实现应该使用直方图中的计数来确定是否有任何重复。

在提供的test_dups 列表中的字符串上编写一个循环。根据该字符串的 has_duplicates() 的返回值打印列表中的每个字符串以及它是否有任何重复项。例如,aaaabc 的输出将如下所示。

aaa has duplicates
abc has no duplicates

为 test_dups 中的每个字符串打印一条类似上述的行。

编写一个名为missing_letters 的函数,它接受一个字符串参数并返回一个新字符串,其中包含不在参数字符串中的所有字母。返回字符串中的字母应按字母顺序排列。

我的实现应该使用来自histogram() 函数的直方图。它还应该使用全局变量alphabet。它应该直接使用这个全局变量,而不是通过参数或本地副本。它应该遍历alphabet 中的字母以确定输入参数中缺少哪些字母。

函数missing_letters 应该将丢失的字母列表组合成一个字符串并返回该字符串。

在列表 test_miss 中的字符串上编写一个循环,并为每个字符串调用 missing_letters。为列出缺失字母的每个字符串打印一行。例如,对于字符串“aaa”,输出应如下所示。

aaa 缺少字母 bcdefghijklmnopqrstuvwxyz

如果字符串包含alphabet 中的所有字母,则输出应显示它使用了所有字母。例如,字符串 alphabet 本身的输出将如下所示。

"abcdefghijklmnopqrstuvwxyz uses all the letters"

为 test_miss 中的每个字符串打印一条类似上述的行。

据我所知……

def has_duplicates(t):
    if histogram(t) > 1:
        return True
    else:
        return False

结果:

'>' not supported between instances of 'str' and 'int'

【问题讨论】:

  • 我意识到我肯定缺乏对这里的一般概念的理解,但如果有人可以提供一些指导,我将不胜感激。不是要求您为我完成作业,因为我真的很想学习如何,但是我将如何开始将直方图值转换为 int 以便 has_duplicates 函数成功执行?
  • 你有没有测试过他们给你的代码,看看它会返回什么?
  • 我不确定你是如何得到这个错误的。您发布的直方图函数返回 dict
  • IMO 你需要edit 你的问题并缩小你所问的范围,因为就目前而言,听起来你想要整个作业的代码。
  • 您的直方图返回一个字典。您必须遍历这些值并检查是否有任何 > 1。(例如 def has_duplicates(t): return any(v > 1 for k, v in histogram(t).items())

标签: python list dictionary histogram


【解决方案1】:

以下内容应提供所需的结果:

alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]

def histogram(s):
     d = dict()
     for c in s:
          if c not in d:
               d[c] = 1
          else:
               d[c] += 1
     return d

def has_duplicates(s):
    # Return False if each letter in s is not distinct
    return len(histogram(s)) != len(s)

def missing_letters(s):
    h = histogram(s)
    rv = ''
    # Loop over letters in alphabet, if the letter is not in the histogram then
    # append to the return string.
    for c in alphabet:
        if c not in h:
            rv = rv + c
    return rv

# Loop over test strings as required.
for s in test_miss:
    miss = missing_letters(s)
    if miss:
        print(f"{s} is missing letters {miss}.")
    else:
        print(f"{s} uses all the letters.")

输出:

zzz is missing letters abcdefghijklmnopqrstuvwxy.
subdermatoglyphic is missing letters fjknqvwxz.
the quick brown fox jumps over the lazy dog uses all the letters.

【讨论】:

    【解决方案2】:
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    test_dups = ["zzz", "dog", "bookkeeper", "subdermatoglyphic", "subdermatoglyphics"]
    test_miss = ["zzz", "subdermatoglyphic", "the quick brown fox jumps over the lazy dog"]
    
    
    def histogram(string):
        d = dict()
        for char in string:
            if char not in d:
                d[char] = 1
            else:
                d[char] += 1
        return d
    
    
    # Part 1
    def has_duplicate(string):
        h = histogram(string)
        for k, v in h.items():
            if v > 1:
                return True
        return False
    
    
    for string in test_dups:
        if has_duplicate(string):
            print(string, "has duplicates")
        else:
            print(string, "has no duplicates")
    
    # Part 2
    
    
    def missing_letters(string):
        h = histogram(string)
        new_list = []
        for char in alphabet:
            if char not in h:
                new_list.append(char)
        return "".join(new_list)
    
    
    for string in test_miss:
        new_list = missing_letters(string)
        if len(new_list):
            print(string, "is missing letters", new_list)
        else:
            print(string, "uses all letters")
    

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 2012-03-23
      • 1970-01-01
      • 2021-06-28
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 2023-03-20
      相关资源
      最近更新 更多