Skip to content
yowasou edited this page Aug 13, 2023 · 2 revisions

Hamamatsu.rb 118

雑談

囚人のジレンマ

型Rubyを試すサイト

ビンパッキング問題の解き方

C#でビンパッキング問題を解く(1)

おねぇさぁぁぁぁぁん! 日本科学未来館のアニメに狂気が宿っていると話題に - ねとらぼ

エイト・クイーン - Wikipedia

バックトラッキング - Wikipedia

ヒト夜の永い夢 (ハヤカワ文庫JA)

Ruby試すサイト

言語処理100本ノック

02. 「パトカー」+「タクシー」=「パタトクカシーー」

  • transposeが鍵
puts %w[パトカー タクシー].map(&:chars).transpose.tap{|a| p a}.inject('') { |buf, (a, b)| buf + a + b }
puts %w[パトカー タクシー].map(&:chars).transpose.join
# https://nlp100.github.io/ja/ch01.html

ar1 = 'パトカー'.chars
ar2 = 'タクシー'.chars

ret = []
ar1.each_with_index do |v, i|
	ret << v
	ret << ar2[i]
end

p ret.join
a = "パトカー"
b = "タクシー"
s = b.split("")
i = 0
j = ""
a.split("").each { |c|
  j = j + c + s[i]
  i = i + 1
}
p j
a = "パトカー"
b = "タクシー"
p a.chars.zip(b.chars).flatten.join
  • joinの懐が深い
[1,2,3,[4,5]].join
=> "12345"

円周率

  • みんなmap使ってるのが上達した感がある

  • 正規表現がつかえるとよりよい

  • カンマ(,)とドット(.)があるから注意

a = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
p a.split.map(&:length)
ar = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.".gsub(',', '').split(' ').map do |v|
	v.size
end
p ar
p <<~EOS.split(/[\s,.]+/).map(&:length)
Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
EOS
a = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
b = a.gsub(",","").gsub(".","").split(" ")
b = b.map { |c| c.length }
p b
p 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'.split.map(&:size)
a = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
p a.delete(",.").split.map(&:length)
p <<~EOS.scan(/\w+/).map(&:length)
Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
EOS
p <<~EOS.scan(/[[:alpha:]]+/).map(&:length)
Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
EOS
a = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
p a.delete("^A-z ").split.map(&:length)
a="Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
p a.tr(".","").split(" ").map{|x|x.size}

元素記号

hash = { "H" => 1, "He" => 2, "Li" => 3, ...}
  • 二次元配列をto_hすると配列になります。zipしてからでも。
single_chars = [1, 5, 6, 7, 8, 9, 15, 16, 19]
s = <<~EOS
Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.
EOS
p s.scan(/\w+/).map.with_index(1).each_with_object({}) { |(w, n), h| h[w[0...(single_chars.include?(n) ? 1 : 2)]] = n }
a = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
b = [1, 5, 6, 7, 8, 9, 15, 16, 19]
p a.delete(".").split.map.with_index {|w, i| b.include?(i + 1) ? [w.slice(0, 1), i + 1] : [w.slice(0, 2), i + 1] }.to_h
str = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."

ret = str.gsub(/\./, '')
				 .split(' ')
				 .each_with_object({})
				 .with_index(1) do |(data, hash), i|
	key = [1, 5, 6, 7, 8, 9, 15, 16, 19].include?(i) ? data.chars.first : data.chars[0..1].join
	hash[key] = i
end

p ret
puts 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'
  .split(/[^[:alpha:]]/)
  .reject(&:empty?)
  .map
  .with_index(1) { [[1, 5, 6, 7, 8, 9, 15, 16, 19].include?(_2) ? _1[0] : _1[0..1], _2] }
  .to_h
s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
pos = [1, 5, 6, 7, 8, 9, 15, 16, 19]
hash = {}
i = 1
s.delete(".").split.each { |m|
  a = m[0] + m[1]
  if (!pos.index(i).nil?) then
    a = m[0]
  end
  hash[a] = i
  i = i + 1
}
p hash

次回のネタ

Rubyでリファクタリングをやってみよう

Ruby 3.0の"型"の話

新年会:Ruby3.0リリースイベント&雑談

  • 次回、1/13(水)です

宣伝

Webで使えるmrubyシステムプログラミング入門

【第7回】浜松IT合同勉強会2020 LT大会

正規表現

KPT

KEEP

  • 色々な書き方が見れて良かったです。 ★3

  • Ruby技術が(少し)上がっている気がする ★2

  • Ruby書くのはやっぱり楽しい ★2

  • 言語処理系は使いどころ多そうで楽しかった ★2

PROBLEM

  • 遅刻してしまいました。 ★1

  • 言語処理が苦手だと分かりました。 ★1

  • 他の人がコードをPOSTすると焦るw ★2

TRY

  • Ruby 3.0 ★2

  • 言語処理について勉強する。 ★1

  • mruby ★1

  • 正規表現について勉強する。 ★2

  • 次こそ議事録リアルタイム共有 ★3

  • 次回は 2021/1/13(水) 19:00~ 新年会兼ねて飲食しつつ雑談その他 ★4

  • 参加する ★1

Clone this wiki locally