Created
June 6, 2014 09:15
-
-
Save jadon1979/cc0c595e06d281567187 to your computer and use it in GitHub Desktop.
Code Tests: Words Sequencer (sequencer.rb w/ sample output text files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| aaas | |
| aarh | |
| arhu | |
| rhus | |
| aaro | |
| abab | |
| bacu | |
| balo | |
| bbas | |
| bbey |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AAAS | |
| Aarhus | |
| Aarhus | |
| Aarhus | |
| Aaron | |
| Ababa | |
| abacus | |
| abalone | |
| abbas | |
| abbey |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Sequencer | |
| def initialize(data) | |
| parse_data(data) | |
| end | |
| # build the sequence hash and save the output | |
| def parse_data(data) | |
| sequence_hash = build_sequence_hash(data) | |
| save_file('sequences.txt', sequence_hash.keys.join("\n")) | |
| save_file('words.txt', sequence_hash.values.join("\n")) | |
| end | |
| # Iterate dictionary data and create | |
| # a sequence => word hash | |
| def build_sequence_hash(data) | |
| data.inject({}) do |repo, word| | |
| scan_for_sequences(repo, word) | |
| end.reject!{|sequence, word| word.count > 1 } | |
| end | |
| # Scan individual words for potential sequences | |
| # and assign words to their corresponding sequence | |
| def scan_for_sequences(repo, word) | |
| word.scan(/(?=([A-Za-z]{4}))/).flatten.each do |seq| | |
| set_sequence_key(repo, seq.downcase, word) | |
| end | |
| repo | |
| end | |
| # Verify if sequence hash key exists. | |
| # If the key does not exists then create an array with | |
| # the necessary word value else append the word value to the | |
| # existing word array | |
| def set_sequence_key(repo, key, value) | |
| repo.has_key?(key) ? repo[key] << value : repo[key] = [value] | |
| end | |
| # Split hash and save the results to their | |
| # respective destinations('sequences.txt', 'words.txt') | |
| def save_file(file_name, contents) | |
| file_path = File.join(__dir__, file_name) | |
| File.open(file_path, 'w+') { |f| f.write(contents) } | |
| end | |
| end | |
| dictionary_file = File.join(__dir__, 'dictionary.txt') | |
| dictionary = IO.read(dictionary_file).split | |
| Sequencer.new(dictionary) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment