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

那么我们应该得到:
["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

执行结果:
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://bbs.chinaunix.net/) Powered by Discuz! X3.2