Well, I was comparing it to something like Java. Though Python still scores far lower on my readability scale. It's seems like it's halfway to being minified, the lines are chock-a-block.
Also, this is a better version in Ruby:
file = File.open('customers.txt')
customers = {}
file.each_line do |line|
name, age = line.split(",")
customers[age.to_i] ||= []
customers[age.to_i] << name
end
grouped = customers.sort_by{|age, names| names.count}.reverse
grouped.first(3).each do |age, customers|
puts "#{age} years old: #{customers.count} customers"
end
…not sure what I was thinking before, it was late.
> Python still scores far lower on my readability scale. It's seems like it's halfway to being minified, the lines are chock-a-block.
That's likely because you're not used to reading Python. For me, Ruby seems unreadable - you have all these weird << and ||= assignments, {||} blocks, etc. I don't parse those as well, because I don't program much Ruby. I'm sure if I did, it'd make a lot more 'intuitive' sense - ditto for you and Python's list comprehensions.
<< and >> are rudimentary it's almost always exactly the same as unix dated from 1970's redirection. e.g: $ echo 'crap' >> somefile.txt. ||= reads as 'or equal', simple.
Yeah, I know what they do - eg. ||= being a Ruby idiom for 'set if this currently evaluates as false'. The point is that because I don't often program in Ruby, it's less readable (for me). Similarly, if you're not used to list comprehensions they'll seem opaque - but I use them all the time, and find them far more readable than equivalent expressions using map/filter.
That said, I do find the more "squiggly" languages (Perl, Ruby, C) to be less readable than the equivalent Scheme or Python, and I suspect that it's because there are fewer weird characters, so it's closer to English.
Also, this is a better version in Ruby:
…not sure what I was thinking before, it was late.