Assignment 2

简介: unix diff two files3. Task specificationsWrite a program stored in a file named diff.py that implements three classes,• DiffCommands,• DiffCommandsError and• OriginalNewFiles, the second one deriving from Exception.DiffCommands does not provide any method in its public interface. It builds

Assignment 2
COMP9021, Trimester 1, 2023

  1. General matters

1.1. Aim. The purpose of the assignment is to:
• develop your problem solving skills;
• design and implement a solution to a problem in the form of medium sized Python program;
• in particular but no to only, parse text in files, make use of the dynamic programming algorithmic technique, implement class methods.
1.2. Submission. Your programs will be stored in a file named diff.py. After you have developed and tested your program, upload it using Ed (unless you worked directly in Ed). Assignments can be submitted more than once; the last version is marked. Your assignment is due by April 24, 10:00am.
1.3. Assessment. The assignment is worth 13 marks, distributed as indicated in Part 3. It is going to be tested against a number of inputs. For each test, the automarking script will let your program run for 30 seconds. The outputs of your programs should be exactly as indicated.
1.4. Reminder on plagiarism policy. You are permitted, indeed encouraged, to discuss ways to solve the assignment with other people. Such discussions must be in terms of algorithms, not code. But you must implement the solution on your own. Submissions are routinely scanned for similarities that occur when students copy and modify other people’s work, or work very closely together on a single implementation. Severe penalties apply.
1

  1. General presentation

This presentation refers to a number of text files that are provided together with this pdf. Consider the two text files file_1_1.txt and file_1_2.txt:

$ cat -n file_1_1.txt
1    A line to delete: 1
2    A line to delete: 2
3    A line that stays: 1
4    A line that stays: 2
5    A line to change: 1
6    A line that stays: 3
7    A line that stays: 4
8    A line that stays: 5
9    A line that stays: 6
10    A line to delete: 3
11    A line that stays: 7
12    A line that stays: 8
13    A line to change: 2
14    A line to change: 3
15    A line to change: 4
16    A line to change: 5
17    A line that stays: 9
$ cat -n file_1_2.txt
1    A line that stays: 1
2    A line to insert: 1
3    A line that stays: 2
4    A changed line: 1
5    A line that stays: 3
6    A line that stays: 4
7    A line to insert: 2
8    A line to insert: 3
9    A line that stays: 5 10    A line that stays: 6
11    A line that stays: 7
12    A line that stays: 8
13    A changed line: 2
14    A changed line: 3
15    A changed line: 4
16    A line that stays: 9

The Unix diff command can be applied to file_1_1.txt and file_1_2.txt:

$ diff file_1_1.txt file_1_2.txt

1,2d0
< A line to delete: 1
< A line to delete: 2
3a2

A line to insert: 1
5c4
< A line to change: 1

A changed line: 1
7a7,8
A line to insert: 2
A line to insert: 3
10d10
< A line to delete: 3
13,16c13,15
< A line to change: 2
< A line to change: 3
< A line to change: 4
< A line to change: 5

A changed line: 2
A changed line: 3
A changed line: 4
Study this example to understand diff’s output.
• d is for delete, for commands of the form l_1dl_2 with l_1 and l_2 two line numbers, or of the form l_1,l_2dl_3 with l_1, l_2 and l_3 three line numbers.
• a is for add, for commands of the form l_1al_2 with l_1 and l_2 two line numbers, or of the form l_1al_2,l_3 with l_1, l_2 and l_3 three line numbers.
• c is for change, for commands of the form l_1cl_2 with l_1 and l_2 two line numbers, or of the form l_1,l_2cl_3 with l_1, l_2 and l_3 three line numbers, or of the form l_1cl_2,l_3 with l_1, l_2 and l_3 three line numbers, or of the form l_1,l_2cl_3,l_4 with l_1, l_2, l_3 and l_4 four line numbers.
diff identifies a longest common subsequence (LCS) of lines that are common to both files. It then computes the unique minimal set of commands that allows one to convert the first file into the second one (the ed editor can actually precisely do this). With the previous example, there is a unique LCS, consisting of the lines A line that stays: 1, A line that stays: 2, A line that stays: 3 and A line that stays: 4.
Still the LCS is not always unique. Consider for instance the two text files file_2_1.txt and file_2_2.txt:
$ cat -n file_2_1.txt
1    A line

$ cat -n file_2_2.txt
1 A line
2 A line
There are then 2 LCSs, as one can chose from file_2_2.txt either the first or the second line. Unix diff determines one LCS and yields the corresponding output:
$ diff file_2_1.txt file_2_2.txt
1a2

