-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.go
More file actions
123 lines (103 loc) · 2.65 KB
/
filter.go
File metadata and controls
123 lines (103 loc) · 2.65 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
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
// Sort by name (UTF-8)
func sortPhoneNumbers(phoneNumbers map[string]string) map[string]string {
keyList := []string{}
for key, _ := range phoneNumbers {
keyList = append(keyList, key)
}
sort.Strings(keyList)
ret := map[string]string{}
for i, v := range keyList {
ret[v] = phoneNumbers[keyList[i]]
}
return ret
}
func main() {
// Open the file
file, err := os.Open("contacts.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
// Create a map to store the phone numbers and their corresponding names
phoneNumbers := make(map[string]string)
// Read the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Trim line
line = strings.TrimSpace(line)
// Skip empty lines
if len(line) == 0 {
continue
}
// Split the line by tabs
columns := strings.Split(line, "\t")
// Skip empty lines
if len(columns) < 3 {
continue
}
// Get the name and phone number
name := columns[1]
phoneNumber := columns[2]
// Trim name
name = strings.TrimSpace(name)
// If the phone number not starts with 0, and it starts with 9 accept it otherwise skip it
if len(phoneNumber) > 0 {
if phoneNumber[0] != '0' && phoneNumber[0] != '9' {
continue
} else if phoneNumber[0] == '9' {
// add 0 to the beginning of the phone number
phoneNumber = "0" + phoneNumber
}
}
// Skip phone numbers that are not 11 digits
if len(phoneNumber) != 11 {
continue
}
// Check if the phone number is already in the map
if _, exists := phoneNumbers[phoneNumber]; exists {
continue
}
// Add the phone number and name to the map
phoneNumbers[phoneNumber] = name
}
// Check for errors while reading the file
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return
}
sortedPhoneNumbers := sortPhoneNumbers(phoneNumbers)
_ = sortedPhoneNumbers
// Create a new file to store the filtered results
resultFile, err := os.Create("filtered_contacts.txt")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer resultFile.Close()
// Write the header to the result file
header := "ID\tName\tPhone Number\n"
resultFile.WriteString(header)
// Write the filtered results to the result file
var id int
for phoneNumber, name := range phoneNumbers {
id++
result := fmt.Sprintf("%d\t%s\t%s\n", id, name, phoneNumber)
resultFile.WriteString(result)
}
// Print the filtered results in a table format
fmt.Print(header)
for phoneNumber, name := range phoneNumbers {
id++
fmt.Printf("%d\t%s\t%s\n", id, name, phoneNumber)
}
}