Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- code = File.open(WORKING_DIR + '/code.txt','r')
- char_count = {'a' => 0,'b' => 0,'c' => 0,'d' => 0,'e' => 0,'f' => 0,'g' => 0,'h' => 0,'i' => 0,
- 'j' => 0,'k' => 0,'l' => 0,'m' => 0,'n' => 0,'o' => 0,'p' => 0,'q' => 0,'r' => 0,
- 's' => 0,'t' => 0,'u' => 0,'v' => 0,'w' => 0,'x' => 0,'y' => 0,'z' => 0
- }
- # Step through each line in the file.
- code.readlines.each do |line|
- # Print each character of this particular line.
- line.split('').each do
- |ch|
- char_count.has_key?('ch')
- char_count['ch'] +=1
- end
- File.open(WORKING_DIR + '/code.txt','r') do |f|
- char_count = Hash.new(0) # create a hash where 0 is the default value
- f.each_char do |c| # iterate on each character
- ... # some filter on the character you want to reject.
- char_count[c] +=1
- end
- end
- f.each_char do |c| # iterate on each character
- next if c ~= /W # exclude with a regexp non word character
- ....
- char_count.has_key?('ch')
- char_count['ch'] +=1
- end
- char_count = {}
- ('a'..'z').each{|l| char_count[l] = 0}
- char_count = ('a'..'z').inject({}){|hash,l| hash[l] = 0 ; hash}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement