'''
merge two configure files, basic file is aFile
insert the added content of bFile compare to aFile
for example, 'bbb' is added content
-----------------------------------------------------------
a file content | b file content | c merged file content
111 | 111 | 111
aaa | bbb | aaa
| | bbb
222 | 222 | 222
------------------------------------------------------------
'''
def mergeFiles(aPath, bPath, cPath): with open(aPath, 'r') as f:
aLines = f.readlines();
aLines = [ line.strip() + '\n' for line in aLines] with open(bPath, 'r') as f:
bLines = f.readlines();
bLines = [ line.strip() + '\n' for line in bLines] cLines = mergeSequences(aLines, bLines) with open(cPath, 'w') as f:
for line in cLines:
f.write(line) '''
merge the sequence
'''
def mergeSequences(aLines, bLines):
record = {}
lcs = findLCS(record, aLines, 0, bLines, 0)
currA = currB = 0
merged = []
for (line, aI, bI) in lcs: # add deleted
if aI > currA:
merged.extend(aLines[currA:aI])
currA = aI + 1 # add added
if bI > currB:
merged.extend(bLines[currB:bI])
currB = bI + 1 # add common
merged.append(line) if currA < len(aLines):
merged.extend(aLines[currA:])
if currB < len(bLines):
merged.extend(bLines[currB:]) return merged '''
find Longest common subsequence
return list of (line, x, y)
line is common line, x is the index in aLines, y is the index in bLines
TODO: eliminate recursive invoke, use dynamic algorithm
'''
def findLCS(record, aLines, aStart, bLines, bStart): key = lcsKey(aStart, bStart)
if record.has_key(key):
return record[key] aL = aLines[aStart:]
bL = bLines[bStart:]
if len(aL) > 0 and len(bL) > 0:
if aL[0] == bL[0]:
lsc = [(aL[0], aStart, bStart)]
lsc.extend(findLCS(record, aLines, aStart + 1, bLines, bStart + 1))
record[key] = lsc
return lsc
else:
aLsc = findLCS(record, aLines, aStart, bLines, bStart + 1)
bLsc = findLCS(record, aLines, aStart + 1, bLines, bStart) if len(aLsc) > len(bLsc):
record[key] = aLsc
return aLsc
else:
record[key] = bLsc
return bLsc
else:
return [] Code

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(34)-文章发布系统①-简要分析
  2. 解决:Redis:java.util.NoSuchElementException: Unable to validate object at
  3. “Transaction rolled back because it has been marked as rollback-only”
  4. ActiveReports 报表应用教程 (3)---图表报表
  5. pip 直接安装tar.gz zip文件包 (windows linux mac 可用)
  6. php 注入
  7. java多线程之:创建开启一个线程的开销
  8. js 跨域的问题 (同一个主域名不同的二级域名下的跨域问题) 解决 WdatePicker.js my97日期选择控件
  9. 判断IE版本的语句 [if lte IE 6]...[endif]
  10. Poj2948Martian Mining(记忆化)
  11. 批处理:循环解压不同文件夹下的zip压缩包
  12. webwervice发布时出错 java.security.PrivilegedActionException
  13. pdf点击超链接后返回:alt+ 向左 /向右
  14. cmake 入门实战
  15. mmap:速度快+整块操作
  16. Ubuntu和Windows双系统的安装
  17. Linux su切换用户后命令提示符变为bash-4.2$
  18. Windows Community Toolkit 3.0 - InfiniteCanvas
  19. bzoj 1531 Bank notes 多重背包/单调队列
  20. 剪格子|2013年蓝桥杯A组题解析第九题-fishers

热门文章

  1. POJ 3090 Visible Lattice Points | 其实是欧拉函数
  2. a标签打电话
  3. matlab sqlite
  4. cocos2d programming guide 翻译 引导页(完结)
  5. MFC CString GetBuffer/ReleaseBuffer 的使用条件
  6. 如何防止Android反编译
  7. Python学习杂记_15_正则表达式
  8. RobotFramework自动化3-搜索案例【转载】
  9. python 实现经典算法
  10. 浅谈正则表达式-PHP为例