见贤思小齐,知足常乐呵

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

  

 3.   There is an array with some numbers. All numbers are equal except for one. Try to find it!

find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55
  • Answer : 
  • 列表对象的set函数和 count函数
def find_uniq(arr):
     a, b = set(arr)  ## set(列表参数)函数是 生成一个不重复元素集合,
     return a if arr.count(a) == 1 else b ## arr.count 是列表函数

  

 

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