文件名称:
Brainteasers in Python 3.5.pdf.pdf
开发工具:
文件大小: 292kb
下载次数: 0
上传时间: 2019-09-14
详细说明:Brainteasers in Python 3.5.pdfEx 1: To find if given strings are anagrams of each other
>>>def isAnagram(a, b)
L1=[]
L2=[]
for chi in a
1. append(ch1)
for ch2 in b:
L2 append(ch2
L3=Sorted(L1)
L4=sorted(L2)
if L3==L4
pint(Yes! Anagrams”)
else
print( No, Not anagrams)
>> soNagram(“star”,rats”)
Yes Anagrams
>>> isanagram(“tic”,"tac
No, Not anagrams
Ex 2: Subtract a list from a list
>>>a=[1,2,3,4,5
>b=[3,4,5,6,7
>>> def doMinus(a, b)
for i in b
ifiin a:
a remove()
return a
doMinus(a, b)
>>>
Ex 3: Find union of two lists in Python
>>>a=[1,2,3,4,5]
>b=[3,4,5,6,7
>>>def doUpion( a, b)
result=D
orin a
if not i in result
result. append(i)
for i in
b:
if not i in result
result. append①
return result
>>>doUpion(a, b)
[1,2,3,4,5,6,7
Ex 4: Find intersection of two lists
>>>a=[1,2,3,4,5]
>b=[3,4,5,6,7
>>>def dolntersect(a, b)
d=[
C-dOUnion(a
for i in c.
ifi in a and i in b:
d append(i)
return
>>>doIntersect(a, b)
3,4,5
Ex 5: Merge two lists and give the sorted results
>>>a=[2,1,3]
>b=[5,4,6]
>>>C=a+b
C
[2,1,3,5,4,6
>>>sorted(c
[1,2,3,4,5,6]
Ex 6: Find triplets in a list whose sum equals zero
>>>a=[1,2,3,-1,0,-1
for i in rangedlen(a))
forj in range(i+1,len(a)):
for k in range (i+2, len(a)
ifa[i]+a前]+a[k]==0:
print("Found Triplet: ,ali], aljl, alkd
Refine the program so that each triplet is printed just once
Ex. 7. Find if elements of list are in a Arithmetic Sequence
>>>a=[1,3,5,7,9,11,13]
>b=[2,4,5,7,9,11,13]
>> def is arith Seqla)
length=le
diff=a[l]-a[O
for i in range(1, length)
if a[]-ali-1=diff
return“ Not in A Seq”
retu“ In Arith. Seq
>>> isArithSeqa
In Arith. Seq
>>> is ArithSeq(b)
Not in A Seq
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.