Module: Analysis
- Defined in:
- lib/dekryptos/analysis.rb
Overview
Tools for analyzing suggested Kryptos solutions.
Instance Method Summary collapse
-
#anagram(text) ⇒ Object
Public: Provides all anagrams for a given word.
-
#anagram?(phrase_one, phrase_two) ⇒ Boolean
Public: Checks whether one word or phrase is an anagram of another.
-
#factor(n) ⇒ Object
Public: Provides all factors of a given number.
-
#frequency(text) ⇒ Object
Public: Performs frequency analysis on the provided text.
-
#ioc(text) ⇒ Object
Public: Calculates the index of coincidence for the given text (see en.wikipedia.org/wiki/ Index_of_coincidence).
-
#vowels(text) ⇒ Object
Public: Calculates the percentage of text composed of vowels.
Instance Method Details
#anagram(text) ⇒ Object
Public: Provides all anagrams for a given word.
Parameter(s)
text - String: The text to anagram.
Return Value
Array<String>: A list of anagrams. rubocop: disable MethodLength, UnderscorePrefixedVariableName
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dekryptos/analysis.rb', line 34 def anagram(text) head, tail = [], text.split('') stack = [[head, tail]] result = [] while stack.size > 0 head, tail = stack.pop if tail.size.zero? result << head else tail.each_with_index do |_, i| _tail = tail.dup curr = _tail.delete_at(i) _head = head.dup _head << curr stack << [_head, _tail] end end end result.map! { |p| p.join('') } result.uniq! result end |
#anagram?(phrase_one, phrase_two) ⇒ Boolean
Public: Checks whether one word or phrase is an anagram of another.
Parameter(s)
phrase_one - String: A word or phrase. phrase_two - String: A word or phrase that may be an anagram of phrase_one.
Return Value
Boolean: True if one phrase is an anagram of the other, false otherwise.
71 72 73 |
# File 'lib/dekryptos/analysis.rb', line 71 def anagram?(phrase_one, phrase_two) phrase_one.split('').sort == phrase_two.split('').sort end |
#factor(n) ⇒ Object
Public: Provides all factors of a given number.
Parameter(s)
n - Integer: The number to factor.
Return Value
Array<Integer>: A list of factors.
17 18 19 20 21 22 23 24 |
# File 'lib/dekryptos/analysis.rb', line 17 def factor(n) factors = [] (1..n).each do |x| factors << x if n % x == 0 end factors end |
#frequency(text) ⇒ Object
Public: Performs frequency analysis on the provided text.
Parameter(s)
text - String: The text to analyze.
Return Value
Hash<String, Integer>: A hash representing the letter frequencies.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dekryptos/analysis.rb', line 84 def frequency(text) text = text.downcase.gsub(/\s*/, '') chars = text.split('') freqs = Hash[('a'..'z').to_a.zip([0] * 26)] chars.each do |c| freqs[c] += 1 end freqs end |
#ioc(text) ⇒ Object
Public: Calculates the index of coincidence for the given text (see en.wikipedia.org/wiki/ Index_of_coincidence).
Parameter(s)
text - String: The text to analyze.
Return Value
Float: The index of coincidence for the text.
105 106 107 108 109 110 111 112 |
# File 'lib/dekryptos/analysis.rb', line 105 def ioc(text) c = 26.0 freqs = frequency(text) n = text.length f = freqs.values.reduce(0) { |a, e| a + (e * (e - 1)) } f / ((n * (n - 1)) / c) end |
#vowels(text) ⇒ Object
Public: Calculates the percentage of text composed of vowels. (Y is not included as a vowel.)
Parameter(s)
text - String: The text to analyze.
Return Value
Float: The percentage comprising vowels.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/dekryptos/analysis.rb', line 122 def vowels(text) vowels = %w(A E I O U) chars = text.upcase.split('') vowel_count = 0 chars.each do |c| vowel_count += 1 if vowels.include? c end vowel_count.to_f / text.length end |