Calculate the difference between numbers in a Ruby array


This time, we will calculate the difference between the two numbers in the array.


You want to know how many daily temperatures change from the highest and lowest temperatures in the weather forecast to manage your physical condition.

Since the maximum temperature t and the minimum temperature u of a day are input separated by spaces, output how many temperatures are changing in a day.

For example, if you enter the following, it means that the maximum temperature is 7 degrees and the minimum temperature is -3 degrees.

7 -3

Since the temperature difference is 10, output as follows.


Before correction

input_line = gets.split(' ').map(&:to_i)

#Make an array before and after
before_after = input_line.each_cons(2)

before_after.each do |ba|
    puts (ba[1] - ba[0]).abs

#↓ Same meaning(Disassemble and fix for easy understanding)

before_after.each do |a, b|
    puts (b - a).abs


input_line = gets.split(' ').map(&:to_i)

-Get the input value with the gets method -Split the elements of the array with the split method separated by commas -The map method takes out the elements one by one and converts them to integers.

before_after = input_line.each_cons(2)

-Each_cons (2) can be obtained by shifting two consecutive elements one by one.

before_after.each do |a, b|
    puts (b - a).abs

-Calculate the difference by substituting the two elements into the ʻa, b` variables -Convert to absolute value with ads method


a, b = &:to_i
puts (a - b).abs

#If you force it to write in one line


a, b = &:to_i

・ Since this time we are limited to two elements, we divide the variables into ʻa and b`. -Get the input value with the gets method -Split the elements of the array with the split method -The map method takes out the elements one by one and converts them to integers.

puts (a - b).abs

Calculate the difference between ʻa and b` and convert it to an absolute value with the ads method.


I was able to calculate with this description, but I couldn't understand the end, so I would appreciate it if you could teach me.

Thank you for your professor this time as well. It will be a great learning experience as you will be able to discover and point out various things by sending them out. We will continue to send messages!

Recommended Posts

Calculate the difference between numbers in a Ruby array
Multiplication in a Ruby array
About the difference between classes and instances in Ruby
Sorting hashes in a Ruby array
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Ruby] Returns characters in a pyramid shape according to the entered numbers
[Android] Easily calculate the difference between two dates
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Count the number of occurrences of a string in Ruby
[Ruby] I thought about the difference between each_with_index and each.with_index
What is the difference between a class and a struct? ?? ??
Regarding the difference between the three Timeouts in Java's HttpClient
[Ruby / Rails] Set a unique (unique) value in the class
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
[Ruby] Extracting a two-dimensional array
Calculate prime numbers in Java
[Ruby] Difference between match / scan
[Ruby] Find numbers in arrays
[Ruby] Count an even number in an array using the even? Method
Determine that the value is a multiple of 〇 in Ruby
How to change a string in an array to a number in Ruby
[Android, Java] Convenient method to calculate the difference in days
How to retrieve the hash value in an array in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
How to add the same Indexes in a nested array
I was confused because there was a split in the Array
The difference between programming with Ruby classes and programming without it
[Ruby] How to batch convert strings in an array to numbers
[Ruby] Difference between get and post
Consider implementing a method that returns the difference between two Lists
[Ruby] How to count even or odd numbers in an array
Find the difference between List types
[Ruby] Difference between is_a? And instance_of?
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Ruby] I want to put an array in a variable. I want to convert to an array
Output the difference between each field of two objects in Java
Understand the difference between each_with_index and each.with_index
[Ruby] Extracting double hash in array
A memorial service for the library used when competing in Ruby
Implement a gRPC client in Ruby
Hanachan in Ruby (non-destructive array manipulation)
What is the difference between a web server and an application server?
[Java] Difference between array and ArrayList
What is a Ruby 2D array?
Easy to understand the difference between Ruby instance method and class method.
The difference between puts and print in Ruby is not just the presence or absence of line breaks
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
A note on the differences between interfaces and abstract classes in Java
I thought about the best way to create a ValueObject in Ruby
Understand the difference between int and Integer and BigInteger in java and float and double
The application absorbs the difference in character code
I tried a calendar problem in Ruby
Note: Difference between Ruby "p" and "puts"
When seeking multiple in a Java array
Difference between final and Immutable in Java
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
Difference between pop () and peek () in stack