Skip to content

Instantly share code, notes, and snippets.

class Collatz
def init(i)
buf = [i]
while i != 1
if (i % 2) == 0
i = i / 2
else
i = i * 3 + 1
end
buf << i
[1, 2, 3, 4, 5].each do |i|
p i * 2
end
#2
#4
#6
#8
#10
def collatz(arg):
if type(arg) == int:
arg = [arg]
elif arg[-1] == 1:
# print arg => [10, 5, 16, 8, 4, 2, 1]
return arg
if arg[-1] % 2 == 0:
arg.append(arg[-1] / 2)
collatz(arg)
def collatz(arg)
if arg.kind_of?(Integer)
arg = [arg]
elsif arg[-1] == 1
return [arg.size - 1, "[#{arg.join(', ')}]"]
end
if (arg[-1] % 2) == 0
collatz(arg << arg[-1] / 2)
else
# [問題 6.2]
# * 文字列を,a ならば z,b ならば y,c ならば x のように,小文字も大文字もアルファベットの逆順の出現文字に置き換えるプログラムを作りなさい.
# http://www.aoni.waseda.jp/ichiji/2009/second-term/ruby-06-1.html
def reverse(s)
alphabet = ('a'..'z').to_a
if alphabet.index(s).nil?
alphabet[alphabet.size - alphabet.index(s.downcase)].upcase
else
alphabet[alphabet.size - alphabet.index(s)]
# [問題 6.3]
# * 文字列が与えられたとき,アルファベットを大文字,小文字を区別せずに出現頻度順に小文字で並べた文字列を返すプログラムを作りなさい.
# http://www.aoni.waseda.jp/ichiji/2009/second-term/ruby-06-1.html
def freq(str)
h = {}
str.downcase.each_byte do | s |
if h.has_key?(s.chr)
h[s.chr] = h[s.chr] + 1
else
# [問題 6.1]
# シーザー暗号の暗号化,複合化を行うプログラムを作りなさい.
# http://www.aoni.waseda.jp/ichiji/2009/second-term/ruby-06-1.html
# irb(main):122:0> string2 = caesar(string,3)
# => "Lq fubswrjudskb, d Fdhvdu flskhu, dovr nqrzq dv d Fdhvdu'v flskhu, wkh vkliw flskhu, Fdhvdu'v frgh ru Fdhvdu vkliw,
# lv rqh ri wkh vlpsohvw dqg prvw zlghob nqrzq hqfubswlrq whfkqltxhv."
# irb(main):123:0> caesar(string2,-3)
# => "In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift cipher, Caesar's code or Caesar shift,
# is one of the simplest and most widely known encryption techniques."
#!/bin/sh
trash=$HOME/trash
if [ ! -d $trash ]
then
mkdir -p $trash
fi
for i
do
buf = [0, 1, 2, 3, 4, 5, 6, 7]
buf.each do | n |
p n / 2
end
#0
#0
#1
#1
The grammar translation method is a traditional way to study foreign languages.
This method was commonly used in Europe in the 19th century to teach students how to
read the literature of the Greeks and Romans.
In this method, Students memorize grammatical rules, and words of the languages that they study.
Then students try to analyze the texts that they read and translate into their mother tongue.
Until recently it was the most common method to teach foreign languages in school in many countries.
And it still is in some countries. But since it puts little importance on sound and speaking practice,
the grammar translation method hardly improves the abilities to speak foreign languages,
and understand them by listening.