[Ruby] I tried to summarize the methods that frequently appear in paiza ②

Part 1

-[Ruby] I tried to summarize the methods that frequently appear in paiza

References

-How to receive value from Ruby standard input

◆ each method + array

//Creating an array for the sample
sample = [1,2,3,4]

//Create an array to store the output results
result = []

//Object: sample array
sample.each do |i|

//Variable t: Stored in variable t from the sample array
if i == 1

//Store in the array created above
 result.push(i)
end

◆ 4 ways to receive input values

//When the input value is one line
input = gets.split.map(&:to_i)

//Store input values in an array at once
input = readlines.map &:to_i

//Variable declaration+Array ①
a,b,c = gets.split(" ").map &:to_i

//Variable declaration+Array ② No line breaks
a,b,c = gets.chomp.split(" ").map &:to_i

//Store input values in an array without line breaks
input = readlines(chomp: true).map(&:to_s)

◆ include? / blank method

-** include? Method **: Determine if a particular value exists in the array'' -** blank? Method **: Determine if the contents of the array exist''

sample = ["a","b","c"]

//include?Method
if.sample.include?("a")
  puts "true"
end

//blank?Method
if.sample.blank?
  puts "true"
end

//include?Method+variable
sample_num = 3

if.sample.include?("#{sample_num}")
  puts "true"
end

◆ each_cons method

//sample array creation
sample = [1,2,3,4,5]

//each_Split with cons method
result = sample.each_cons(2).to_a

//Calculated with each method
result.each do |t|
   puts result[1] - result[0]
end

Recommended Posts

[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I tried to summarize the methods used
I tried to summarize the words that I often see in docker-compose.yml
I tried to summarize the basic grammar of Ruby briefly
Methods that I found useful in Ruby
I tried to summarize the Stream API
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to summarize the methods of Java String and StringBuilder
I tried to organize the session in Rails
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to make full use of the CPU core in Ruby
I want to get the value in Ruby
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
I tried to summarize again the devise that was difficult at first sight
I tried to organize the cases used in programming
I tried to summarize the state transition of docker
I tried to implement the Euclidean algorithm in Java
I tried to summarize the basics of kotlin and java
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I tried to summarize object orientation in my own way.
I tried to summarize iOS 14 support
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to sort the data in descending order, ascending order / Rails
I tried to write code like a type declaration in Ruby
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to make Numeron which is not good in Ruby
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I want to change the value of Attribute in Selenium of Ruby
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th
I tried a calendar problem in Ruby
[Ruby basics] Methods that frequently use blocks
I tried to summarize Java lambda expressions
I tried the new era in Java
I tried to implement the Iterator pattern
I tried to build Ruby 3.0.0 from source
[Ruby] I want to output only the odd-numbered characters in the character string
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the key points of gRPC design and development
[Ruby] I tried to diet the if statement code with the ternary operator
I thought about the best way to create a ValueObject in Ruby
I tried to solve the past 10 questions that should be solved after registering with AtCoder in Java
I tried to summarize about JVM / garbage collection
I tried to easily put CentOS-7 in a PC that I no longer need
I tried to implement polymorphic related in Nogizaka.
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I want to use arrow notation in Ruby
How to resolve errors that occur in the "Ruby on Rails" integration test
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried to get the distance from the address string to the nearest station with ruby
[Ruby basics] I tried to learn modules (Chapter 1)
I tried to output multiplication table in Java
[JavaScript] The strongest case when I tried to summarize the parts I do not understand
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!