Skip to content

Instantly share code, notes, and snippets.

@telyn
Created September 17, 2019 15:03
Show Gist options
  • Save telyn/66918c8f993c251223f4084fbd78fca7 to your computer and use it in GitHub Desktop.
Save telyn/66918c8f993c251223f4084fbd78fca7 to your computer and use it in GitHub Desktop.
Find out what the mean string is from a yaml document of strings (for a ... particular definition of "mean string")
require 'set'
# Calculates the mean string
class MeanString
def initialize(strings)
@strings = strings
end
def mean
(0...average_string_length)
.to_a
.map(&method(:mean_character))
.join
end
private
attr_reader :strings
def average_string_length
@average_string_length ||=
(strings.map(&:length).sum.to_f / strings.count)
.round
end
def mean_character(idx)
char_indices = strings_with_length_at_least(idx + 1)
.map { |a| a[idx] }
.map { |c| all_chars.index(c) }
mean_char_index = (indices.sum.to_f / indices.count).round
all_chars[mean_char_index]
end
def strings_with_length_at_least(len)
strings.reject { |a| a.length < len }
end
# constructs a set of all possible characters - this allows us to use a "mean"
# and get a value that's actually inside the possible characters, and
# minimises the chances of getting a non-alphanumeric
def all_chars
@all_chars ||= Set.new(strings.map { |x| x.split('') }.flatten).sort
end
end
require 'yaml'
# the yaml doc should be a simple array of strings, e.g.:
# - string1
# - string2
strings = YAML.safe_load(IO.read('strings.yml'))
puts MeanString.new(strings).mean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment