Exercise: Sum of numbers in a file
Let's implement something slightly useful.
In this exercise we have a file called numbers.txt that has numbers in it. Each line is a number.
Our aim is to add the numbers and print the result.
examples/data/numbers.txt
3 7 23 0 -17 98 12
Expected result is 126.
Tools
Some of the tools you might need to solve this exercise.
- Perl:
- Ruby:
- Open file and read content line-by-line in Ruby
- Convert string to number in Ruby
- For the advanced version:
- map in Ruby
- reduce in Ruby
- Python:
- Open file in Python
Solutions
- Python: sum of numbers
- Perl 5: sum of numbers in a file
- Ruby: sum of numbers in a file
- Groovy: sum of numbers
Published on 2015-10-19