sorted result
This commit is contained in:
parent
925129f76e
commit
f80e2da7e4
2 changed files with 66 additions and 13 deletions
|
@ -3,31 +3,84 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/dariubs/percent"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type letter struct {
|
||||
upperCase string
|
||||
lowerCase string
|
||||
count int
|
||||
frequency float64
|
||||
}
|
||||
|
||||
type letterList []letter
|
||||
|
||||
func (l letterList) Len() int {
|
||||
return len(l)
|
||||
}
|
||||
|
||||
func (l letterList) Less(i, j int) bool {
|
||||
return l[i].frequency > l[j].frequency
|
||||
}
|
||||
|
||||
func (l letterList) Swap(i, j int) {
|
||||
l[i], l[j] = l[j], l[i]
|
||||
}
|
||||
|
||||
var alphabet = []rune{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
}
|
||||
var letters = make([]letter, len(alphabet))
|
||||
|
||||
func letterCount(inputText string) {
|
||||
count := make([]int, len(alphabet))
|
||||
frequency := make([]float64, len(alphabet))
|
||||
totalCount := 0
|
||||
for index, letter := range alphabet {
|
||||
count[index] = strings.Count(inputText, string(letter))
|
||||
count[index] += strings.Count(inputText, string(letter+32))
|
||||
totalCount += count[index]
|
||||
}
|
||||
func initLetterStruct() {
|
||||
for index := range alphabet {
|
||||
frequency[index] = percent.PercentOf(count[index], totalCount)
|
||||
letters[index].upperCase = string(alphabet[index])
|
||||
letters[index].lowerCase = string(alphabet[index] + 32)
|
||||
}
|
||||
for index, letter := range alphabet {
|
||||
fmt.Printf("The letter %c (%c) occurs %d times in the text and the frequency in percent is %0.2f\n", letter, letter+32, count[index], frequency[index])
|
||||
}
|
||||
|
||||
func readFile(relativePath string) string {
|
||||
content, err := ioutil.ReadFile(relativePath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return string(content)
|
||||
}
|
||||
|
||||
func countLetters(inputText string) int {
|
||||
totalCount := 0
|
||||
for index := range alphabet {
|
||||
letters[index].count +=
|
||||
strings.Count(inputText, letters[index].lowerCase) +
|
||||
strings.Count(inputText, letters[index].upperCase)
|
||||
totalCount += letters[index].count
|
||||
}
|
||||
return totalCount
|
||||
}
|
||||
|
||||
func calculateFrequency(totalCount int) {
|
||||
for index := range alphabet {
|
||||
letters[index].frequency = percent.PercentOf(letters[index].count, totalCount)
|
||||
}
|
||||
}
|
||||
|
||||
func printResult() {
|
||||
for index := range alphabet {
|
||||
l := letters[index]
|
||||
fmt.Printf(
|
||||
"The letter %s (%s) occurs %d times in the text and the frequencyArray in percent is %0.2f\n",
|
||||
l.upperCase, l.lowerCase, l.count, l.frequency,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
letterCount("This is a test to check for something.Zz")
|
||||
initLetterStruct()
|
||||
totalCount := countLetters(readFile("plaintext.txt"))
|
||||
calculateFrequency(totalCount)
|
||||
sort.Sort(letterList(letters))
|
||||
printResult()
|
||||
}
|
||||
|
|
2
Lab04/code/part01/plaintext.txt
Normal file
2
Lab04/code/part01/plaintext.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
In cryptography, encryption is the process of encoding information. This process converts the original representation of the information, known as plaintext, into an alternative form known as ciphertext. Only authorized parties can decipher a ciphertext back to plaintext and access the original information. Encryption does not itself prevent interference but denies the intelligible content to a would-be interceptor. For technical reasons, an encryption scheme usually uses a pseudo-random encryption key generated by an algorithm. It is possible to decrypt the message without possessing the key, but, for a well-designed encryption scheme, considerable computational resources and skills are required. An authorized recipient can easily decrypt the message with the key provided by the originator to recipients but not to unauthorized users. Historically, various forms of encryption have been used to aid in cryptography. Early encryption techniques were often utilized in military messaging. Since then, new
|
||||
techniques have emerged and become commonplace in all areas of modern computing. Modern encryption schemes utilize the concepts of public-key and symmetric-key. Modern encryption techniques ensure security because modern computers are inefficient at cracking the encryption.
|
Reference in a new issue