小白的自我救赎:Codewars第三天

题目:
An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
就是不区分字符串中字母大小写的情况下判断字符串中有没有重复的字母

我的代码:

def is_isogram(string):
    string=string.lower()
    if len(string) == 0:
        return True
    else:
        newstring=list(string)
        for i in range(len(newstring)-1):
            for j in range(i+1,len(newstring)):
                if newstring[i] == newstring[j]:
                    return False
        return True
                    

哎 简直太复杂了
看看别人的代码

def is_isogram(string):
    temp_list = []
    for item in list(string):
        item = item.lower()
        if item in temp_list:
            return False
        else:
            temp_list.append(item)
    return True

还有更厉害的

def is_isogram(string):
    s = set(string.lower()) 
    if len(s) == len(string): 
        return True
    return False

灵活运用集合

题目:
Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.
统计字符串中有几个单词有重复,不必考虑大小写

分析:
已经说了不考虑大小写,那么先全部小写。
统计重复单词的个数:把有重复的单词扔到列表里最后求列表的长度就知道有几个重复的单词了

def duplicate_count(text):
	string = text.lower()
	output = []
	for i in string:
		if string.count(i)>= 2 and i not in output:
			output.append(i)
	return len(output)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值