A line
But another implementation could have output instead:
$ diff file_2_1.txt file_2_2.txt
0a1
A line
For another example, consider the two text files file_3_1.txt and file_3_2.txt:
$ cat -n file_3_1.txt
1 Line 1
2 Line 2
3 A line to go
4 A line to go
5 A line to go
6 Line 3
7 Line 4
8 A line to go
$ cat -n file_3_2.txt
1 A line to come
2 A line to come
3 Line 1
4 Line 2
5 A line to come
6 A line to come
7 Line 1
8 Line 2
9 A line to come 10 A line to come
11 A line to come
12 Line 3
13 Line 4
There are then 3 LCSs, as the unique occurrences of Line 1 and Line 2 in the first file can be matched with the first occurrences of Line 1 and Line 2 in the second file, or with the first occurrence of Line 1 and the second occurrence of Line 2 in the second file, or with the second occurrences of Line 1 and Line 2 in the second file.
Unix diff determines one LCS and yields the corresponding output:
$ diff file_3_1.txt file_3_2.txt
0a1,2
A line to come
A line to come
3,5c5,11
< A line to go
< A line to go
< A line to go

A line to come
A line to come
Line 1
Line 2
A line to come
A line to come
A line to come
8d13
< A line to go
But another implementation could have output instead either
0a1,2
A line to come
A line to come
1a4,7
Line 2
A line to come
A line to come
Line 1
3,5c9,11
< A line to go
< A line to go
< A line to go

A line to come
A line to come
A line to come
8d13
< A line to go
or 0a1,6
A line to come
A line to come
Line 1
Line 2
A line to come
A line to come
3,5c9,11
< A line to go
< A line to go
< A line to go

A line to come
A line to come
A line to come
8d13
< A line to go
One could want to output the LCS, common to both files, with ... for longest nonempty sequences of lines that are not part of the LCS, w.r.t. the first file, or w.r.t. the second file.
For the Unix diff applied to file_1_1.txt and file_1_2.txt, that would be, w.r.t. file_1_1.txt:
...
A line that stays: 1
A line that stays: 2
...
A line that stays: 3
A line that stays: 4
A line that stays: 5
A line that stays: 6
...
A line that stays: 7
A line that stays: 8
...
A line that stays: 9
and w.r.t. file_1_2.txt: A line that stays: 1
...
A line that stays: 2
...
A line that stays: 3
A line that stays: 4
...
A line that stays: 5
A line that stays: 6
A line that stays: 7
A line that stays: 8
...
A line that stays: 9
For the Unix diff applied to file_2_1.txt and file_2_2.txt, that would be, w.r.t. file_2_1.txt: A line
and w.r.t. file_2_2.txt: A line ...
For the Unix diff applied to file_3_1.txt and file_3_2.txt, that would be, w.r.t. file_3_1.txt:
Line 1
Line 2 ...
Line 3
Line 4 ...
and w.r.t. file_3_2.txt:
...
Line 1
Line 2 ...
Line 3
Line 4
  1. Task specifications

Write a program stored in a file named diff.py that implements three classes,
• DiffCommands,
• DiffCommandsError and
• OriginalNewFiles, the second one deriving from Exception.
DiffCommands does not provide any method in its public interface. It builds a DiffCommands object from a text file meant to store a plausible sequence of diff commands, one command per line, without any space on any line, and without any extra line. In case the text file does not satisfy those conditions, the __init__()
method of DiffCommands raises a DiffCommandsError error (1 mark) with as message
Cannot possibly be the commands for the diff of two files
Seven files of this type are provided (wrong_1.txt to wrong_7.txt). Three files from which you can build a DiffCommands object are provided:
• diff_1.txt, which stores the only possible sequence of diff commands for the sample files file_1_1.txt and file_1_2.txt;
• diff_2.txt, which stores one of the two possible sequences of diff commands for the sample files file_2_1.txt and file_2_2.txt;
• diff_3.txt, which stores one of the three possible sequences of diff commands for the sample files file_3_1.txt and file_3_2.txt.
DiffCommands implements the __str__() function (3 marks) so that print() can be used to output the diff commands of the object under consideration.
OriginalNewFiles provides a user interface with 5 methods:
• is_a_possible_diff() (2 marks)
• output_diff() (2 marks)
• output_unmodified_from_original() (1.5 marks)
• output_unmodified_from_new() (1.5 marks)
• get_all_diff_commands() (2 marks)
The following example of interaction shows how the methods are supposed to be called and what they are supposed to return. The only point which might not be obvious from that example is that in the sequence of diff commands output by the method
OriginalNewFiles.pair_of_files.get_all_diff_commands()
the diff commands are lexicographically ordered.
$ python3 ...

