Last active
February 16, 2016 09:39
-
-
Save 5t111111/ae174a7a53506d28e0a7 to your computer and use it in GitHub Desktop.
ケブンリッジにシャッフルのテストを
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
require 'strscan' | |
module StringWithCabmridge | |
refine String do | |
def cabmridge_word | |
self[0] + self[1..-2].chars.shuffle.*('') + (self[1] ? self[-1] : '') | |
end | |
def cabmridge_whole_text | |
buffer = StringScanner.new(self) | |
result = '' | |
until buffer.eos? | |
word = buffer.scan_until(/[[:blank:]]|\n|$/) | |
result << if word =~ /\A([[:blank:]]|\n)\z/ | |
$~.to_s | |
elsif word =~ /[[:blank:]]|\n/ | |
word.chop.cabmridge_word + $~.to_s | |
else | |
word.cabmridge_word | |
end | |
end | |
result | |
end | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
using StringWithCabmridge | |
puts "ケンブリッジ".cabmridge_word | |
puts "ケン".cabmridge_word | |
puts "ケ".cabmridge_word | |
end |
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
require 'minitest/autorun' | |
require_relative 'cabmridge' | |
using StringWithCabmridge | |
class StringWithCabmridgeTest < Minitest::Test | |
def setup | |
@word = 'cambridge' | |
end | |
def test_cabmridge_word_does_not_change_first_character | |
assert_equal 'c', @word.cabmridge_word[0] | |
end | |
def test_cabmridge_word_does_not_change_last_character | |
assert_equal 'e', @word.cabmridge_word[-1] | |
end | |
def test_cabmridge_word_does_not_change_characters_but_order | |
assert_equal 'ambridg'.chars.sort, @word.cabmridge_word[1..-2].chars.sort | |
end | |
def test_cabmridge_word_really_shuffles_characters | |
hash = { | |
'a' => [0, 0, 0, 0, 0, 0, 0], | |
'm' => [0, 0, 0, 0, 0, 0, 0], | |
'b' => [0, 0, 0, 0, 0, 0, 0], | |
'r' => [0, 0, 0, 0, 0, 0, 0], | |
'i' => [0, 0, 0, 0, 0, 0, 0], | |
'd' => [0, 0, 0, 0, 0, 0, 0], | |
'g' => [0, 0, 0, 0, 0, 0, 0] | |
} | |
sample_number = 100_000 | |
results = sample_number.times.each_with_object(hash) do |_i, h| | |
shuffled_characters = @word.cabmridge_word[1..-2].chars | |
shuffled_characters.each_with_index do |c, j| | |
h[c][j] += 1 | |
end | |
end | |
expected_value = sample_number.to_f / results.length | |
permissible_range = (expected_value * 0.95)..(expected_value * 1.05) | |
results.each_value do |v| | |
v.each do |n| | |
assert_includes permissible_range, n | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment