Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://old.reddit.com/r/dailyprogrammer/comments/ffxabb/20200309_challenge_383_easy_necklace_matching/
- require "test/unit"
- require "open-uri"
- def same_necklace(original, result)
- if original.length != result.length
- return false
- end
- if result.empty?
- return true
- end
- acc = original
- for i in 0...original.length do
- char = acc.slice!(0)
- acc << char
- if acc.eql? result
- return true
- end
- end
- return false
- end
- class TestSameNecklace < Test::Unit::TestCase
- def tests
- assert_equal(same_necklace("nicole", "icolen"), true)
- assert_equal(same_necklace("nicole", "lenico"), true)
- assert_equal(same_necklace("nicole", "coneli"), false)
- assert_equal(same_necklace("aabaaaaabaab", "aabaabaabaaa"), true)
- assert_equal(same_necklace("abc", "cba"), false)
- assert_equal(same_necklace("xxyyy", "xxxyy"), false)
- assert_equal(same_necklace("xyxxz", "xxyxz"), false)
- assert_equal(same_necklace("x", "x"), true)
- assert_equal(same_necklace("x", "xx"), false)
- assert_equal(same_necklace("x", ""), false)
- assert_equal(same_necklace("", ""), true)
- end
- end
- def repeats s
- if s.length == 1 or s.length == 0
- return 1
- end
- hash = Hash.new
- acc = s
- for i in 0...s.length do
- char = acc.slice!(0)
- acc << char
- if hash[acc] != nil
- hash[acc] += 1
- else
- hash[acc] = 1
- end
- end
- count_times = []
- hash.each { |_, value| count_times.push value}
- max = count_times.max
- return max
- end
- class TestRepeat < Test::Unit::TestCase
- def tests
- assert_equal(repeats("abc"), 1)
- assert_equal(repeats("abcabcabc"), 3)
- assert_equal(repeats("abcabcabcx"), 1)
- assert_equal(repeats("aaaaaa"), 6)
- assert_equal(repeats("a"), 1)
- assert_equal(repeats(""), 1)
- end
- end
- def download_file
- url = "https://raw.githubusercontent.com/dolph/dictionary/master/enable1.txt"
- File.open("./dict.txt", "wb") do |file|
- file.write open(url).read
- end
- end
- def find_four_words
- filename = "./dict.txt"
- if !File.exist?(filename)
- download_file
- end
- dict = []
- File.open(filename, "r") do |file|
- file.each do |line|
- dict << line.strip
- end
- end
- hash = Hash.new
- dict.each do |word|
- acc = word.strip
- for i in 0...word.length
- char = acc.slice!(0)
- acc << char
- if hash[acc] != nil
- hash[acc] += 1
- else
- hash[acc] = 1
- end
- end
- end
- hash_with_4_times = hash.select {|k, v| v == 4}
- result = []
- dict.each do |word_in_dict|
- if hash_with_4_times[word_in_dict] != nil
- result.push word_in_dict
- end
- end
- return result
- end
- class TestFindFourWords < Test::Unit::TestCase
- def test_find_four_words
- result = find_four_words()
- assert_equal(result.include?("estop"), true)
- assert_equal(result.include?("pesto"), true)
- assert_equal(result.include?("stope"), true)
- assert_equal(result.include?("topes"), true)
- end
- end
Add Comment
Please, Sign In to add comment