Skip to content

Commit

Permalink
String#to/from_morse, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jodell committed Aug 9, 2010
1 parent bbbcdea commit 6033bf6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 21 deletions.
13 changes: 13 additions & 0 deletions ext/string.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'toycipher'
class String
LETTER_A_VALUE = 97 unless defined?(LETTER_A_VALUE) # ASCII value of 'a'

Expand Down Expand Up @@ -43,8 +45,19 @@ def phone(type = :default)
end
end
end

# NOTE: Might want to switch to class method to avoid all of these allocations
def to_morse
ToyCipher::Morse.new.encrypt(self)
end

def from_morse
ToyCipher::Morse.new.decrypt(self)
end
end

#puts "abcdefghijklmnopqrstuvwxyz".phone
#puts "ABCDEFGHIJKLMNOPQRSTUVWXYZ".phone(:legacy)
#puts "THIS IS A TEST".phone
#puts '.... .- ...- . ..-. ..- -. .--. .- ... ... .-- --- .-. -.. --... ..... ..... --... .... . .-.. .-.. --- .... .- ...- . ..-. ..- -.'.from_morse
#puts 'HAVEFUNPASSWORD7557HELLOHAVEFUN'.to_morse
18 changes: 7 additions & 11 deletions lib/toycipher/morse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,17 @@ def initialize
end

def encrypt(plaintext = @plaintext)
result = ''
plaintext.split(' ').each do |c|
next unless c =~ /\.|-/
result += LETTER[c]
end
result
plaintext.upcase.split(//).inject([]) do |acc, c|
next unless LETTER.keys.include?(c)
acc << LETTER[c]
end * ' '
end

def decrypt(ciphertext = @ciphertext)
result = ''
ciphertext.split(' ').each do |c|
next unless c =~ /\.|-/
result += LETTER.invert[c]
ciphertext.split(' ').inject('') do |acc, c|
next unless LETTER.values.include?(c)
acc += LETTER.invert[c]
end
result
end

def normalize(string)
Expand Down
73 changes: 73 additions & 0 deletions script/pretty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env ruby

# This will evolve into word recognition

#puts "initializing dict file"
#puts "dict file has #{DICT.size} words"

class String
DICT_FILE = '/usr/share/dict/words'
def dict
@@dict ||= IO.readlines(DICT_FILE)
end

def word?
!dict.grep(/^#{self}\n/i).empty? && self.size != 1
end
end

#test = 'thisisatest'
test = 'thisisamuchlargerfuckingtest'

puts "Testing: #{test}"
puts "basic: #{test.word?}"

@start = test.split(//).first
@end = @start.dup
@words = []
test.split(//).each do |char|
@current = char
end

def gross_check(str)
for i in 0..str.size - 1 do
@current = str[i].chr
puts "Inspecting current letter: #{@current}"
j = i
while j < str.size - 1
puts "checking word status of #{str[i..j]}: #{str[i..j].word?}" if str[i..j].word?
j += 1
end
end
end

def simple_check(str)
i = 0
found = false
while i < str.size - 1 # for the whole string
j = i
while j < str.size - 1
if str[i..j].word?
found = true
puts "word found: #{str[i..j]}"
else
j += 1
end
while found && j < str.size - 1
j += 1
found = false unless str[i..j].word?
end
#puts "current word: #{str[i..j]}"
end
end
end


puts "t: #{'t'.word?}"
puts "th: #{'th'.word?}"
puts "thi: #{'thi'.word?}"
puts "this: #{'this'.word?}"


#gross_check test
simple_check test
11 changes: 3 additions & 8 deletions test/tc_morse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestMorse < Test::Unit::TestCase

def setup
@cipher = ToyCipher::Morse.new
@mc2010_ciphetext = '.... .- ...- . ..-. ..- -. .--. .- ... ... .-- --- .-. -.. --... ..... ..... --... .... . .-.. .-.. --- .... .- ...- . ..-. ..- -.'
@mc2010_ciphertext = '.... .- ...- . ..-. ..- -. .--. .- ... ... .-- --- .-. -.. --... ..... ..... --... .... . .-.. .-.. --- .... .- ...- . ..-. ..- -.'
@mc2010_plaintext = 'HAVEFUNPASSWORD7557HELLOHAVEFUN'
end

Expand All @@ -20,13 +20,8 @@ def teardown

def test_morse
assert @cipher.is_a?(ToyCipher::Morse)
assert_equal @mc2010_plaintext, @cipher.decrypt(@mc2010_ciphertext)
assert_equal @mc2010_ciphertext, @cipher.encrypt(@mc2010_plaintext)
end

def test_cli
assert_equal @mc2010_plaintext, @cipher.decrypt(@mc2010_ciphetext)
end




end
4 changes: 2 additions & 2 deletions test/tc_substitution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def test_substitution
UIQ
EoC
@cipher.key = key
puts @cipher.key.sort.inspect
puts @cipher.decrypt(ciphertext)
#puts @cipher.key.sort.inspect
#puts @cipher.decrypt(ciphertext)

# 'WE SAY AGAIN DELIBERATELY THAT HUMAN INGENUITF CANNOT
# CONCOCT A CYPHER WHICH HUMAN INGENUITY CANNOT RESOLVE'
Expand Down

0 comments on commit 6033bf6

Please sign in to comment.