最大的卡住是在有一題他會給你”“.mp3.”
我本來以為用split(‘.’)他會拆成
["", "mp3", ""]
結果是這樣["", "mp3"]
要在後面加-1才會變成我心目中以為的樣子 http://ruby-doc.org/core-2.3.1/String.html#method-i-split
fname.split('.', -1)
@n = gets.to_i # Number of elements which make up the association table.
@q = gets.to_i # Number Q of file names to be analyzed.
list = {}
@n.times do
# ext: file extension
# mt: MIME type.
ext, mt = gets.split(" ")
list[ext.downcase] = mt
end
# STDERR.puts ext2, mt2
STDERR.puts list
@q.times do |i|
fname = gets.chomp # One file name per line.
fname_ext = fname.split('.', -1)[-1].downcase
STDERR.puts "fname " + fname
if fname.split('.').size != 1
if list.has_key?(fname_ext)
puts list[fname_ext]
else
puts "UNKNOWN"
end
else
puts "UNKNOWN"
end
end
上面是我的解答,就很普通的那種XD 後來看別人寫的
@q.times do
puts @map[File.extname("x#{gets.chomp}").gsub('.', '').downcase]
end
原來有extname可以直接抓副檔名阿………… https://ruby-doc.org/core-2.2.0/File.html
Written with StackEdit.
沒有留言:
張貼留言