-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (115 loc) · 3.67 KB
/
main.go
File metadata and controls
142 lines (115 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
var file1Path, file2Path string
fmt.Println("Enter path for file 1:")
fmt.Scanln(&file1Path)
fmt.Println("Enter path for file 2:")
fmt.Scanln(&file2Path)
file1Lines, file1UniqueCount, err := readAndCountUniqueLines(file1Path, file2Path)
if err != nil {
fmt.Println("Error reading file 1:", err)
return
}
file2Lines, file2UniqueCount, err := readAndCountUniqueLines(file2Path, file1Path)
if err != nil {
fmt.Println("Error reading file 2:", err)
return
}
maxLineLength := maxLength(file1Lines, file2Lines)
welcomeMessage := "Welcome to Unique Line Comparison! | C15C01337"
welcomeBoxTop := "┌" + strings.Repeat("─", len(welcomeMessage)+2) + "┐"
welcomeBoxMiddle := "│ " + welcomeMessage + " │"
welcomeBoxBottom := "└" + strings.Repeat("─", len(welcomeMessage)+2) + "┘"
fmt.Println(welcomeBoxTop)
fmt.Println(welcomeBoxMiddle)
fmt.Println(welcomeBoxBottom)
fmt.Println("┌", strings.Repeat("─", maxLineLength+4), "┬", strings.Repeat("─", maxLineLength+6), "┐")
fmt.Printf("│ %-*s │ %-*s │\n", maxLineLength+2, "File 1", maxLineLength+4, "File 2")
fmt.Println("├", strings.Repeat("─", maxLineLength+4), "┼", strings.Repeat("─", maxLineLength+6), "┤")
for i := 0; i < len(file1Lines) || i < len(file2Lines); i++ {
var line1, line2 string
if i < len(file1Lines) {
line1 = file1Lines[i]
}
if i < len(file2Lines) {
line2 = file2Lines[i]
}
fmt.Printf("│ %-*s │ %-*s │\n", maxLineLength+2, line1, maxLineLength+4, line2)
}
fmt.Println("└", strings.Repeat("─", maxLineLength+4), "┴", strings.Repeat("─", maxLineLength+6), "┘")
fmt.Println("\nUnique line count in file 1:", file1UniqueCount)
fmt.Println("Unique line count in file 2:", file2UniqueCount)
}
func readAndCountUniqueLines(filePath string, otherFilePath string) ([]string, int, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, 0, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, 0, err
}
otherLines, err := readLines(otherFilePath)
if err != nil {
return nil, 0, err
}
uniqueLines := findUniqueLines(lines, otherLines)
return uniqueLines, len(uniqueLines), nil
}
func readLines(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
func findUniqueLines(lines1, lines2 []string) []string {
unique := make(map[string]bool)
for _, line := range lines1 {
unique[line] = true
}
for _, line := range lines2 {
if unique[line] {
delete(unique, line)
}
}
var uniqueLines []string
for line := range unique {
uniqueLines = append(uniqueLines, line)
}
return uniqueLines
}
func maxLength(lines1, lines2 []string) int {
maxLen := 0
for _, line := range lines1 {
if len(line) > maxLen {
maxLen = len(line)
}
}
for _, line := range lines2 {
if len(line) > maxLen {
maxLen = len(line)
}
}
return maxLen
}