我有一个任务是找到每个句子中的单词.
给定一个字符串,我们想将字符串分成句子,然后确定哪些单词(如果有的话)在所有句子中.
这是我的解决方案:
# encoding: utf-8
text = ''
File.foreach("lab2.in") do |line|
text += line
end
hash = Hash.new
text = text.gsub(/[\n,]/,'').split(/[!.?]/)
number = 0
text.each do |sen|
number += 1
words = sen.split(/ /)
words.each do |word|
if hash[word]
hash[word] += "#{number}"
else
hash[word] = "#{number}"
end
end
end
flag = false
needle = ''
count = text.length
for i in 1..count
needle += "#{i}"
end
hash.each do |word|
if word[1].squeeze == needle
puts "this word is \"#{word[0]}\""
flag …Run Code Online (Sandbox Code Playgroud) 这里我有一个数组未定义的元素.我试图打印这个数组的随机元素并剪切它.在这里我的代码.
function rand(min, max){
return (Math.floor(Math.random() * (max - min + 1)) + min).toFixed(0);
}
$('#do').click(function(){
var count = chamarr.length;
var num = 0;
if (count == 1) {
$('#output').html('Nothing can found');
} else {
num = rand(1,chamarr.length);
$('#output').html(chamarr[num]);
chamarr.splice(num,1);
}
});
Run Code Online (Sandbox Code Playgroud)
当我记录一个阵列被切割时,我看到它总是好的,但有时候元素没有切割!