免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7468 | 回复: 9
打印 上一主题 下一主题

[练习] 寻找单词 [复制链接]

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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"]

论坛徽章:
31
CU大牛徽章
日期:2013-03-13 15:15:08CU大牛徽章
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:31CU大牛徽章
日期:2013-05-20 10:46:38CU大牛徽章
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43CU十二周年纪念徽章
日期:2013-10-24 15:41:34丑牛
日期:2013-12-01 10:11:07水瓶座
日期:2014-01-15 08:47:25
2 [报告]
发表于 2014-03-30 22:10 |只看该作者
这种单词分割很不好处理啊。

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
3 [报告]
发表于 2014-03-30 23:48 |只看该作者
回复 2# ddd010


    是啊!

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
4 [报告]
发表于 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()

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
5 [报告]
发表于 2014-04-01 00:05 |只看该作者
本帖最后由 rubyish 于 2014-04-01 00:15 编辑

回复 4# bikong0411

赞美   

but error!~

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
6 [报告]
发表于 2014-04-01 04:22 |只看该作者
本帖最后由 rubyish 于 2014-04-24 19:02 编辑

dddddddddddd~

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
7 [报告]
发表于 2014-04-01 08:55 |只看该作者
回复 5# rubyish


    没明白你要干啥

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
8 [报告]
发表于 2014-04-01 08:57 |只看该作者
回复 6# rubyish


    学习

论坛徽章:
0
9 [报告]
发表于 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
稍微改动了点,初学者,不知道这个符不符合要求?

论坛徽章:
5
白羊座
日期:2014-10-28 11:23:27水瓶座
日期:2015-01-20 10:19:022015亚冠之柏斯波利斯
日期:2015-07-11 18:17:2015-16赛季CBA联赛之同曦
日期:2015-12-23 12:38:582016猴年福章徽章
日期:2016-02-18 15:30:34
10 [报告]
发表于 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"]
复制代码
写的不好,没有完全达到效果
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP