栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

手写代码:给一个英文文本“i have a dream i am a human you can have dream too.”再给一个文本“i you am ”,要求计算出第一个文本中包含第二个文

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写代码:给一个英文文本“i have a dream i am a human you can have dream too.”再给一个文本“i you am ”,要求计算出第一个文本中包含第二个文

参考回答:

#!/usr/bin/python#encoding:utf-8def lengthCa(arr):start = arr[0]end= arr[0]for i in arr:if i >start:start=iif i < end :end = ireturn start-end

#输入的第一个文本

text = 'ILM runs a batch processing environment capable of modeling, rendering and compositing tens of thousands of motion picture frames per day. Thousands of machines running Linux, IRIX, Compaq Tru64, OS X, Solaris, and Windows join together to provide a production pipeline used by ~800 users daily. Speed of development is key, and Python was a faster way to pre (and re-pre) the programs that control this production pipeline.'

#输入的第二个文本

keywords='a of'newtext=text.split(' ')newkeys = keywords.split(' ')textLen = len(newtext)array=[]

#把index计算出来,用做最优路径规划使用

for i in newkeys:dan=[]for j in range(textLen):if i == newtext[j]:dan.append(j)array.append(dan)print(array)

#最优规划开始

caculateArray=[]for n in array[0]:temp=[]temp.append(n)caculateArray.append(n)flag = 0for n in array:if array.index(n) == 0:continuetemparr=[]for m in caculateArray:#遍历当前最短路径index = caculateArray.index(m)#计算当前路径的index值tempminlen=1000000tempminarr=[]for j in n:#计算当前最短路径,添加下一个节点if flag ==0:    temparr.append(m)    else:    for x in m:    temparr.append(x)    temparr.append(j)    if lengthCa(temparr)<tempminlen :    tempminlen=lengthCa(temparr)    tempminarr=temparr    temparr=[]    caculateArray[index]=tempminarr    print(caculateArray)    flag+=1    tempminlen=1000000    tempminarr=[]    #找出最终所有解里的最优解,为tempminarr    for n in caculateArray:    if lengthCa(n)<tempminlen :    tempminlen=lengthCa(n)    tempminarr=n    #计算tempminarr的起点和重点,现在发现用min()和max()函数就可以了    start = tempminarr[0]    end = tempminarr[0]    for i in tempminarr:    if start<i:    start=i    if end>i:    end=i#输出起始位置和终止位置    print(start,end)    for m in range(end,start+1):    print newtext[m]

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/363654.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号