亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標題: [練習] 尋找單詞 [打印本頁]

作者: rubyish    時間: 2014-03-29 23:38
標題: [練習] 尋找單詞

給定一個輸入的字符串和
一個包含各種單詞的字典,
用空格將字符串分割成一系列字典中存在的單詞。

example: 字典

WORDS = %w[
100 200 ARG Linux Note To UNIX a an and apple as
available between command commend contains delimited
dict dictionary each elect file generate input is
like line list newline numbers of on operating options
permutations random select sentence sentences sep
separate share shuf shuffle standard system the
treat usr with words
]

字符串:
sentenceselect
Toshufflethenumbersbetween100and200

那么我們應(yīng)該得到:
["sentence", "select"] ["sentences", "elect"]
["To", "shuffle", "the", "numbers", "between", "100", "and", "200"]


作者: ddd010    時間: 2014-03-30 22:10
這種單詞分割很不好處理啊。
作者: rubyish    時間: 2014-03-30 23:48
回復 2# ddd010


    是!
作者: bikong0411    時間: 2014-03-31 10:48
#!/usr/bin/env ruby

WORDS = %w[
100 200 ARG Linux Note To UNIX a an and apple as
available between command commend contains delimited
dict dictionary each elect file generate input is
like line list newline numbers of on operating options
permutations random select sentence sentences sep
separate share shuf shuffle standard system the
treat usr with words
]

start = 0
str='sentenceselect'
str='Toshufflethenumbersbetween100and200'
length = str.length
w=[]
(start..length).each do |s|
        (s+1..length).each do |x|
           tmp_word=str[s...x]
           w.push(tmp_word) if WORDS.count(tmp_word) > 0
        end
end

puts w.inspect()
作者: rubyish    時間: 2014-04-01 00:05
本帖最后由 rubyish 于 2014-04-01 00:15 編輯

回復 4# bikong0411

贊美   

but error!~
作者: rubyish    時間: 2014-04-01 04:22
本帖最后由 rubyish 于 2014-04-24 19:02 編輯

dddddddddddd~
作者: bikong0411    時間: 2014-04-01 08:55
回復 5# rubyish


    沒明白你要干啥
作者: bikong0411    時間: 2014-04-01 08:57
回復 6# rubyish


    學習
作者: gdw1986    時間: 2014-06-07 22:17
本帖最后由 gdw1986 于 2014-06-07 22:17 編輯

#!/usr/bin/env ruby
WORDS = %q[
100 200 ARG Linux Note To UNIX a an and apple as
available between command commend contains delimited
dict dictionary each elect file generate input is
like line list newline numbers of on operating options
permutations random select sentence sentences sep
separate share shuf shuffle standard system the
treat usr with words
]
Subwords=WORDS.split

spec=[]
string1="sentenceselect"
string2="Toshufflethenumbersbetween100and200"
Subwords.each do |words| if string1=~/#{words}/ or string2=~/#{words}/
       spec<< words if !spec.include?("#{words}")
end
end
puts spec

執(zhí)行結(jié)果:
100
200
To
a
an
and
between
elect
numbers
select
sentence
sentences
shuf
shuffle
the
稍微改動了點,初學者,不知道這個符不符合要求?
作者: klainogn    時間: 2014-08-09 00:21
  1. #!/usr/bin/env ruby
  2. WORDS = %w[
  3. 100 200 ARG Linux Note To UNIX a an and apple as
  4. available between command commend contains delimited
  5. dict dictionary each elect file generate input is
  6. like line list newline numbers of on operating options
  7. permutations random select sentence sentences sep
  8. separate share shuf shuffle standard system the
  9. treat usr with words
  10. ]

  11. string1="sentenceselect"
  12. string2="Toshufflethenumbersbetween100and200"
  13. class String
  14.     def ssss(words, rs=[])
  15.         words.each do |x|
  16.             if self =~ /^#{x}/
  17.                 rs =[x]+ self.sub(x,"").ssss(words,rs)
  18.             end
  19.         end
  20.         rs
  21.     end
  22. end
  23. p string1.ssss(WORDS)
  24. p string2.ssss(WORDS)
  25. ["sentences", "elect", "sentence", "select"]
  26. ["To", "shuffle", "the", "numbers", "between", "100", "and", "200", "an", "a", "shuf"]
復制代碼
寫的不好,沒有完全達到效果




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2