10道單選題
10道多選題
2道編程題
第一題:十進制轉二進制計算1的個數(負數轉為補碼)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/8/23 15:44
# @Author : @linlianqin
# @Site :
# @File : 十進制轉換為二進制(計數1的個數).py
# @Software: PyCharm
# @description:class Solution:def oct_to_binary(self, input_int):# write code heretag = Trueif input_int == 0:return 0if input_int < 0:tag = Falseabs_int = abs(input_int)# 原碼bin_int = list(bin(abs_int)[2:])bin_int = ["0" for _ in range(32 - len(bin_int))] + bin_intif tag:return bin_int.count("1")else:# 反碼reverse_bin_int = ["0" for _ in range(32)]for index, i in enumerate(bin_int):if i == "0":reverse_bin_int[32-len(bin_int)+index] = "1"# 補碼res = ""resual = Truefor index,i in enumerate(reverse_bin_int[::-1]):if i == "0":if resual:res = "1"+reselse:res = "0"+resresual = Falseelif i == "1":if resual:res = "0" + resresual = Trueelse:res = "1" + resif resual:res = "1" + resreturn res.count("1")print(Solution().oct_to_binary(-5))
第二題:字符串轉為駝峰
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/8/23 16:21
# @Author : @linlianqin
# @Site :
# @File : 輸入的字符串轉換為駝峰.py
# @Software: PyCharm
# @description:
'''
詳細描述
1. 轉換后的字符串只保留字母[a-zA-Z]和數字[0-9],去除其他字符;
2. 輸入字符串中的字母字符的前一字符如非字母或數字,該字母轉換后為大寫,如果前一個字符為字母或者數字,字母轉換后為小寫;
例外:轉換后的字符串第一個字符如果是字母,則該字母轉換后為小寫;——字符串首字母小寫
3. 轉換后的字符串保留數字字符。
4. 字符串如果為空或者無[a-zA-Z]和數字[0-9]中字符,請默認輸出如下字符串"shopee"
'''class Solution:def camelCase(self, newString):# write code herestrLen = len(newString)# 字符串為空if strLen == 0:return "shopee"# 字符串不為空res = []for index,code in enumerate(newString):# 是字母或者數字if code.isalnum():if index == 0:if code.isdigit():res.append(code)else:res.append(code.lower())continue# 前一個是字母和數字時:if index >= 1 and newString[index-1].isalnum():if code.isdigit():res.append(code)else:res.append(code.lower())# 前一個不是字母和數字時elif index >= 1 and not newString[index - 1].isalnum():if code.isdigit():res.append(code)else:res.append(code.upper())#處理第一個字符if len(res) == 0:return "shopee"else:if res[0].isalpha():res[0] = res[0].lower()res = "".join(res)return resprint(Solution().camelCase("__HELLO_World"))