有没有办法优化我为String类写的这个字符计数器的代码?

huangapple go评论101阅读模式
英文:

Any way to optimize this character counter i wrote in ruby for String class

问题

  1. # 字符计数器
  2. class String
  3. def count_lcases
  4. count(('a'..'z').to_a.join(''))
  5. end
  6. def count_upcases
  7. count(('A'..'Z').to_a.join(''))
  8. end
  9. def count_num
  10. count((0..9).to_a.join(''))
  11. end
  12. def count_spl_chars
  13. length - count_lcases - count_upcases - count_num
  14. end
  15. end
  16. input = ARGV[0]
  17. if ARGV.empty?
  18. puts '请提供输入'
  19. exit
  20. end
  21. puts '小写字符 = %d' % [input.count_lcases]
  22. puts '大写字符 = %d' % [input.count_upcases]
  23. puts '数字字符 = %d' % [input.count_num]
  24. puts '特殊字符 = %d' % [input.count_spl_chars]

对于你提出的优化问题,可以考虑使用正则表达式来一次性匹配并计数不同类型的字符。不过,这个示例中的方法也是有效的。

英文:
  1. # Character Counter
  2. class String
  3. def count_lcases
  4. count(('a'..'z').to_a.join(''))
  5. end
  6. def count_upcases
  7. count(('A'..'Z').to_a.join(''))
  8. end
  9. def count_num
  10. count((0..9).to_a.join(''))
  11. end
  12. def count_spl_chars
  13. length - count_lcases - count_upcases - count_num
  14. end
  15. end
  16. input = ARGV[0]
  17. if ARGV.empty?
  18. puts 'Please provide an input'
  19. exit
  20. end
  21. puts 'Lowercase characters = %d' % [input.count_lcases]
  22. puts 'Uppercase characters = %d' % [input.count_upcases]
  23. puts 'Numeric characters = %d' % [input.count_num]
  24. puts 'Special characters = %d' % [input.count_spl_chars]

I used ranges to count characters but count function is called 3 times.

I can always use loops and count it one by one.I was wondering is there any way to optimize this?...

答案1

得分: 3

如果您使用Ruby 2.7,您可以使用tally;字符串的字符只需迭代一次。

  1. def classify_char(c)
  2. case c
  3. when /[a-z]/ then :lcase
  4. when /[A-Z]/ then :ucase
  5. when /\d/ then :digit
  6. else :other
  7. end
  8. end
  9. p "asg3456 ERTYaeth".chars.map{|c| classify_char(c) }.tally
  10. # => {:lcase=>7, :digit=>4, :other=>2, :ucase=>4}
英文:

If you are using Ruby 2.7 you could use tally; the string's chars are just iterated one time.

  1. def classify_char(c)
  2. case c
  3. when /[a-z]/ then :lcase
  4. when /[A-Z]/ then :ucase
  5. when /\d/ then :digit
  6. else :other
  7. end
  8. end
  9. p "asg3456 ERTYaeth".chars.map{|c| classify_char(c) }.tally
  10. # => {:lcase=>7, :digit=>4, :other=>2, :ucase=>4}

答案2

得分: 0

如果 Ruby 版本在 2.3 到 2.7 之间,这段代码将有效:

  1. CHAR_CLASSES = {
  2. lcase: ?a..?z,
  3. ucase: ?A..?Z,
  4. digit: ?0..?9,
  5. }
  6. p "asg3456 ERTYaeth".each_char.with_object(Hash.new(0)) { |c, o|
  7. o[CHAR_CLASSES.find { |label, group| group === c }&.first || :other] += 1
  8. }

对于小于 2.3 的情况,

  1. p "asg3456 ERTYaeth".each_char.with_object(Hash.new(0)) { |c, o|
  2. p = CHAR_CLASSES.find { |label, group| group === c }
  3. o[p ? p.first : :other] += 1
  4. }
英文:

If Ruby 2.3...2.7, this will work:

  1. CHAR_CLASSES = {
  2. lcase: ?a..?z,
  3. ucase: ?A..?Z,
  4. digit: ?0..?9,
  5. }
  6. p "asg3456 ERTYaeth".each_char.with_object(Hash.new(0)) { |c, o|
  7. o[CHAR_CLASSES.find { |label, group| group === c }&.first || :other] += 1
  8. }

For < 2.3,

  1. p &quot;asg3456 ERTYaeth&quot;.each_char.with_object(Hash.new(0)) { |c, o|
  2. p = CHAR_CLASSES.find { |label, group| group === c }
  3. o

    += 1

  4. }

huangapple
  • 本文由 发表于 2020年1月6日 20:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定