Pedometer (Ruby edition)
Solve the pedometer with Ruby! (for statement, scope, self-assignment operator)
problem
Wanting to know how much you walked per day, you decided to calculate your average daily steps. Since the number of days recorded and the data of the number of steps are given, create a program to calculate the average number of steps per day.
The average is the sum of the given values divided by the number of data.
Value to be entered
The input is given in the following format.
N
a_1
...
a_N
-The first line is given an integer N that represents the number of days recorded.
• The 1 + i (1 ≤ i ≤ N) line is given the integer a_i, which represents the number of steps taken on day i.
-The total input is N + 1 line, and one line feed is inserted at the end.
Expected output
Output the average number of steps per day as an integer. However, the numbers after the decimal point shall be truncated.
Input example 1
6
12
7
51
15
50
24
Output example 1
27
Input example 2
3
1
1
2
Output example 2
1
My answer
python
a = gets.chomp.to_i
s = 0
for i in 1..a do
int = gets.chomp.to_i
s += int
end
print s / a
Points to pay attention to this time
Pay attention to "6" in the input example (1) and "3" in the input example (2), which are the first characters of the input value on the first line. It can be seen that the input values continue below this number. In other words, you can imagine that you have to iterate </ font> and get.
s = 0 </ font> on the 2nd line is defined so that the result can be output even outside the iterative processing (out of scope) on the 3rd to 6th lines. Important here.
On the 3rd to 6th lines, use for statement </ font> to iterate the input value and add int (repeated input value) to the variable s with the self-assignment operator. !! It is a process called.
And at the time of the 7th line, the total of input values is assigned to the variable s, so if you divide the </ font> variable s by the variable a, you can find the average value from this total value. I can.
As an aside, the method for truncating the decimal point is floor </ font>, so the print variable .floor may be fine!
that's all!