2020面试笔试,参加的有浦发笔试面试,农行研发,中行,中信,京东网易数据挖掘,以及一些商业银行题型,至于腾讯百度的太难了,本人直接放弃,非计算机专业,纯自学python。
整理了自己练习的Python基础:
偶数求和
# 偶数求和
sum([i for i in range(101) if i%2==0])
sum = 0
for i in range(0,101,2):
sum += i
print(sum)
something = sys.stdin.readline().strip()
请输出奇数位字母
# 请输出奇数位字母
st = 'Hello Python DuShuSir'
sr =[]
i=0
while i<len(st):
if i%2!=0:
sr.append(st[i])
i +=1
print(sr)
请输出纯数字项
>>> str ="h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [int(s) for s in str.split() if s.isdigit()]
[23, 11, 2]
输出数字
import re
totalCount = '100abc'
totalCount = re.sub("\D", "", totalCount)
print(totalCount)
>>> 100
已知一个二叉树前序遍历和中序遍历分别为ABDEGCFH和DBGEACHF,则该二叉树的后序遍历为?
答:DGEBHFCA
计算题,判断变量是否为0, 是0则为False,非0判断为True
# and中含0,返回0; 均为非0时,返回后一个值,
2 and 0 # 返回0
2 and 1 # 返回1
1 and 2 # 返回2
# or中, 至少有一个非0时,返回第一个非0,
2 or 0 # 返回2
2 or 1 # 返回2
0 or 1 # 返回1
#实际题目,网易的选择题好像是
a = 'hello'
b = [1,2]
print(a and b) #[1, 2]
print(b and a) #hello
改变字母大小写
# 改变字母大小写
def change(text):
list_a = []
for i in text.split(' '):
list_a.append(i.capitalize())
a = ' '.join(list_a)
return a
text = 'hello your world!'
change(text)
list_b = []
for i in text.split(' '):
list_b.append(i.capitalize())
b = ' '.join(list_b)
print(b)
字母大小写
s='What is Your Name?'
s2=s.lower()
print(s2) #返回小写字符串
# what iss your name?
print(s.upper()) #返回大写字符串
# WHAT IS YOUR NAME?
print(s.capitalize()) #字符串首字符大写
# What is your name?
print(s.title()) #每个单词的首字母大写
# What Is Your Name?
print(s.swapcase()) #大小写互换
# wHAT IS yOUR nAME?
#提取一个整数的偶数位
# 第一题:提取一个整数的偶数位
# 1.输入123456,输出246
list[::2 ] #就是取奇数位 这里的 i j 我们省略的话就是默认数组最开头到结尾
list[1::2] #这里缺省了j 但是i定义了1 也就是从数组第二个数开始取 ,所以这个是取偶数位
a = int(12345678)
b = list(str(a))
c = []
for i, num in enumerate(b):
if i%2 != 0:
c.append(num)
print(''.join(c))
统计一个字符串中的字符和数字出现个数
# 统计一个字符串中的字符和数字出现个数
# 2.输入a3rcd57sc8,输出 数字个数 4 字符个数 6
intCount = 0 #用来记录列表中的int元素个数
strCount = 0 #记录str元素个数
spacecount = 0
otherCount = 0
a = "pl421z inp42ut442 a string343bgb2 gfregfe%%%%%"
# 使用for循环遍历字符串,每次循环判断当前获取的元素的类型,并给对应计数器计数
for i in a:
if i.isdigit(): #判断i是不是int
intCount += 1
elif i.isalpha(): #判断i是不是str
strCount += 1
elif i.isspace():
spacecount += 1
else:
otherCount += 1
print ("数字=%d,字母=%d,其他=%d, 空格%d" % (intCount,strCount,otherCount,spacecount))
A-Z对应1-26,输入一串字符,输出对应的数。
# A-Z对应1-26,输入一串字符,输出对应的数。
l1 = ['a','b','c','d','e']
l2 = [1,2,3,4,5]
a = input()
dict_1 = dict(zip(l1,l2))
print(dict_1[a])
子字符串
# 子字符串
def cut(s: str):
results = []
# x + 1 表示子字符串长度
for x in range(len(s