Created
July 29, 2018 17:25
-
-
Save lkrych/7a14eac84c6f7651d2fa51e1983750ad to your computer and use it in GitHub Desktop.
Skeleton for sorting input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"math/rand" | |
"os" | |
"strings" | |
"time" | |
flags "github.com/jessevdk/go-flags" | |
) | |
//Write a filter that reads strings from standard input | |
//and prints them to standard output, in sorted order with all duplicate strings removed. | |
var opts struct { | |
File string `short:"f" long:"file" default:"generatedStrings.txt" description:"a file to read strings from."` | |
NumWords int `short:"n" long:"num" default:"500" description:"a number of strings to create."` | |
} | |
func main() { | |
//interpret the CL stdin | |
flags.Parse(&opts) | |
//check if file is default, if so, create the file if it doesn't exist | |
if opts.File == "generatedStrings.txt" { | |
createFile(opts.NumWords) | |
} | |
//read from the file | |
file, err := ioutil.ReadFile(opts.File) | |
checkErr(err) | |
//create an array of strings that is split by the newline character | |
splitByNewline := strings.Split(string(file), "\n") | |
//deduplicate words by throwing them into hash | |
deDup := map[string]bool{} | |
for _, word := range splitByNewline { | |
deDup[word] = true | |
} | |
//get an array of keys from the hash | |
keys := make([]string, len(deDup)) | |
i := 0 | |
for k := range deDup { | |
keys[i] = k | |
i++ | |
} | |
//sort the keys | |
sorted := quickSort(keys) | |
for _, word := range sorted { | |
//print to stdout | |
fmt.Println(word) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment