-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piper Wolters
committed
Jun 21, 2019
1 parent
98c2ee3
commit d1c8411
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
THE SEVEN OLD SAMURAI | ||
|
||
Once upon a time, in far off Japan, a band of fierce robbers had their | ||
hiding place on top of a mountain almost always covered with grey clouds, | ||
windswept and battered by storms. The robbers lived in a large cave where they | ||
had piled their spoils. Now and again, they went down the mountain, attacked a | ||
village, murdered the poor folk they chanced upon, stole whatever they could | ||
lay hands on and burned it to the ground. Wherever the robbers passed, there | ||
was nothing but smoking ruins, weeping men and women, misery, mournlng and | ||
desolation. | ||
The Emperor, worried at this, had sent his soldiers to attack the mountain, | ||
but the robbers had always managed to drive them off. The Emperor sent for one | ||
of the last remaining Samurai, old Raiko, and said to him: | ||
"Raiko, you've served me for many years. Do my bidding for one last time. | ||
Go to the mountain at the head of an army and wipe out these bloodthirsty | ||
bandits." Raiko sighed. | ||
"Your Majesty, if I were young again I'd do it alone. Today I'm too old, | ||
far too old to do that, or to command an army." | ||
"Must I then," said the Emperor, "submit to the force of these marauding | ||
robbers?" The old Samurai replied: | ||
"No, I'll go up there with six Samurai like myself." | ||
"But if they're all as old as you, how can they help you?" | ||
"Have faith in us!" said Raiko. | ||
A few days later, the seven Samurai set off on their journey, not with | ||
horses, swords, shields and armour, which they could no longer have worn | ||
anyway, but dressed as humble pilgrims. From the summit, the bandits watched | ||
them come, and their leader said, | ||
"Who cares about seven beggars. Let them climb up." The seven reached the | ||
cave and Raiko humbly said, | ||
"Let us come in, it's cold outside. There's a wind blowing and we, as you | ||
can see, are old men. We'll be no trouble to you." The leader of the gang | ||
scornfully replied: | ||
"Come in, old men, and stay in a corner." And so. the seven pilgrims | ||
huddled in a corner while the bandits ate their meal of food stolen from the | ||
villages nearby. | ||
Now and again, they threw scraps of food and leftovers to the old men, | ||
saying: "Eat this, and it is much too good for you." A few hours later, Raiko | ||
rose to his feet saying: | ||
"The wind has dropped. We can go on our way. In thanks. for your | ||
hospitality, we would like to offer you this liqueur, it is sake, rice wine. | ||
Drink our health with it." The robbers needed no second telling. In the blink | ||
of an eye, they had emptied the goatskin bottle Raika held out to them. And in | ||
the blink of an eye they all lay dead, for the sake contained a very potent | ||
poison. And so, the seven Samurai, too old to wield a sword, served the | ||
Emperor for the last time. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
|
||
func word_count_test(t *testing.T) { | ||
words := scanWords("../7oldsamr.txt") | ||
|
||
length := len(words) | ||
v1 := words[0] | ||
v2 := words[100] | ||
|
||
if length != 497 { | ||
t.Error( | ||
"For the length, expected 497, got", length, | ||
) | ||
} | ||
if v1 != "THE" { | ||
t.Error( | ||
"For words[0], expected THE, got", words[0], | ||
) | ||
} | ||
if v2 != "Emperor" { | ||
t.Error( | ||
"For words[100], expected Emperor, got", words[100], | ||
) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// scanWords scans the file to parse the strings | ||
func scanWords(path string) []string { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
|
||
scanner.Split(bufio.ScanWords) | ||
|
||
var words []string | ||
|
||
for scanner.Scan() { | ||
word := strings.Replace(scanner.Text(), ".", "", -1) | ||
word = strings.Replace(scanner.Text(), ",", "", -1) | ||
word = strings.Replace(scanner.Text(), "?", "", -1) | ||
word = strings.Replace(scanner.Text(), ":", "", -1) | ||
if word[0] == '"' { | ||
word = word[1:] | ||
} | ||
if word[len(word)-1] == '"' { | ||
word = word[:len(word)-1] | ||
} | ||
words = append(words, word) | ||
} | ||
return words | ||
} | ||
|
||
// sortWords is the logic to reverse sort the words according to frequencies | ||
func sortWords(words []string, m map[string]int) { | ||
for _, word := range words { | ||
m[strings.ToLower(word)] += 1 | ||
} | ||
|
||
// Creating another map to swap the positions of keys and values | ||
n := map[int][]string{} | ||
var a []int | ||
|
||
// Filling the map | ||
for k, v := range m { | ||
n[v] = append(n[v], k) | ||
} | ||
for k := range n { | ||
a = append(a, k) | ||
} | ||
|
||
// Reverse sorting the word frequencies and their respective keys | ||
sort.Sort(sort.Reverse(sort.IntSlice(a))) | ||
|
||
// Printing the output | ||
for _, k := range a { | ||
for _, s := range n[k] { | ||
fmt.Printf("%s, %d\n", s, k) | ||
} | ||
} | ||
} | ||
|
||
// Main function | ||
func main() { | ||
|
||
// Checking for the necessary command line argument | ||
if len(os.Args) <= 1 { | ||
fmt.Println("Command line argument missing") | ||
return | ||
} | ||
|
||
// Initilizing a map | ||
m := make(map[string]int) | ||
|
||
// Scanning through the text file and adding initial info to the map | ||
words := scanWords(os.Args[1]) | ||
|
||
// Sort the words by frequencies | ||
sortWords(words, m) | ||
} |