Last active
April 24, 2017 03:43
-
-
Save sukechannnn/3b4a25d965feff7e449be4a33c23e9c7 to your computer and use it in GitHub Desktop.
たのしいRuby練習問題
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
たのしい Ruby | |
Rubyは たのしい | |
Ruby is fun. |
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
# Chapter 12 | |
module Chapter12 | |
class Exercise1 | |
def self.cels_to_fahr(cels) | |
cels.to_f * 9 / 5 + 32 | |
end | |
end | |
class Exercise2 | |
def self.fahr_to_cels(fahr) | |
(fahr.to_f - 32) * 5 / 9 | |
end | |
def self.correspondence_table_cels_to_fahr | |
1.upto(100) do |cels| | |
fahr = Exercise1.cels_to_fahr(cels) | |
puts "cels = #{cels.to_f}, fahr = #{fahr}" | |
end | |
end | |
end | |
class Exercise3 | |
def self.dice | |
rand(1..6) | |
end | |
end | |
class Exercise4 | |
def self.dice10 | |
sum = 0 | |
10.times do | |
sum += Exercise3.dice | |
end | |
sum | |
end | |
end | |
class Exercise5 | |
# require 'prime' | |
# def self.prime?(num) | |
# Prime.prime?(num) | |
# end | |
# def self.prime?(num) | |
# return false if num < 2 | |
# return true if num == 2 | |
# return false if num.even? | |
# i = 3 | |
# while i <= num / i | |
# return false if (num % i).zero? | |
# i += 2 | |
# end | |
# true | |
# end | |
def self.prime?(num) | |
return false if num < 2 | |
2.upto(Math.sqrt(num)) do |i| | |
if num % i == 0 | |
return false | |
end | |
end | |
true | |
end | |
1.upto(10) do |n| | |
puts n if prime?(n) | |
end | |
end | |
end | |
puts '****************************************' | |
puts "Chapter 12\n\n" | |
puts 'exercise 1' | |
puts Chapter12::Exercise1.cels_to_fahr(25) | |
puts 'exercise 2' | |
# exercise2 = Chapter12::Exercise2 | |
puts Chapter12::Exercise2.fahr_to_cels(77) | |
Chapter12::Exercise2.correspondence_table_cels_to_fahr | |
puts 'exercise 3' | |
puts Chapter12::Exercise3.dice | |
puts 'exercise 4' | |
puts Chapter12::Exercise4.dice10 | |
puts 'exercise 5' | |
puts Chapter12::Exercise5.prime?(7) | |
puts '****************************************' | |
# Chapter 13 | |
module Chapter13 | |
class Exercise1 | |
def self.create_ary_a | |
a = [] | |
1.upto(100) { |i| a << i } | |
a | |
end | |
end | |
class Exercise2 | |
def self.create_ary_a2 | |
a = Chapter13::Exercise1.create_ary_a | |
a2 = a.map { |i| i * 100 } | |
a2 | |
end | |
def self.replace_a_element | |
a = Chapter13::Exercise1.create_ary_a | |
a.map! { |i| i * 100 } | |
end | |
end | |
class Exercise3 | |
def self.create_ary_a3 | |
a = Chapter13::Exercise1.create_ary_a | |
a3 = a.select { |i| (i % 3).zero? } | |
a3 | |
end | |
def self.delete_multiples_of_3 | |
a = Chapter13::Exercise1.create_ary_a | |
a.reject { |i| !(i % 3).zero? } | |
end | |
end | |
class Exercise4 | |
def self.reverse_a | |
a = Chapter13::Exercise1.create_ary_a | |
a.reverse | |
end | |
end | |
class Exercise5 | |
def self.sum_all_of_a | |
a = Chapter13::Exercise1.create_ary_a | |
sum_of_a = a.inject(:+) | |
sum_of_a | |
end | |
end | |
class Exercise6 | |
def self.exercise6 | |
ary = Chapter13::Exercise1.create_ary_a | |
result = [] | |
10.times do |i| | |
result << ary[i * 10..(i * 10 + 9)] | |
end | |
p result | |
end | |
end | |
class Exercise7 | |
def self.sum_array(ary1, ary2) | |
[ary1, ary2].transpose.map { |i| i.inject(:+) } | |
end | |
end | |
end | |
puts '****************************************' | |
puts "Chapter 13\n\n" | |
puts 'exercise 1' | |
p Chapter13::Exercise1.create_ary_a | |
puts 'exercise 2' | |
p Chapter13::Exercise2.create_ary_a2 | |
p Chapter13::Exercise2.replace_a_element | |
puts 'exercise 3' | |
p Chapter13::Exercise3.create_ary_a3 | |
p Chapter13::Exercise3.delete_multiples_of_3 | |
puts 'exercise 4' | |
p Chapter13::Exercise4.reverse_a | |
puts 'exercise 5' | |
p Chapter13::Exercise5.sum_all_of_a | |
puts 'exercise 6' | |
Chapter13::Exercise6.exercise6 | |
puts 'exercise 7' | |
p Chapter13::Exercise7.sum_array([1, 2, 3], [4, 6, 8]) | |
puts '****************************************' |
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
# Chapter 14 | |
module Chapter14 | |
class Exercise1 | |
def self.string_to_ary | |
'Ruby is an object oriented programming language'.split(' ') | |
end | |
end | |
class Exercise2 | |
def self.sort_by_alphabet | |
ary = Exercise1.string_to_ary | |
ary.sort_by { |a| a[0] } | |
end | |
end | |
class Exercise3 | |
def self.sort_by_alphabet | |
ary = Exercise1.string_to_ary | |
ary.sort_by { |a| a.downcase[0] } | |
end | |
end | |
class Exercise4 | |
def self.count_char | |
char_ary = 'Ruby is an object oriented programming language'.split('') | |
char_hash = char_ary.sort.each_with_object(Hash.new(0)) { |a, hash| hash[a] += 1; } | |
char_hash.each { |k, v| print "'#{k}': "; v.times { print '*' }; puts } | |
end | |
end | |
class Exercise5 | |
KANNUM_1_9 = { '' => 1, '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5, '六' => 6, '七' => 7, '八' => 8, '九' => 9 }.freeze | |
KANNUM_10 = { '千' => 1000, '百' => 100, '十' => 10, '' => 1 }.freeze | |
def self.kan2num(kan) | |
kan.scan(/([^千百十]*)([千百十]?)/).inject(-1) do |ret, (first_digit, unit)| | |
ret + KANNUM_1_9[first_digit] * KANNUM_10[unit] | |
end | |
end | |
end | |
end | |
puts '****************************************' | |
puts "Chapter 14\n\n" | |
puts 'Exercise 1' | |
p Chapter14::Exercise1.string_to_ary | |
puts 'Exercise 2' | |
p Chapter14::Exercise2.sort_by_alphabet | |
puts 'Exercise 3' | |
p Chapter14::Exercise3.sort_by_alphabet | |
puts 'Exercise 4' | |
p Chapter14::Exercise4.count_char | |
puts 'Exercise 5' | |
p Chapter14::Exercise5.kan2num('七千百二十三') | |
puts '****************************************' | |
# Chapter 15 | |
module Chapter15 | |
class Exercise1 | |
def self.wday | |
{ sunday: '日曜日', monday: '月曜日', tuesday: '火曜日', wednesday: '水曜日', thursday: '木曜日', friday: '金曜日', saturday: '土曜日' } | |
end | |
end | |
class Exercise2 | |
def self.hash_size | |
Chapter15::Exercise1.wday.size | |
end | |
end | |
class Exercise3 | |
def self.exercise3 | |
Chapter15::Exercise1.wday.each { |k, v| puts "「#{k}」は#{v}のことです。" } | |
end | |
end | |
class Exercise4 | |
def self.str2hash(str) | |
hash = {} | |
ary = str.split(/\s+/) | |
hash.store(ary.shift, ary.shift) until ary.empty? | |
hash | |
end | |
end | |
end | |
puts '****************************************' | |
puts "Chapter 15\n\n" | |
puts 'Exercise 1' | |
p Chapter15::Exercise1.wday | |
puts 'Exercise 2' | |
p Chapter15::Exercise2.hash_size | |
puts 'Exercise 3' | |
Chapter15::Exercise3.exercise3 | |
puts 'Exercise 4' | |
p Chapter15::Exercise4.str2hash("blue 青 white 白\nred 赤") | |
puts '****************************************' |
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
# Chapter 16 | |
module Chapter16 | |
class Exercise1 | |
def self.parse_mail(mail) | |
mail.match(/@/) | |
{ '$1' => $`, '$2' => $' } | |
end | |
end | |
class Exercise2 | |
def self.defficult_to_easy | |
str = '正規表現は難しい! なんて難しいんだ!' | |
str.gsub('難しいんだ', '簡単なんだ').gsub('難しい', '簡単だ') | |
end | |
end | |
class Exercise3 | |
def self.word_capitalize(word) | |
word.split(/-/).collect{|w| w.capitalize}.join('-') | |
end | |
end | |
end | |
puts '****************************************' | |
puts "Chapter 16\n\n" | |
puts 'Exercise 1' | |
p Chapter16::Exercise1.parse_mail('ローカルパート@ドメイン名') | |
puts 'Exercise 2' | |
p Chapter16::Exercise2.defficult_to_easy | |
puts 'Exercise 3' | |
p Chapter16::Exercise3.word_capitalize('in-reply-to') | |
p Chapter16::Exercise3.word_capitalize('X-MAILER') | |
puts '****************************************' | |
# Chapter 17 | |
module Chapter17 | |
class Exercise1 | |
def self.count_linenum(file) | |
File.read(file).count("\n") | |
end | |
def self.count_word(file) | |
File.read(file).split(/[\s+[[:blank:]]]/).reject { |w| w.empty? }.size | |
end | |
def self.count_char(file) | |
File.read(file).split('').reject { |w| w.match(/[\s+[[:blank:]]]/) }.size | |
end | |
end | |
end | |
puts '****************************************' | |
puts "Chapter 17\n\n" | |
puts 'Exercise 1' | |
p Chapter17::Exercise1.count_linenum('./Chapter_17_1.txt') | |
p Chapter17::Exercise1.count_word('./Chapter_17_1.txt') | |
p Chapter17::Exercise1.count_char('./Chapter_17_1.txt') | |
puts '****************************************' |
↑の点を直してもらったら LGTM です 👍
お疲れ様でした!
以下はおまけなのでチャレンジしてもしなくても良いです!
答えが気になったら聞いてもらえれば教えます 🙏
Chapter13 - Exercise1
Enumerator#to_a
で書き換えてみるEnumerator#times
,Enumerator#to_a
,Enumerator#map
を使って書き換えてみる
Chapter13 - Exercise2
Enumerator#map
に「ある引数 x を渡して x を 100 倍した数を返す Proc オブジェクト(lambda)」を渡す方法で処理を書き換えてみる
Chapter13 - Exercise3
Enumerator#select
に「ある引数 x を渡して x が 3 の倍数なら true, そうでなければ false を返す Proc オブジェクト(lambda)」を渡す方法で処理を書き換えてみる
Chapter13 - Exercise5
Enumerator#reduce
を使って書き換えてみる- ブロックを渡す方法
- Proc オブジェクトを渡す方法
Chapter13 - Exercise6
Enumerator#map
を使って書き換えてみる- ブロックを渡す方法
- Proc オブジェクトを渡す方法
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
再レビュー
Chapter12 - Exercise5
以下のコードですが、
https://gist.github.com/yosuke0315/3b4a25d965feff7e449be4a33c23e9c7#file-exercises_12_13-rb-L57-L69
次の点が気になります。
return
していないので引数に0, 1
以上の数を与えた場合、常に結果がtrue
になる