Initial Commit

This commit is contained in:
2016-08-13 18:20:14 -05:00
commit 50f4a86fd8
408 changed files with 15301 additions and 0 deletions

View File

@@ -0,0 +1,87 @@
# Crypto Square
Implement the classic method for composing secret messages called a square code.
The input is first normalized: The spaces and punctuation are removed
from the English text and the message is downcased.
Then, the normalized characters are broken into rows. These rows can be
regarded as forming a rectangle when printed with intervening newlines.
For example, the sentence
> If man was meant to stay on the ground god would have given us roots
is 54 characters long.
Broken into 8-character columns, it yields 7 rows.
Those 7 rows produce this rectangle when printed one per line:
```plain
ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots
```
The coded message is obtained by reading down the columns going left to
right.
For example, the message above is coded as:
```plain
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
```
Write a program that, given an English text, outputs the encoded version
of that text.
The size of the square (number of columns) should be decided by the
length of the message.
If the message is a length that creates a perfect square (e.g. 4, 9, 16,
25, 36, etc), use that number of columns.
If the message doesn't fit neatly into a square, choose the number of
columns that corresponds to the smallest square that is larger than the
number of characters in the message.
For example, a message 4 characters long should use a 2 x 2 square. A
message that is 81 characters long would use a square that is 9 columns
wide.
A message between 5 and 8 characters long should use a rectangle 3
characters wide.
Output the encoded text grouped by column.
For example:
- "Have a nice day. Feed the dog & chill out!"
- Normalizes to: "haveanicedayfeedthedogchillout"
- Which has length: 30
- And splits into 5 6-character rows:
- "havean"
- "iceday"
- "feedth"
- "edogch"
- "illout"
- Which yields a ciphertext beginning: "hifei acedl v…"
To run the tests simply run the command `go test` in the exercise directory.
If the test suite contains benchmarks, you can run these with the `-bench`
flag:
go test -bench .
For more detailed info about the Go track see the [help
page](http://help.exercism.io/getting-started-with-go.html).
## Source
J Dalbey's Programming Practice problems [view source](http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html)

View File

@@ -0,0 +1,45 @@
package cryptosquare
import (
"math"
"regexp"
"strings"
)
// TestVersion is an exercism thing
const TestVersion = 1
// Encode implements the classic method for composing
// secret messages called a square code
func Encode(txt string) string {
r := regexp.MustCompile("[^a-zA-Z0-9]+")
nrm := strings.ToLower(r.ReplaceAllString(txt, ""))
// The string should be normalized now (alphanumeric, lower case)
sqrt := int(math.Ceil(math.Sqrt(float64(len(nrm)))))
var square, ret []string
var tmp string
// Build the initial square
for i := 0; i < len(nrm); i++ {
if i%sqrt == 0 && len(tmp) != 0 {
square = append(square, tmp)
tmp = ""
}
tmp += string(nrm[i])
}
square = append(square, tmp)
tmp = ""
// Now rebuild the string by columns
for i := 0; i < len(square[0]); i++ {
for j := 0; j < len(square); j++ {
if i < len(square[j]) {
tmp += string(square[j][i])
}
}
ret = append(ret, tmp)
tmp = ""
}
return strings.Join(ret, " ")
}

View File

@@ -0,0 +1,111 @@
package cryptosquare
import "testing"
const testVersion = 1
// Retired testVersions
// (none) 71ad8ac57fe7d5b777cfa1afd116cd41e1af55ed
var tests = []struct {
pt string // plain text
ct string // cipher text
}{
{
"s#$%^&plunk",
"su pn lk",
},
{
"1, 2, 3 GO!",
"1g 2o 3",
},
{
"1234",
"13 24",
},
{
"123456789",
"147 258 369",
},
{
"123456789abc",
"159 26a 37b 48c",
},
{
"Never vex thine heart with idle woes",
"neewl exhie vtetw ehaho ririe vntds",
},
{
"ZOMG! ZOMBIES!!!",
"zzi ooe mms gb",
},
{
"Time is an illusion. Lunchtime doubly so.",
"tasney inicds miohoo elntu illib suuml",
},
{
"We all know interspecies romance is weird.",
"wneiaw eorene awssci liprer lneoid ktcms",
},
{
"Madness, and then illumination.",
"msemo aanin dnin ndla etlt shui",
},
{
"Vampires are people too!",
"vrel aepe mset paoo irpo",
},
{
"",
"",
},
{
"1",
"1",
},
{
"12",
"1 2",
},
{
"12 3",
"13 2",
},
{
"12345678",
"147 258 36",
},
{
"123456789a",
"159 26a 37 48",
},
{
"If man was meant to stay on the ground god would have given us roots",
"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau",
},
{
"Have a nice day. Feed the dog & chill out!",
"hifei acedl veeol eddgo aatcu nyhht",
},
}
func TestEncode(t *testing.T) {
if TestVersion != testVersion {
t.Errorf("Found TestVersion = %v, want %v.", TestVersion, testVersion)
}
for _, test := range tests {
if ct := Encode(test.pt); ct != test.ct {
t.Fatalf(`Encode(%q):
got %q
want %q`, test.pt, ct, test.ct)
}
}
}
func BenchmarkEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range tests {
Encode(test.pt)
}
}
}