简体   繁体   English

确保每个字母在密码中仅出现一次

[英]Ensuring that each letter appears only once in the password

Just starting learning python and trying to use loop to write codes checking unique letters as follows: 刚开始学习python并尝试使用循环编写检查唯一字母的代码,如下所示:

def has_unique_letters(x):
  count = 0
  letters = set(x)
  while count < len(x):
    if x[count] in letters:
       return False
    count += 1
  return True

However no matter what I input in x, it returns false. 但是,无论我在x中输入什么,它都将返回false。 What's wrong with the code? 代码有什么问题? Thanks! 谢谢!

Every letter of x will always be contained in the set(letters of x) , therefore your function will always return False x每个字母将始终包含在set(letters of x) ,因此您的函数将始终返回False

To solve your problem, you could verify that the length of set(x) is equal to the length of x : 为了解决您的问题,您可以验证set(x)的长度等于x的长度:

here is why: 这是为什么:

  1. A set is a collection of unique objects. 集合是唯一对象的集合。
  2. The set of the elements of x, is the collection of the unique elements composing x. x的元素集是组成x的唯一元素的集合。
  3. Therefore, if x is composed exclusively of unique elements, the cardinality of the set of the elements of x will be equal to the total number of elements composing x. 因此,如果x仅由唯一元素组成,则x元素集的基数将等于组成x的元素总数。

def has_unique_letters(x):
    return len(set(x)) == len(x)

it returns True if x contains only unique letters, and False otherwise 如果x仅包含唯一字母,则返回True ,否则返回False

[edit] - question about indifference to lower/upper case: [edit]-对小写/大写无动于衷的问题:

If upper & lower case letters in the pw are equivalent, it is necessary to transform the input parameter to lower (or upper) case, prior to processing: 如果pw中的大写和小写字母等效,则在处理之前,必须将输入参数转换为小写(或大写):

def has_unique_letters(x):
    lower_x = x.lower()
    return len(set(lower_x)) == len(lower_x)

[edit] placing a unique letter constraint on a password reduces the domain space; [edit]在密码上设置唯一字母约束会减少域空间; maybe it is not such a great idea. 也许这不是一个好主意。

Here is another approach: 这是另一种方法:

def has_unique_letters(string):
    return not bool([x for x in __import__('collections').Counter(string).values() if x > 1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅将字符串中的每个字母打印一次 - How to print each letter in a string only once 我需要只出现一次的元素 - I need the element that appears only occur once 计算每个字母在文本样本中出现的次数 - Count how many times each letter appears in a text sample 如何查找每个字母在名称中出现的次数? - How to find how many times each letter appears in a name? 返回一个字母在一个字符串中出现多少次 - Return how many times each letter of the alphabet appears in a string 计算每个字母在字符串中出现的次数 - Counting how many times each letter appears in the string 你可以用给定单词的字母制作4个字母或更多的常用英文单词(每个字母只能使用一次) - How many common English words of 4 letters or more can you make from the letters of a given word (each letter can only be used once) 尝试仅使用while循环来计算字母出现的次数 - Trying to count the number of times a letter appears using only a while loop 确保__init__仅在构造函数或__new__创建类实例时调用一次 - Ensuring __init__ is only called once when class instance is created by constructor or __new__ 计算列表中字符串的字母并输出每个字母在每个字符串中出现的数量 - Counting the letters of a string in a list and outputting the amount each letter appears in each string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM