LoginSignup
0
0

More than 3 years have passed since last update.

【アルゴリズム】文字列から一番出力された文字を探す

Posted at

#一番出力された文字は何??
#input this is a pen. this is an apple. applepen!
#output ('p',6)

def count_check(str)
  #1.メソッドを探す
  #2.配列のループからハッシュのキーとバリューに保存していく。最終的にkey、valueを出力する。
  str_list = str.split('')
  str_list.delete(" ")
  result = Hash.new
  str_list.each do |str|
    #もうすでにハッシュの中にあるのかを確かめる
    if result.key?(str)
      print str
      # result[:str]
      result[str] += 1
    else
      result.store(str,1)
    end
  end
  # #キーが最大の文字列を探す。
  max = 0
  ans = []
  result.each{|key,value|
  if value > max
    ans.pop(2)
    ans.push(key,value)
    max = value
  end
  }
  return ans
end

print(count_check("this is a pen. this is an apple. applepen!"))

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0