開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- Ruby (プログラミング言語)
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 13章(新しいクラスの作成と既存クラスの変更), 13.1(練習問題)、組み込みクラスの拡張、を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
バースデーヘルパー!
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby2.0
#-*- coding: utf-8 -*-
class Array
def shuffle
recursive_shuffle self, []
end
private
def recursive_shuffle unshuffled_array, shuffled_array
return shuffled_array if unshuffled_array.length == 0
l = unshuffled_array.length
r = rand l
shuffled_array.push unshuffled_array[r]
tmp = unshuffled_array.slice(0, r) +
unshuffled_array.slice(r + 1, l)
recursive_shuffle tmp, shuffled_array
end
end
class Integer
def factorial
return '負の数' if self < 0
return 1 if self <= 1
self * (self - 1).factorial
end
def to_roman
nums = self
result = ""
num_roman = {1000 => 'M',
100 => {5 => 'D',
1 => 'C'},
10 => {5 => 'L',
1 => 'X'},
1 => {5 => 'V',
1 => 'I'}}
keys = num_roman.keys.sort.reverse
k = keys.shift
n = nums / k
nums = nums % k
c = num_roman[k]
pre_c = c
result += c * n
keys.each do |k|
roman = num_roman[k]
roman_five = roman[5]
roman_one = roman[1]
n = nums / k
nums = nums % k
if n == 9
result += roman_one + pre_c
elsif n == 4
result += roman_one + roman_five
else
result += roman_five * (n / 5)
result += roman_one * (n % 5)
end
pre_c = roman_one
end
result
end
end
puts [1,2,3,4,5,6,7,8,9,10].shuffle.join ' '
puts "10 ! = #{10.factorial}"
n = 1234
puts "#{n}: #{n.to_roman}"
入出力結果(Terminal)
$ ./sample.rb 8 6 4 10 9 2 7 1 3 5 10 ! = 3628800 1234: MCCXXXIV $
ちなみにpython3.4の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import random
class MyList(list):
def shuffle(self):
return self.recursiveShuffle([])
def recursiveShuffle(self, shuffled_list):
unshuffled_list = self
if len(unshuffled_list) == 0:
return shuffled_list
r = random.randint(0, len(unshuffled_list) - 1)
shuffled_list.append(unshuffled_list[r])
tmp = unshuffled_list[0:r] + unshuffled_list[r + 1:]
return MyList(tmp).recursiveShuffle(shuffled_list)
class MyInt(int):
def factorial(self):
if self < 0:
return '負の数'
if self <= 1:
return 1
return self * MyInt(self - 1).factorial()
def romanNumeral(self):
num = self
result = ''
num_roman = {
1000: 'M',
100: {5: 'D', 1: 'C'},
10: {5: 'L', 1: 'X'},
1: {5: 'V', 1: 'I'}
}
keys = sorted(num_roman.keys(), reverse=True)
k = keys.pop(0)
n = num // k
num = num % k
c = num_roman[k]
pre_c = c
result += c * n
for k in keys:
roman = num_roman[k]
roman_five = roman[5]
roman_one = roman[1]
n = num // k
num = num % k
if n == 9:
result += roman_one + pre_c
elif n == 4:
result += roman_one + roman_five
else:
result += roman_five * (n // 5)
result += roman_one * (n % 5)
pre_c = roman_one
return result
n = MyInt(1294)
print(' '.join(map(lambda x: str(x),
MyList([1,2,3,4,5,6,7,8,9,10]).shuffle())))
print('10! = {0}'.format(MyInt(10).factorial()))
print('{0}: {1}'.format(n, n.romanNumeral()))
入出力結果(Terminal)
$ ./sample.py 8 1 2 10 7 3 5 6 4 9 10! = 3628800 1294: MCCXCIV $
0 コメント:
コメントを投稿