我有一個方法可以計算字符串中單詞的頻率。我手動包括一些應該刪除的單詞。我發現,對於短字符串,'''被刪除...對於較長的字符串(如下面的字符串),該方法仍會打印'the'。有關這是爲什麼以及如何解決它的任何想法?如何從紅寶石哈希中刪除項目?
def count_words(string)
words = string.downcase.split(' ')
delete_list = ['the']
delete_list.each do |del|
words.delete_at(words.index(del))
end
frequency = Hash.new(0)
words.each do |word|
frequency[word.downcase] += 1
end
return frequency.sort_by {|k,v| v}.reverse
end
puts count_words('Pros great benefits fair compensation reasonable time off Cons middle management are empty suits, void of vision and very little risk taking
politics have gotten out of control since gates left the building..
sales metrics often do not reflect the contributions of the role, which demonstrates that line management is out of touch of what the individual contributors role really does
middle management does not care about the career of his/her directs, 90% of the time management competes directly with their people, or takes credit for their work
lots of back stabbing going on
Microsoft changes the organization or commitment or comp model, faster than the average deal cycle, making it next to near impossible to develop momentum in role or a rhythm of success
execs promote themselves in years when they freeze employees merit increases
only way to advance is to step on your peers/colleagues and take credit for work you had no impact on, beat your chest loud enough and you get "visibility" you need to advance
visibility is not based on performance by enlarge, it is based on being in your manager\'s swim lane for advancement
I have observed people get promoted in years when they did not meet their quota, nor did the earn the highest performance on the team, they kissed their way to the promotion
Advice to Senior Management 1, get back to risk taking and teaming, less politics please, you are killing the company
2, set realistic commitments and stick to them for multiple years, stop changing the game faster than your people can react
3, stop over engineering commitments and over segmenting the company, people are not willing to collaborate or be corporate citizens
4, too many empty suits in middle management, keep flattening out the company and getting rid of middle managers that run reports all day, get back to a culture where managers also sell and drives wins
5, keep your word microsoft, you said stability, but you keep tinkering with the org too much for any changes to take affect A great Culture
Limitless opportunities
Supportive Management team who are passionate about people
A company that really does want you to have a good work life balance and backs it up with policies that enable you to manage how and where you work.
Cons Support resources are constrained
Can be overly competitve and hard to get noticed
Sales rewards are definitely prioritised and marketing cuts are always prioritised.
Consumer organisation is still far from ideal.
Advice to Senior Management Focus on getting the internal organisation simplified to improve performance and increase empowerment.
Get some REAL consumer focus and invest for the long term
Start connecting with people, focussing on telling stories rather than selling products.')
夫婦快速點;你不需要頻率[word.downcase]'因爲單詞已經被降低了。你也應該使用'each_with_object'而不是'words.each'循環。看到我的答案。 – meagar 2013-04-10 02:00:46
發生這種情況是因爲數組中有多個「the」,所以當你說'delete_at(words.index(del))'時,它只會刪除第一個匹配項。要修復它,請執行@meagar所說的話。 – 2013-04-10 02:08:52
當你創建一個問題時,不要放置比絕對必要的更多的數據。你的問題很難閱讀,因爲太多不必要的文字會帶來所有的視覺噪音。 – 2013-04-10 02:44:22