Python 练习题总结

1.Given an array of one's and zero's convert the equivalent binary value to an integer.

   Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1

  常规解法:

def binary_array_to_number(arr):
  # your code
    n=len(arr)
    res=0
    for i in range(n):
        res=res+arr[i]*pow(2,n-1-i)
    return res

   新颖解法:

def binary_array_to_number(arr):
    return int("".join(map(str,arr)),2)

2.Count the number of Duplicates

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.

Example

"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'

"ABBA" -> 2 # 'A' and 'B' each occur twice

普通解法:

def duplicate_count(text):
    # Your code goes here
    res=text.lower()
    s=0
    while(len(res)>0):
        a=res[0]
        if res.count(a)>1:
            s=s+1
        res=res.replace(a,"")
    return s

新奇解法:

def duplicate_count(s):
  return len([c for c in set(s.lower()) if s.lower().count(c)>1])

3.Create a function named divisors/Divisors that takes an integer and returns an array with all of the integer's divisors(except for 1 and the number itself). If the number is prime return the string '(integer) is prime' (null in C#) (use Either String a in Haskell and Result<Vec<u32>, String> in Rust).

Example:
divisors(12); #should return [2,3,4,6]
divisors(25); #should return [5]

divisors(13); #should return "13 is prime"

普通解法:

def divisors(integer):
    arr=[]
    for i in range(2,integer):
        if integer%i==0:
            arr.append(i)
    if len(arr)>0:
        return arr
    else:
        return "%d is prime"%integer

新颖解法:

def divisors(num):
    l = [a for a in range(2,num) if num%a == 0]
    if len(l) == 0:
        return str(num) + " is prime"
    return l
4. Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.

Examples:

spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" 
spinWords( "This is a test") => returns "This is a test" 

spinWords( "This is another test" )=> returns "This is rehtona test"

普通解法:

def spin_words(sentence):
    arr=[a for a in sentence.split(" ")]
    m=[]
    for i in arr:
        if len(i)>=5:
            m.append(i[::-1])
        else:
            m.append(i)
    res=" ".join(a for a in m)
    return res
新颖解法:
def spin_words(sentence):
    # Your code goes here
    return " ".join([x[::-1] if len(x) >= 5 else x for x in sentence.split(" ")])
5,Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number.
Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case

普通解法:

def countBits(n):
    bits=0
    while n!=0:
        if n%2!=0:
            bits=bits+1
        n=n//2
            
    return bits
新颖解法:
def countBits(n):
    return bin(n).count("1")


更多python练习题以及学习教程,请关注本人公众号:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值