Skip to content

Commit 289d3a7

Browse files
Merge pull request #244 from ricemery/atbash-cipher
Atbash - use object instead of an instance - refs #242. Add Atbash.sc…
2 parents e30e726 + 39c35b3 commit 289d3a7

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@
182182
"slug": "atbash-cipher",
183183
"difficulty": 1,
184184
"topics": [
185+
"Strings",
186+
"Transforming",
187+
"Security"
185188
]
186189
},
187190
{

exercises/atbash-cipher/example.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
case class Atbash() {
2-
def encode(s: String): String =
3-
s.foldLeft("")((acc, c) => acc + substitute(c)).grouped(5).mkString(" ")
4-
1+
object Atbash {
52
private def substitute(c: Char) =
63
if (c.isDigit) c.toString
74
else if (c.isLetter) ('a' + ('z' - c.toLower)).toChar.toString
85
else ""
6+
7+
def encode(s: String): String =
8+
s.foldLeft("")((acc, c) => acc + substitute(c)).grouped(5).mkString(" ")
99
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Atbash {
2+
def encode(s: String): String = ???
3+
}

exercises/atbash-cipher/src/test/scala/atbash_test.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@ import org.scalatest.{Matchers, FlatSpec}
22

33
class AtbashTest extends FlatSpec with Matchers {
44
it should "encode no" in {
5-
Atbash().encode("no") should equal("ml")
5+
Atbash.encode("no") should equal("ml")
66
}
77

88
it should "encode yes" in {
99
pending
10-
Atbash().encode("yes") should equal("bvh")
10+
Atbash.encode("yes") should equal("bvh")
1111
}
1212

1313
it should "encode OMG" in {
1414
pending
15-
Atbash().encode("OMG") should equal("lnt")
15+
Atbash.encode("OMG") should equal("lnt")
1616
}
1717

1818
it should "encode lowercase omg" in {
1919
pending
20-
Atbash().encode("omg") should equal("lnt")
20+
Atbash.encode("omg") should equal("lnt")
2121
}
2222

2323
it should "encode O M G" in {
2424
pending
25-
Atbash().encode("O M G ") should equal("lnt")
25+
Atbash.encode("O M G ") should equal("lnt")
2626
}
2727

2828
it should "encode and group string " in {
2929
pending
30-
Atbash().encode("mindblowingly") should equal("nrmwy oldrm tob")
30+
Atbash.encode("mindblowingly") should equal("nrmwy oldrm tob")
3131
}
3232

3333
it should "encode string with digits and punctuation" in {
3434
pending
35-
Atbash().encode("Testing, 1 2 3, testing. ") should equal("gvhgr mt123 gvhgr mt")
35+
Atbash.encode("Testing, 1 2 3, testing. ") should equal("gvhgr mt123 gvhgr mt")
3636
}
3737

3838
it should "encode \"Truth is fiction.\"" in {
3939
pending
40-
Atbash().encode("Truth is fiction.") should equal("gifgs rhurx grlm")
40+
Atbash.encode("Truth is fiction.") should equal("gifgs rhurx grlm")
4141
}
4242

4343
it should "encode a long string" in {
4444
pending
45-
Atbash().encode("The quick brown fox jumps over the lazy dog.") should
45+
Atbash.encode("The quick brown fox jumps over the lazy dog.") should
4646
equal("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt")
4747
}
4848
}

0 commit comments

Comments
 (0)