from diff import * >>> DiffCommands('wrong_1.txt') ...
diff.DiffCommandsError: Cannot possibly be the commands for the diff of two files >>> DiffCommands('wrong_2.txt') ...
diff.DiffCommandsError: Cannot possibly be the commands for the diff of two files >>> DiffCommands('wrong_3.txt') ...
diff.DiffCommandsError: Cannot possibly be the commands for the diff of two files >>> DiffCommands('wrong_4.txt') ...
diff.DiffCommandsError: Cannot possibly be the commands for the diff of two files >>> DiffCommands('wrong_5.txt') ...
diff.DiffCommandsError: Cannot possibly be the commands for the diff of two files >>> DiffCommands('wrong_6.txt') ...
diff.DiffCommandsError: Cannot possibly be the commands for the diff of two files >>> DiffCommands('wrong_7.txt') ...
diff.DiffCommandsError: Cannot possibly be the commands for the diff of two files
DiffCommands('diff_1.txt')
<diff.DiffCommands object at ...>
diff_1 = DiffCommands('diff_1.txt')
print(diff_1)
1,2d0
3a2
5c4
7a7,8
10d10
13,16c13,15
diff_2 = DiffCommands('diff_2.txt')
print(diff_2)
1a2
diff_3 = DiffCommands('diff_3.txt')
print(diff_3)
0a1,2
3,5c5,11
8d13
pair_of_files = OriginalNewFiles('file_1_1.txt', 'file_1_2.txt')

pair_of_files.is_a_possible_diff(diff_1)
True

pair_of_files.is_a_possible_diff(diff_2)
False
pair_of_files.is_a_possible_diff(diff_3)
False
pair_of_files.output_diff(diff_1)
1,2d0
< A line to delete: 1
< A line to delete: 2
3a2> A line to insert: 1
5c4< A line to change: 1
A changed line: 1
7a7,8> A line to insert: 2
A line to insert: 3
< A line to delete: 3

10d10
13,16c13,15
< A line to change: 2
< A line to change: 3
< A line to change: 4

< A line to change: 5

A changed line: 2
A changed line: 3
A changed line: 4

pair_of_files.output_unmodified_from_original(diff_1) ...
A line that stays: 1
A line that stays: 2
...
A line that stays: 3
A line that stays: 4
A line that stays: 5
A line that stays: 6
...
A line that stays: 7
A line that stays: 8
...
A line that stays: 9 pair_of_files.output_unmodified_from_new(diff_1) A line that stays: 1
...
A line that stays: 2
...
A line that stays: 3
A line that stays: 4
...
A line that stays: 5
A line that stays: 6
A line that stays: 7
A line that stays: 8
...
A line that stays: 9
pair_of_files.get_all_diff_commands()
[<diff.DiffCommands object at ...>]
diffs = pair_of_files.get_all_diff_commands()
len(diffs)
1
print(diffs[0])
1,2d0
3a2
5c4
7a7,8
10d10
13,16c13,15
pair_of_files = OriginalNewFiles('file_1_2.txt', 'file_1_1.txt')
diffs = pair_of_files.get_all_diff_commands()
len(diffs)
1
print(diffs[0])
0a1,2
2d3
4c5
7,8d7
10a10
13,15c13,16
pair_of_files = OriginalNewFiles('file_1_1.txt', 'file_1_1.txt')
diffs = pair_of_files.get_all_diff_commands()
len(diffs)
1
print(diffs[0])
pair_of_files = OriginalNewFiles('file_2_1.txt', 'file_2_2.txt') pair_of_files.is_a_possible_diff(diff_1)
False
pair_of_files.is_a_possible_diff(diff_2)
True
pair_of_files.is_a_possible_diff(diff_3)
False
pair_of_files.output_diff(diff_2)
1a2
A line
pair_of_files.output_unmodified_from_original(diff_2)
A line
pair_of_files.output_unmodified_from_new(diff_2)
A line ...
diffs = pair_of_files.get_all_diff_commands()
len(diffs)
2
print(diffs[0])
0a1
print(diffs[1])
1a2
pair_of_files = OriginalNewFiles('file_2_2.txt', 'file_2_1.txt')
diffs = pair_of_files.get_all_diff_commands()
len(diffs)
2
print(diffs[0])
1d0
print(diffs[1])
2d1
pair_of_files = OriginalNewFiles('file_3_1.txt', 'file_3_2.txt')
pair_of_files.is_a_possible_diff(diff_1)
False
pair_of_files.is_a_possible_diff(diff_2)
False
pair_of_files.is_a_possible_diff(diff_3) True
pair_of_files.output_diff(diff_3)
0a1,2
A line to come
A line to come
3,5c5,11
< A line to go
< A line to go
< A line to go

A line to come
A line to come
Line 1
Line 2
A line to come
A line to come
A line to come
8d13
< A line to go

