LoginSignup
1
0

More than 3 years have passed since last update.

みんなでしりとり Ruby編

Posted at

エンジニア目指して一生懸命勉強中です。
ランクBは難しい!調べながらじゃないとクリアできなくて悔しい!
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
みんなでしりとり(Bランク練習問題 Ruby)
https://paiza.jp/works/mondai/skillcheck_archive/word_chain?language_uid=ruby&t=5849f929b8390356d4da2de5cc89575f
参考にした動画(Python3)
https://www.youtube.com/watch?v=ihItHDx9sX0

※練習問題はブログやSNS等に掲載することはOKとのことです。
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー

line = gets.split.map(&:to_i)
n = line[0] #人数
k = line[1] #単語数
m = line[2] #発言数
word_list = [] #使用可能な単語
siri = [] #入力された単語全部
used_siri = [] #使用した単語

単語の格納

k.times do
word_list.push(gets.chomp.to_s)
end

m.times do
siri.push(gets.chomp.to_s)
end

生きてる人数

alive = Array.new(n,true)

4つのルール

def ruleO(w,s)
return w.include?(s)
end

def ruleT(b,a,ruleT_pass)
return b[-1,1] == a[0,1] ||
ruleT_pass == true
end

def ruleTh(a,b)
return a.include?(b) != true
end

def ruleF(a)
return a[-1,1] != "z"
end

次に発言する人の番号を求める

def next_number(alive,now_number)
now_number += 1
until alive[now_number] == true
if now_number >= alive.size
now_number = 0
else
now_number += 1
end
end
return now_number
end

初期値

now_number = 0

一番最初の人はルール2免除

ruleT_pass = true

しりとり開始

(1..m).each do |num|
if ruleO(word_list,siri[num - 1]) &&
ruleT(siri[num - 2],siri[num - 1],ruleT_pass) &&
ruleTh(used_siri,siri[num - 1]) && ruleF(siri[num - 1])
used_siri.push(siri[num - 1])
ruleT_pass = false
else
alive[now_number] = false
ruleT_pass = true
end
now_number = next_number(alive,now_number)
if word_list.sort == used_siri.sort
used_siri = []
end
end

output

p alive.count(true)

alive.size.times do
if alive.find_index(true)
f = alive.find_index(true)
puts (f.to_i + 1)
alive[f] = false
end
end

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0