您的位置:首页 > 编程语言 > Python开发

python基础知识部分习题及答案

2020-03-09 22:54 513 查看

习题1
输入一个字符串列表,返回同时满足以下两个条件的字符串的个数:
#1.字符串长度大等于2
#2.字符串的第一个字符等于最后一个字符

def match_ends(words):
i=0
for item in words:
if len(item) >= 2 and item[0] == item[-1]:
i = i + 1
return i
print(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']))

习题2
输入一个字符串列表,返回满足以下条件的字符串列表:
1.按字母顺序从小到大排序
2.第一个字母是’x’的字符串排列在最前面

def front_x(words):
list1=[]
list2 = []
words.sort()
for item in words:
if item[0]=='x':
list1.append(item)
else:
list2.append(item)
words=list1+list2
return words
print(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']))

习题3
输入一个非空的元组列表,返回按列表中元组的最后一个元素从小到大排序后的元组列表
例如:输入:[(1, 7), (1, 3), (3, 4, 5), (2, 2)]应该返回:[(2, 2), (1, 3), (3, 4, 5), (1, 7)]
提示:使用自定义键=函数从每个元组提取最后一个元素。

def sort_last(tuples):
for a in range(len(tuples)):
for item in range(len(tuples) - a - 1):
if tuples[item][-1] > tuples[item+1][-1]:
tuples[item], tuples[item + 1] = tuples[item + 1], tuples[item]
return tuples
print(sort_last([(1, 3), (3, 2), (2, 1)]))
>>> [(2, 1), (3, 2), (1, 3)]

习题4
输入一个数字列表,将所有相邻且相同的元素去重保留一个元素后返回
例如:输入[1, 2, 2, 3] 返回 [1, 2, 3];输入[1, 1, 2, 2, 3, 3, 3] 返回 [1, 2, 3]
新建一个列表或者修改原来的列表返回均可。

def remove_adjacent(nums):
b=[]
a=set(nums)
b.extend(a)
return b
print(remove_adjacent([1, 2, 2, 3]))
>>> [1, 2, 3]

习题5
给定两个按递增顺序排序的列表,创建并返回一个合并的按排序排列的所有元素的列表。
例如输入 [‘aa’, ‘xx’, ‘zz’], [‘bb’, ‘cc’],应该返回[‘aa’, ‘bb’, ‘cc’, ‘xx’, ‘zz’]
希望你提供的解决方案在“线性”时间内工作,使两个列表都可以一次完成。

def linear_merge(list1, list2):
a=list1+list2
a.sort()
return a

print(linear_merge(['aa', 'xx', 'zz'], ['bb', 'cc']))
>>> ['aa', 'bb', 'cc', 'xx', 'zz']

字符串练习题
习题1
输入一个字符串s,返回由字符串的最前面两个字母和最后两个字母组成的字符串。
例如: ‘spring’ 返回 ‘spng’, ‘is’ 返回 ‘is’
当输入的字符串长度小于2时,返回空字符串

test(both_ends('spring'), 'spng')
def both_ends(s):
if len(s)>=2:
a=s[0]+s[1]+s[-2]+s[-1]
return a
else:
return ''
return
print(both_ends('sadfgh'))
>>> sagh

习题2
输入一个字符串s, 返回满足以下条件的字符串
1.找出与字符串的第一个字母相同的字母,把它们替换成 ‘*’,除了第一个字母本身以外
例如: 输入’babble’, 返回 ‘ba**le’
提示:使用 s.replace(stra, strb) 函数,可以将字符串 s 中的所有 子字符串stra 替换为 子字符串strb

def fix_start(s):
i=1
while i < (len(s)-1):
if s[i]==s[0]:
s=s[:i]+'*'+s[i+1:]
i+=1
return s
print(fix_start('babble'))
>>>ba**le

习题3
输入字符串 a 和 b, 返回添加以下条件的字符串
1.使用空格把两个字符串分隔后合并成一个字符串
2.交换两个字符串的最前面的两个字母
3.字符串 a 和 b 的长度都大等于2
例如:
‘mix, pod’ -> ‘pox mid’
‘dog’, ‘dinner’ -> ‘dig donner’

def mix_up(a, b):
c=a
if len(a)>=2 and len(b)>=2:
a=b[:2]+a[2:]
b=c[:2]+b[2:]
s=a+' '+b
return s
else:
print('字符串长度应该大于等于2')
return
print(mix_up('mix', 'pod'))
>>> pox mid

习题4
输入一个字符串,返回满足以下条件的字符串
如果字符串长度大等于3,添加 ‘ing’ 到字符串的末尾
如果字符串是以 ‘ing’ 结尾的,就在末尾添加 ‘ly’
如果字符串长度小于3,返回原字符串

def verbing(s):
if len(s)>=3:
if s[-3:]=='ing':
s=s+'ly'
else:
s=s+'ing'
else:
return s
return s

习题5
考虑把一个字符串拆分成两个等分
1.如果字符串长度是偶数,前一半和后一半的长度是相同的
2.如果字符串长度是奇数,则多出的一个字符加到前一半,如:‘abcde’,前一半是’abc’,后一半是’de’
3.输入两个字符串, a 和 b,按以下格式返回结果
a-front + b-front + a-back + b-back
(1)先穷举

def front_back(a, b):
if len(a)%2==0:
m = int(len(a) / 2)
x1 = a[:m]
x2 = a[m:]
if len(b)%2==0:
n = int(len(b) / 2)
y1 = b[:n]
y2 = b[n:]
elif len(b)%2==1:
n = int(len(b) / 2+1)
y1 = b[:n]
y2 = b[n:]
s=x1+y1+x2+y2
elif len(a)%2==1:
m = int(len(a) / 2 + 1)
x1 = a[:m]
x2 = a[m:]
if len(b)%2==0:
n = int(len(b) / 2)
y1 = b[:n]
y2 = b[n:]
elif len(b)%2==1:
n = int(len(b) / 2+1)
y1 = b[:n]
y2 = b[n:]
s=x1+y1+x2+y2
return s
print(front_back('abcd', 'xy'))
>>>abxcdy

(2)稍微改进一下,还是很啰嗦

def front_back(a, b):
def ou(x):
m = int(len(x) / 2)
x1 = x[:m]
x2 = x[m:]
p = [x1, x2]
return p
def ji(y):
z = int(len(y) / 2 + 1)
y1 = y[:z]
y2 = y[z:]
q = [y1, y2]
return q
if len(a)%2==0 and len(b)%2==0:
s = ou(a)[0] + ou(b)[0] + ou(a)[1] + ou(b)[1]
elif len(a) % 2 == 1 and len(b) % 2 == 1:
s = ji(a)[0] + ji(b)[0] + ji(a)[1] + ji(b)[1]
elif len(a) % 2 == 0 and len(b) % 2 == 1:
s = ou(a)[0] + ji(b)[0] + ou(a)[1] + ji(b)[1]
elif len(a) % 2 == 1 and len(b) % 2 == 0:
s = ji(a)[0] + ou(b)[0] + ji(a)[1] + ou(b)[1]
return  s

(3)让他们都先等于一个数,后续加条件再改

def front_back(a, b):
a_middle = len(a) // 2
b_middle = len(b) // 2
if len(a) % 2 == 1:  # add 1 if length is odd
a_middle = a_middle + 1
if len(b) % 2 == 1:
b_middle = b_middle + 1
return a[:a_middle] + b[:b_middle] + a[a_middle:] + b[b_middle:]
print(front_back('abcd', 'xy'))
  • 点赞
  • 收藏
  • 分享
  • 文章举报
muguangjingkong 发布了7 篇原创文章 · 获赞 0 · 访问量 263 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: