CodeWar打怪升级-Python篇

本文分享了两个Python编程挑战的解决方案。第一个挑战是将字符串中的每个字符转换为括号,根据其在原始字符串中出现的次数决定使用左括号还是右括号,忽略大小写。第二个挑战是找出列表中最小的两个正整数并返回它们的和,同时对输入数据进行预处理,移除负数和非整数。

1.  The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.

Examples

"din"      =>  "((("
"recede"   =>  "()()()"
"Success"  =>  ")())())"
"(( @"     =>  "))((" 

My answer

大小写转换函数、for循环、replace函数

def duplicate_encode(word):
    
    count={}
    for i in word.lower():
        if i not in count:
            count[i]=1
        else:
            count[i]+=1
    
    new_word=word.lower()
    for i in new_word:
        if count[i]>1:
            new_word=new_word.replace(i,')')
        else:
            new_word=new_word.replace(i,'(')
    return new_word

  

 2

 

 

 

 

 

def sum_two_smallest_numbers(numbers):
    for i in range(0,len(numbers)-1):
        if (not isinstance(numbers[i],int)) or numbers[i]<0:
            numbers.remove(numbers[i])
    new_num=numbers.sort()
    sum=0
    sum=sum+int(new_num[0])+int(new_num[1])
    return sum

  

 

 

 
posted on 2019-06-12 10:42 Suckseedeva 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/skyEva/p/11008185.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值