Guest User

383

a guest
Mar 24th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.05 KB | None | 0 0
  1. # https://old.reddit.com/r/dailyprogrammer/comments/ffxabb/20200309_challenge_383_easy_necklace_matching/
  2.  
  3. require "test/unit"
  4. require "open-uri"
  5.  
  6.  
  7. def same_necklace(original, result)
  8.   if original.length != result.length
  9.     return false
  10.   end
  11.  
  12.   if result.empty?
  13.     return true
  14.   end
  15.  
  16.   acc = original
  17.   for i in 0...original.length do
  18.     char = acc.slice!(0)
  19.     acc << char
  20.     if acc.eql? result
  21.       return true
  22.     end
  23.   end
  24.  
  25.   return false
  26. end
  27.  
  28. class TestSameNecklace < Test::Unit::TestCase
  29.   def tests
  30.     assert_equal(same_necklace("nicole", "icolen"), true)
  31.     assert_equal(same_necklace("nicole", "lenico"), true)
  32.     assert_equal(same_necklace("nicole", "coneli"), false)  
  33.     assert_equal(same_necklace("aabaaaaabaab", "aabaabaabaaa"), true)
  34.     assert_equal(same_necklace("abc", "cba"), false)  
  35.     assert_equal(same_necklace("xxyyy", "xxxyy"), false)  
  36.     assert_equal(same_necklace("xyxxz", "xxyxz"), false)  
  37.     assert_equal(same_necklace("x", "x"), true)
  38.     assert_equal(same_necklace("x", "xx"), false)
  39.     assert_equal(same_necklace("x", ""), false)
  40.     assert_equal(same_necklace("", ""), true)
  41.   end
  42. end
  43.  
  44.  
  45. def repeats s
  46.   if s.length == 1 or s.length == 0
  47.     return 1
  48.   end
  49.  
  50.   hash = Hash.new
  51.   acc = s
  52.  
  53.   for i in 0...s.length do
  54.     char = acc.slice!(0)
  55.     acc << char
  56.    
  57.     if hash[acc] != nil
  58.       hash[acc] += 1
  59.     else
  60.       hash[acc] = 1
  61.     end
  62.   end
  63.  
  64.   count_times = []
  65.   hash.each { |_, value| count_times.push value}
  66.  
  67.   max = count_times.max
  68.  
  69.   return max
  70. end
  71.  
  72.  
  73. class TestRepeat < Test::Unit::TestCase
  74.   def tests
  75.     assert_equal(repeats("abc"), 1)
  76.     assert_equal(repeats("abcabcabc"), 3)
  77.     assert_equal(repeats("abcabcabcx"), 1)
  78.     assert_equal(repeats("aaaaaa"), 6)
  79.     assert_equal(repeats("a"), 1)
  80.     assert_equal(repeats(""), 1)
  81.   end
  82. end
  83.  
  84. def download_file
  85.   url = "https://raw.githubusercontent.com/dolph/dictionary/master/enable1.txt"
  86.  
  87.   File.open("./dict.txt", "wb") do |file|
  88.     file.write open(url).read
  89.   end
  90. end
  91.  
  92. def find_four_words
  93.   filename = "./dict.txt"
  94.  
  95.   if !File.exist?(filename)
  96.     download_file
  97.   end
  98.  
  99.  
  100.   dict = []
  101.   File.open(filename, "r") do |file|
  102.     file.each do |line|
  103.       dict << line.strip
  104.     end
  105.   end
  106.    
  107.   hash = Hash.new
  108.   dict.each do |word|
  109.     acc = word.strip
  110.     for i in 0...word.length
  111.       char = acc.slice!(0)
  112.       acc << char
  113.  
  114.       if hash[acc] != nil
  115.         hash[acc] += 1
  116.       else
  117.         hash[acc] = 1
  118.       end
  119.     end
  120.   end
  121.  
  122.   hash_with_4_times = hash.select {|k, v| v == 4}
  123.  
  124.   result = []
  125.   dict.each do |word_in_dict|
  126.     if hash_with_4_times[word_in_dict] != nil
  127.       result.push word_in_dict
  128.     end
  129.   end
  130.  
  131.     return result
  132. end
  133.  
  134.  
  135. class TestFindFourWords < Test::Unit::TestCase
  136.   def test_find_four_words
  137.     result = find_four_words()
  138.     assert_equal(result.include?("estop"), true)
  139.     assert_equal(result.include?("pesto"), true)
  140.     assert_equal(result.include?("stope"), true)
  141.     assert_equal(result.include?("topes"), true)
  142.   end
  143. end
Add Comment
Please, Sign In to add comment