From 6033bf645ce3ad13b42e5cbe32a08d95b5b3e645 Mon Sep 17 00:00:00 2001 From: Jeffrey ODell Date: Mon, 9 Aug 2010 13:46:37 -0500 Subject: [PATCH] String#to/from_morse, tests --- ext/string.rb | 13 ++++++++ lib/toycipher/morse.rb | 18 ++++------ script/pretty.rb | 73 +++++++++++++++++++++++++++++++++++++++++ test/tc_morse.rb | 11 ++----- test/tc_substitution.rb | 4 +-- 5 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 script/pretty.rb diff --git a/ext/string.rb b/ext/string.rb index 2628434..f1fcc3c 100644 --- a/ext/string.rb +++ b/ext/string.rb @@ -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' @@ -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 diff --git a/lib/toycipher/morse.rb b/lib/toycipher/morse.rb index 9b9ae73..007082f 100644 --- a/lib/toycipher/morse.rb +++ b/lib/toycipher/morse.rb @@ -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) diff --git a/script/pretty.rb b/script/pretty.rb new file mode 100644 index 0000000..1c3a77d --- /dev/null +++ b/script/pretty.rb @@ -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 diff --git a/test/tc_morse.rb b/test/tc_morse.rb index e169cb6..d0e671b 100644 --- a/test/tc_morse.rb +++ b/test/tc_morse.rb @@ -11,7 +11,7 @@ class TestMorse < Test::Unit::TestCase def setup @cipher = ToyCipher::Morse.new - @mc2010_ciphetext = '.... .- ...- . ..-. ..- -. .--. .- ... ... .-- --- .-. -.. --... ..... ..... --... .... . .-.. .-.. --- .... .- ...- . ..-. ..- -.' + @mc2010_ciphertext = '.... .- ...- . ..-. ..- -. .--. .- ... ... .-- --- .-. -.. --... ..... ..... --... .... . .-.. .-.. --- .... .- ...- . ..-. ..- -.' @mc2010_plaintext = 'HAVEFUNPASSWORD7557HELLOHAVEFUN' end @@ -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 diff --git a/test/tc_substitution.rb b/test/tc_substitution.rb index 4e8004f..a25d674 100644 --- a/test/tc_substitution.rb +++ b/test/tc_substitution.rb @@ -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'