pair_of_files.output_unmodified_from_original(diff_3)
Line 1
Line 2 ...
Line 3
Line 4 ...
pair_of_files.output_unmodified_from_new(diff_3) ...
Line 1
Line 2 ...
Line 3
Line 4
diffs = pair_of_files.get_all_diff_commands()
len(diffs)
3
print(diffs[0])
0a1,2
1a4,7
3,5c9,11
8d13
print(diffs[1])
0a1,2
3,5c5,11
8d13
print(diffs[2])
0a1,6
3,5c9,11
8d13
pair_of_files = OriginalNewFiles('file_3_2.txt', 'file_3_1.txt')
diffs = pair_of_files.get_all_diff_commands()
len(diffs)
3
print(diffs[0])
1,2d0

4,7d1
9,11c3,5
13a8

print(diffs[1])
1,2d0
5,11c3,5
13a8
print(diffs[2])
1,6d0
9,11c3,5
13a8
相关文章
|
数据采集 供应链 监控
RPA助力企业管理,十大高频场景
rpa在财务、人力资源、客服、供应链管理、市场推广、质量管理、项目管理、数据分析、合规管理和业务流程优化等场景中的应用
|
Java Linux 网络安全
【Linux环境】Centos 7启动jar包的详细步骤
【Linux环境】Centos 7启动jar包的详细步骤
1073 0
|
存储 安全 搜索推荐
虚拟云桌面中RDS、VDI技术比较
下面,就来看看两种主流的云桌面。 1、RDS(Remote Desktop Services,远程桌面服务),俗称共享云桌面 2、VDI(Virtual Desktop Infrastucture,虚拟桌面架构),俗称虚拟云桌面
3989 1
|
Docker 容器
如何提高Docker的下载速度-----配置加速器
在正常情况下,docker默认连接的国外官方镜像,在国外的网友访问该官方镜像自然不成问题,中国的网络访问国外官方镜像网速一向很慢,而且往往还会遭遇断网的窘境。docker pull 国内网络链接失败或很卡慢,一般都需要更换至国内。
17132 1
|
10月前
|
JSON 关系型数据库 MySQL
如何利用Excel宏和离线数据库自动化IP归属地查询?
随着IP查询需求的日益增加,利用Excel宏结合本地离线数据库自动化IP归属地查询,已成为提高工作效率的重要手段。本文将详细介绍如何通过Excel宏和本地数据库(如MySQL、CSV或JSON文件),实现IP归属地查询的自动化操作。
|
12月前
|
人工智能 缓存 自然语言处理
构建智能客服:阿里云智能语音交互+函数计算的低成本方案
本文介绍如何基于阿里云智能语音交互与函数计算,构建高性价比、弹性可扩展的智能客服系统。涵盖架构设计、核心组件、实现步骤及成本优化策略,助力企业快速实现客服智能化转型,提升服务效率,降低60%以上运营成本。
|
Java Spring
Spring Boot 3系列之-启动类详解
Spring Boot启动类是构建Spring Boot应用程序的关键组成部分。它允许开发人员配置和管理应用程序的行为,同时简化了应用程序的配置和部署过程。通过深入了解Spring Boot启动类的功能和用法,开发人员可以更好地构建和管理复杂的Spring Boot应用程序。希望本文能够帮助您更好地理解和使用Spring Boot启动类。
1125 4
Spring Boot 3系列之-启动类详解
|
机器学习/深度学习 编解码 vr&ar
NeurIPS 2024最佳论文,扩散模型的创新替代:基于多尺度预测的视觉自回归架构
本文详细解读NeurIPS 2024最佳论文《视觉自回归建模:基于下一尺度预测的可扩展图像生成》。该研究提出VAR模型,通过多尺度token图和VAR Transformer结构,实现高效、高质量的图像生成,解决了传统自回归模型在二维结构信息、泛化能力和计算效率上的局限。实验表明,VAR在图像质量和速度上超越现有扩散模型,并展示出良好的扩展性和零样本泛化能力。未来研究将聚焦于文本引导生成和视频生成等方向。
1793 8
NeurIPS 2024最佳论文,扩散模型的创新替代:基于多尺度预测的视觉自回归架构
|
网络安全
IP地址SSL证书怎么申请?
本文介绍如何申请浏览器及系统可信的SSL证书,而非自签名证书。申请IP地址SSL证书需满足:1. 使用公网IP;2. 确保外网可访问;3. 认证时必须使用80或443端口。流程包括提交申请、建立临时站点验证URL内容,认证通过后即可获取证书文件,适用于各种服务器环境。若申请多个IP地址,建议一次性提交以避免串站问题。
853 6
|
监控 前端开发 数据可视化
深入调查研究
【11月更文挑战第5天】
662 2