1. Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition.

def add_binary(a,b):
    return str(bin(a+b))[2:]  # 字符串切片,
print(add_binary(1,1))
View Code

字符串切片:

[:end] //省略start,默认从起始偏移量start、即下标0开始,一直到偏移end处,记住一定是不包含end下标的字符的哦

[start:] //省略end,默认从下标start开始,一直到省略到下标end处,end的默认值是字符串的长度或者为-1【错误的】(因为不包含最后一个end位置的下标,所以输出整个字符串,但是肯定end的默认值不可能是-1,那样就不会输出整个字符串了)

[start:end] //即有start、也有end,输出从start下标到end下标处的字符串,记得不包含end下标的字符哈

[start:end:step] //这次多了一个step、step如果不加的话,默认步长肯定是1

[:] //忘记介绍这种了,一个下标也不填写的情况,默认是start == 0、end == 字符串长度、step == 1,输出整个字符串

[::-1] // 字符串转置
————————————————
版权声明:本文为CSDN博主「叫我王员外就行」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cadi2011/java/article/details/84538769

进制转化:

print('转换成二进制', bin(3))
print('转换成16进制', hex(23))
print('转换成8进制',oct(98))
print('看布尔值', bool(0))
View Code

 

2. Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contain any char.

大小写转换

print('转换成二进制', bin(3))
print('转换成16进制', hex(23))
print('转换成8进制',oct(98))
print('看布尔值', bool(0))
View Code

答案:

def xo(s):
    s = s.lower()  # str.lower(s) 也行
    return s.count('x') == s.count('o')  # 数某个字符串的元素
View Code

3. Simple, given a string of words, return the length of the shortest word(s).

自己写的

def find_short(s):
    c = 0
    l = 10000000
    for i in s:
        # print(i)
        if i !=' ':
            c += 1
        else:
            if c < l:
                l = c
                c = 0
            c = 0
    return l
View Code

答案:

def find_short(s):
    return min(len(x) for x in s.split())  # xx.split() 可以空格分成多个字符串
View Code

4. 字符串反转

自己写的

def solution(string):
    st = ''
    while string != '':
        st += string[-1]
        string = string[:-1]
    return st
View Code

答案:

def solution(string):
    return string[::-1]
View Code

 

5. 提取4个字母的单词

mein:

def friend(x):
    a = []
    for i in x:
        if len(i) == 4:
            a.append(i)
    return a

key:

def friend(x):
    return [f for f in x if len(f) == 4]
View Code

 

6 . 矩阵转化为二进制 

Testing: [0, 0, 0, 1] ==> 1 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 0, 1] ==> 5 Testing: [1, 0, 0, 1] ==> 9 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 1, 0] ==> 6 Testing: [1, 1, 1, 1] ==> 15 Testing: [1, 0, 1, 1] ==> 11

mein: 

def binary_array_to_number(arr):
    x = ''
    for i in arr:
        x += str(i)
    return int(x,2)

key:

def binary_array_to_number(arr):
    return int(''.join(str(a) for a in arr), 2)
def binary_array_to_number(arr):
  return int("".join(map(str, arr)), 2)    

map(function, iterable, …),它的返回结果是一个列表。  将iterable 里面的每个值,给入function 求结果

7. 去掉字符串当中的空格

mein:

def no_space(x):
    a = ''
    for i in x:
        if i !=' ':
            a +=i
    return a

key:  字符串 replace

def no_space(x):
    return x.replace(' ' ,'')
def no_space(x):
    return "".join(x.split())  # 跟自己的同一逻辑

 

 8. 将数字倒序排列

mein:

def descending_order(num):
    a = []
    b = ''
    for i in str(num):
        a.append(int(i))
    a.sort(reverse=True)
    for x in a:
        b += str(x)
    return int(b)

key

def Descending_Order(num):
    return int("".join(sorted(str(num), reverse=True)))

9. 返回每位数的乘方  比如 9119 返回811181

def square_digits(num):
    return int(''.join(str(int(i)**2) for i in str(num)))

10. 返回字母在字母表中的位置

mein:

def alphabet_position(text):
    a = ''.join(i for i in text if i.isalpha() == True)
    d = ''
    for c in a:
        if ord(c)>96:
            d = d + str(ord(c)-96) + ' '
        else:
            d = d + str(ord(c)-64) + ' '
    return d[0:len(d)-1]  # 格式要求  可以忽视

key:

def alphabet_position(s):
  return " ".join(str(ord(c)-ord("a")+1) for c in s.lower() if c.isalpha())