判断随机产生单词的另一种方法
方法 判断 一种 随机 产生 单词
2023-09-14 08:56:51 时间
在上一篇中我介绍了判断随机产生单词的3种方法,大致都是用了外在程序spell。现在本猫又在Mac OS X系统上找到了如下文件:/usr/share/dict/words ,其中放置了N多个英语单词啊:
apple@kissAir: dict$ls -ldh words lrwxr-xr-x 1 root wheel 4B 10 18 14:00 words - web2 apple@kissAir: dict$ls -lh web2 -r--r--r-- 1 root wheel 2.4M 9 10 04:47 web2 apple@kissAir: dict$wc -n words wc: illegal option -- n usage: wc [-clmw] [file ...] apple@kissAir: dict$wc -l words 235886 words一行一个单词,即一共23万多个单词,我们可以抛掉spell程序,自己写一个is_spell?方法来判断单词是否可拼写啦,以下是增加way4后的代码,放弃了命令行参数的方式,而是用benchmark包来测试性能:
#!/usr/bin/ruby #code by hopy 2014.12.08 #random create some words and check if a valid word! require tempfile require benchmark words_path = "/usr/share/dict/words" f = File.open(words_path,"r") $lines = f.readlines $lines.map! {|word|word.chomp!} f.close def rand_words(n=10000,min_len=2,max_len=12) chars = (("a".."z").to_a * max_len).freeze words = [] srand n.times do |x| len = min_len + (rand*1000).to_i % max_len idxes = [] len.times {idxes (rand*100)%26} chars.shuffle words chars.values_at(*idxes).join idxes.clear end words #ret word that can spell or ret nil. (way1) def spell_word(word) cmd = `echo #{word}|spell`.chomp if cmd == word return nil else return word #spell all words by tmpfile. (way2) def spell_words(words) puts "using spell_words..." f = Tempfile.new("#{$$}_spell_blablabla") #f = File.open("spell_test","w+") #f.write Marshal.dump(words) f.write words.join(" ") f.close cmd = `spell #{f.path}` no_spell_words = cmd.split("\n") words - no_spell_words #spell all words by tmpfile and spell ret is also use tmpfile. (way3) def spell_words2(words) puts "using spell_words2..." f_words = Tempfile.new("#{$$}_spell_words") f_ret = Tempfile.new("#{$$}_spell_ret") f_ret.close f_words.write words.join(" ") f_words.close cmd = `spell #{f_words.path} #{f_ret.path}` f=File.open(f_ret.path) no_spell_words = f.read.split("\n") f.close words - no_spell_words def is_spell?(word) $lines.include? word #利用is_spell?判断word是否可拼写的方法。(way4) def spell_words3(words) =begin words.each do |word| printf "#{word} " if is_spell?(word) words.select {|word|is_spell?(word)} def sh_each_spell_word(spell_words) spell_words.each {|word|printf "#{word} "} words_count = 2000 $words = nil puts "words_count is 2000,now test..." Benchmark.bm do |bc| bc.report("rand_words:\n") {$words = rand_words(words_count)};puts "" bc.report("way1:spell_word:\n") {$words.each {|w|printf "#{w} " if spell_word(w)}};puts "" bc.report("way2:spell_words:\n") {sh_each_spell_word(spell_words($words))};puts "" bc.report("way3:spell_words2:\n") {sh_each_spell_word(spell_words2($words))};puts "" bc.report("way4:spell_words3:\n") {sh_each_spell_word(spell_words3($words))};puts "" end
不过Mac OS X自身不带spell程序,用brew不知要安装哪一个;而虚拟机中的ubuntu的spell死活无法升级。等明天用本猫的x61来测试吧!
现在已经是明天鸟!发现ubuntu中自带的words文件包含单词比Mac下的要少,只有9万多个单词啊,遂将其用Mac下的文件替换,可以看到他比spell程序实际枚举的单词要多哦:
wisy@wisy-ThinkPad-X61:~/src/ruby_src$ ./x.rb words_count is 2000,now test... user system total real rand_words: 0.050000 0.000000 0.050000 ( 0.069850) way1:spell_word: ho of ts mu so or wag us to lo um ts pa pip mid hip vs no of oboe iv yr re so 0.330000 3.170000 13.480000 ( 29.903239) way2:spell_words: using spell_words... ho of ts mu so or wag us to lo um ts pa pip mid hip vs no of oboe iv yr re so 0.000000 0.000000 0.080000 ( 5.485613) way3:spell_words2: using spell_words2... ho of ts mu so or wag us to lo um ts pa pip mid hip vs no of oboe iv yr re so 0.010000 0.010000 0.100000 ( 4.854248) way4:spell_words3: ho of pob dob mu bo so sa or wag us jo aw to lo um li ca se pa ava bo sho pip mid til tue ya en hip no of di ug oboe io en yr re da eer so ym 36.580000 0.290000 36.870000 ( 37.444370)
我们写的新的方法(way4)竟然是最慢的!!!不试不知道,一试吓一跳啊!
字符串个数匹配问题 # 7-2 子字符串个数匹配 分别输入两个字符串A和B,A由多个小字符串组成(中间由非字母隔开),B是由字母组成的字符串。求出A中包含B的小字符串的个数(详细看样例),并且输出它。(不区分大小写) ### 输入格式: 先输入字符串A,由回车结束。然后输入字符串B。 ### 输出格式: 输出A中包含B字符串的个数、 ### 输入样例: 在这里给出一组输入。例如: ```in aaBbc4./ewfeAbc wefW%!%&aAbc++0 4Abccabc ### 输出样例: 在这里给出相应的输出。例如: ```out A可以看成:a
相关文章
- 基于多源观测数据的降水估计新方法
- WEB基础:Apache POI的基本使用方法详解
- MySQL 中判断NULL值的方法(mysql判断为null)
- 【Oracle中判断星期的方法】(oracle判断星期)
- Linux远程管理的正确方法(怎么远程linux系统)
- 使用MySQL进行空值判断的方法(mysql空判断)
- Linux系统中快速确定文件权限的方法(linux判断文件权限)
- 判断Redis是否宕机一种方法(怎么判断redis宕机)
- MySQL快速比较两种不同的安装方法(mysql 两种安装)
- MySQL下载安装问题解决方法大揭密(mysql下载后无法安装)
- Redis操作出现的错误处理方法(redis 错误)
- asp下对POST提交数据限制的解决方法
- jQuery.validate常用方法及需要注意的问题
- js判断元素是否隐藏的方法
- SQLSEVER数据库重建索引的方法