-
-
Save ababup1192/d1c18031beadf19a40c04373f0fde950 to your computer and use it in GitHub Desktop.
RubyMidterm Answer
This file contains 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
age, sex = STDIN.gets.split.map(&:to_i) | |
# caseは式なので代入可 | |
fee = case age | |
when 0..12 then | |
700 | |
when 13..18 then | |
1000 | |
when 19..22 then | |
1200 | |
else | |
1500 | |
end | |
# 200 * sex だと、この問題にしか対応出来ないので三項演算子 | |
p sex == 0 ? fee : fee-200 | |
# ifも式なので、値として出力可能 | |
p (if sex == 0 then | |
fee | |
else | |
fee-200 | |
end) |
This file contains 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
# この時点では配列(arr)ではない、改行をなくしたものを変数に代入 | |
text = STDIN.gets.chomp | |
puts text | |
# 配列にしなくてもStringが長さを求めるメソッドを提供している | |
puts text.size # lengthも可 | |
# Stringがreverseを提供している | |
puts text.reverse | |
puts text.upcase | |
# Rangeで書くと -1で末尾, -2でその前 | |
text[1..-2] | |
# 問題なし | |
puts text.chars.map{|w| (('a' <= w && w <= 'c') ? w.upcase : w)}.join |
This file contains 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
# 問題なし、コピペ | |
arr = STDIN.gets.split.map(&:to_i) | |
def min(arr) | |
arr.inject{ |min_val, n| min_val > n ? n : min_val } | |
end | |
def max(arr) | |
arr.inject{ |min_val, n| min_val < n ? n : min_val } | |
end | |
def sum(arr) | |
arr.inject{ |sum, n| sum + n } | |
end | |
p min(arr) | |
p max(arr) | |
p sum(arr) |
This file contains 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
dir_name, num_str = STDIN.gets.split | |
num = num_str.to_i | |
# 元コードだと、作られるディレクトリ列が正当かどうかテスト不可能 | |
# ディレクトリ名の配列にしておけば、配列に対してのテストコードを書くことが可能になる。 | |
dir_with_perm = (1..num).map{|n| [dir_name + n.to_s, n % 2 == 0 ? 0754 : 0705]} | |
# また、ディレクトリ名の配列にしておけば、Dir.mkdir命令は、一度で済む | |
dir_with_perm.each{|dir_with_perm| Dir.mkdir(dir_with_perm[0], dir_with_perm[1])} | |
This file contains 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
# 7の問題を作るためのプログラム | |
# アナグラム作成 | |
table = ('a'..'z').zip(('a'..'z').to_a.shuffle).shuffle | |
# アナグラムからハッシュを作成 | |
hash = Hash[*table.flatten] | |
# 複合化ハッシュを作成 | |
rev_hash = hash.invert | |
# 暗号化ハッシュを確認 | |
p hash | |
# 暗号化 | |
chipher = "konnitiwarubytanosii".chars.map{|str| rev_hash[str]}.join | |
# 暗号確認 | |
puts chipher | |
# 複合確認 | |
puts chipher.chars.map{|str| hash[str]}.join | |
p table | |
puts table.map{|arr| "\"#{arr[0]}\" -> \"#{arr[1]}\""}.join("\n") | |
This file contains 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
# arr = [] | |
# loop{ | |
# line = gets | |
# if line == nil | |
# break | |
# end | |
# arr.push(line) | |
#} | |
# hash = {} | |
# arr.each{|text| hash.store(text[1],text[8])} | |
# p hash | |
# 以下の辞書を作るためのコード↑ | |
dict = {"q"=>"e", "t"=>"p", "b"=>"b", "n"=>"w", "j"=>"s", "u"=>"t", "w"=>"z", "c"=>"v", "k"=>"i", "d"=>"r", "p"=>"u", "h"=>"q", "x"=>"m", "z"=>"x", "v"=>"h", "l"=>"k", "s"=>"j", "i"=>"a", "f"=>"d", "r"=>"y", "a"=>"c", "m"=>"n", "e"=>"f", "y"=>"o", "g"=>"g", "o"=>"l"} | |
cipher = "lymmkuknidpbruimyjkk" | |
# 文字を文字に変換 なので、畳み込みではなくmap | |
puts cipher.chars.map{|c| dict[c]}.join |
This file contains 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
men = ["SuzukiMakoto", "OgataRyo", "TanakaMichael", "TanakaTaro"] | |
women = ["KobayashiSaki", "AizuMisaki", "ShibuyaRin"] | |
# ファーストネームを取り出す | |
def get_fstn(name) | |
name.chars.drop(1).drop_while{|n| ('a'..'z').include?(n) }.join | |
end | |
# ファミリーネームを取り出す | |
def get_fmn(name) | |
name.gsub(get_fstn(name), "") | |
end | |
# 正規表現を使う場合(一応一年生いこういった解答もあると紹介) | |
def split_name(name) | |
/([A-Z][a-z]+)([A-Z][a-z]+)/ =~ name | |
[$1, $2] | |
end | |
# 今回用 | |
puts men.zip(women).select{|mw| mw[0] != nil && mw[1] != nil }. | |
map{|mw| get_fmn(mw[0]) + get_fstn(mw[1])}.join("\t") | |
# 正規表現を使用 | |
puts men.zip(women).select{|mw| mw[0] != nil && mw[1] != nil }. | |
map{|mw| split_name(mw[0])[0] + split_name(mw[1])[1]}.join("\t") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment