1.字符串排序:
list1 = input().split()
list1.sort()
for i in range(len(list1)):
if i==0:
print(list1[i],end="")
else:
print(" "+list1[i],end="")
语法:list.sort(cmp=None, key=None, reverse=False)
2.字符之比较大小:
list1 = [int(i) for i in input().split()]
list1.sort()
for i in range(len(list1)):
if i == len(list1)-1:
print("%c"%list1[i],end="")
else:
print("%c<"%list1[i],end="")
3.判断回文字符串:
st1 = input()
list1 = []
list2 = []
for i in range(len(st1)):
if st1[i].isalpha():
list1.append(st1[i].lower())
list2.append(st1[i].lower())
list2.reverse()
if list1 == list2:
print('yes')
else:
print('no')
4.删除字符:
st1 = input().strip()
cha = input().strip()
st1 = st1.replace(cha.upper(),"")
st1 = st1.replace(cha.lower(),"")
st1 = st1.replace(cha,"")
print("result: "+st1,end='')
5. sdut-逆序的N位数
num = input().strip()
num2 = ''.join(reversed(num))
print(int(num2),end="")
6.输出字符串中最大字符及其索引位置
st1 = input()
key = 0
flag = st1[1]
for i in range(len(st1)):
if st1[i]>=flag:
key = i
flag = st1[i]
print("{} {}".format(flag,key))
7.计算多个字符串中最长的字符串长度
n = int(input())
max_num = 0
for i in range(n):
num = len(input().strip())
if num>max_num:
max_num = num
print("length=%d"%max_num,end="")
两边都不能有空格,否则会部分正确
8.十进制数转换成二进制后1和0的个数:
num = int(input())
st1 = bin(num)
num1,num0 = 0,0
for i in range(2,len(st1)):
if st1[i]=='1':
num1+=1
elif st1[i]=='0':
num0+=1
print(num1,num0,end="")
9.整数的二进制相加:
num1 = int(input())
num2 = int(input())
num3 = num1 + num2
print("%08d"%int(format(num1,'b')))
print("%08d"%int(format(num2,'b')))
print('--------')
print("%08d"%int(format(num3,'b')))
10.汉明距离
num1,num2 = map(int,input().split())
str1 = bin(num1)
str2 = bin(num2)
long = max(len(str1),len(str2))
short = min(len(str1),len(str2))
# str1 = str1.ljust(long,'0')
# str2 = str2.ljust(long,'0')
flag = 0
for i in range(short):
if str1[i]!=str2[i]:
flag+=1
flag += long-short
print(flag)
ljust是将字符串左边对齐然后补零
11.输出一个字符串的字符的16-10-8-2进制数
str1 = input()
str2 = "0123456789abcdefABCDEF"
str3 = [i for i in str1 if i in str2]
num = int("".join(str3),16) #列表转字符串
print("{0:x} {0:d} {0:o} {0:b}".format(num))
12.显示数字出现次数:
num = int(input())
key = input()
st1 = hex(num)
flag = 0
for i in range(len(st1)):
if st1[i]==key:
flag+=1
if key == '0':
print(flag-1)
print(flag)