Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. code = File.open(WORKING_DIR + '/code.txt','r')
  2. char_count = {'a' => 0,'b' => 0,'c' => 0,'d' => 0,'e' => 0,'f' => 0,'g' => 0,'h' => 0,'i' => 0,
  3. 'j' => 0,'k' => 0,'l' => 0,'m' => 0,'n' => 0,'o' => 0,'p' => 0,'q' => 0,'r' => 0,
  4. 's' => 0,'t' => 0,'u' => 0,'v' => 0,'w' => 0,'x' => 0,'y' => 0,'z' => 0
  5. }
  6. # Step through each line in the file.
  7. code.readlines.each do |line|
  8.  
  9. # Print each character of this particular line.
  10. line.split('').each do
  11. |ch|
  12. char_count.has_key?('ch')
  13. char_count['ch'] +=1
  14. end
  15.  
  16. File.open(WORKING_DIR + '/code.txt','r') do |f|
  17. char_count = Hash.new(0) # create a hash where 0 is the default value
  18. f.each_char do |c| # iterate on each character
  19. ... # some filter on the character you want to reject.
  20. char_count[c] +=1
  21. end
  22. end
  23.  
  24. f.each_char do |c| # iterate on each character
  25. next if c ~= /W # exclude with a regexp non word character
  26. ....
  27.  
  28. char_count.has_key?('ch')
  29. char_count['ch'] +=1
  30. end
  31.  
  32. char_count = {}
  33. ('a'..'z').each{|l| char_count[l] = 0}
  34.  
  35. char_count = ('a'..'z').inject({}){|hash,l| hash[l] = 0 ; hash}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement