lib/chadet.rb
# check if guess has redundant characters
check if guess has redundant characters
check if guess has redundant characters
def handle_redundancy
      char_freq = {}
      @guess.each_char do |char|
        char_freq[char] ? char_freq[char] += 1 : char_freq[char] = 1
      end
      redundant_char = char_freq.select {|k, v| v > 1}
      sorted_red_char = redundant_char.sort_by {|k, v| v}
      sorted_red_char.reverse! unless sorted_red_char[1].nil? || sorted_red_char[0][1] == sorted_red_char[1][1]
      redundant = sorted_red_char[0][0]
      frequency = sorted_red_char[0][1]
      if frequency == 2
        freq_string = "twice"
      else
        freq_string = "#{frequency} times"
      end
      "Redundant: you typed \"#{redundant}\" #{freq_string}.".yellow.flash
    end
def handle_redundancy char_freq = { } @guess . each_char do | char | char_freq [ char ] ? char_freq [ char ] += 1 : char_freq [ char ] = 1 end redundant_char = char_freq . select { | k , v | v > 1 } sorted_red_char = redundant_char . sort_by { | k , v | v } sorted_red_char . reverse! unless sorted_red_char [ 1 ] . nil? || sorted_red_char [ 0 ] [ 1 ] == sorted_red_char [ 1 ] [ 1 ] redundant = sorted_red_char [ 0 ] [ 0 ] frequency = sorted_red_char [ 0 ] [ 1 ] if frequency == 2 freq_string = "twice" else freq_string = "#{frequency} times" end "Redundant: you typed \"#{redundant}\" #{freq_string}." . yellow . flash end
check if guess has redundant characters
check if guess has redundant characters