[RUBY] List of methods used in PAIZA D rank

Introduction

As a major premise, posting the answer is prohibited, so please use it as a reference only. I will not explain in detail because it can be cleared without using arrays and hashes. In addition, the poster is a fledgling inexperienced person and is currently ranked B, and it is undeniable that he lacks knowledge. Please note that the detailed method explanation is omitted.

Get input value

Input example 1(input)
  10

##Get 10 as a number
input = gets.chomp.to_i
=> 10

##Get 10 as a string
input = gets.chomp.to_s
=> "10"

##Divide 10 and get as a numerical value
input = gets.chomp.split("").map(&:to_i)
=> [1, 0]

##Divide 10 and get as a numerical value
input = gets.chomp.split("").map(&:to_s)
=> ["1", "0"]


Input example 2(input)
  10 20

## 10,Get 20 as a number
input = gets.chomp.split.map(&:to_i)
=> [10, 20]

## 10,Get 20 as a string
input = gets.chomp.split.map(&:to_s)
=> ["10", "20"]


Input example 3(input)
1
2
3

## 1,2,Get 3 as a number
input = readlines.map(&:chomp).map(&:to_i)
=> [1, 2, 3]

## 1,2,Get 3 as a string
input = readlines.map(&:chomp)
=> ["1", "2", "3"]

I don't recommend the readlines method because it becomes difficult to use when it comes to B and C ranks.

Value output

Output example 1
input = 1

##Output 1 as a number
puts input
=> 1


Output example 2
input = ["1", "2", "3"]

## 1,2,Output 3 as a number
puts input.map(&:to_i)
=> [1, 2, 3]

##Output only 1 as a character string
puts input[0]
=> "1"

##Output only 3 as a numerical value
puts input[2].to_i
=> 3


Output example 3
input1, input2, input3 = ["1", "2", "3"]

##Output only 2 as a character string
puts input2
=> "2"

##Output only 1 as a numerical value
puts input1.to_i
=> 1

## "1 2 3"Output as the character string
puts input1 + " " + input2 + " " + input3
Or
puts "#{input1} #{input2} #{input3}"
=> "1 2 3"

If you are not aware of the numerical values and character strings, you will not be able to output as you want, so be careful.

Method

map method

Returns the result of evaluating the block for each element as a new array.

input = [1, 2, 3]
puts input.map { |n| n * 2 }
=> [2, 4, 6]

input = ["1", "2", "3"]
puts input.map { |n| n.to_i }
Or
puts input.map(&:to_i)
=> [1, 2, 3]

join method

Returns a string that concatenates each element.

input = ["a", "b", "c"]
puts input.join
=> "abc"

puts input.join(",")
=> "a,b,c"

puts input.join(" or ")
=> "a or b or c"

slice method

Returns the specified own element.

input = ["a", "b", "c"]
puts input.slice(1)
=> "b"

##Get 1 to 2
puts input.slice(1, 2)
=> ["b", "c"]

##Destructive method
input.slice!(0)
puts input
=> ["b", "c"]

input.slice!(1, 2)
puts input
=> "a"

index method

Searches to the right from the string index and returns the leftmost index of the first substring found. If not found, returns nil.

input = [1, 2, 3, 4, 5]

puts input.index(2)
=> 1

puts input.index("2")
=> nil

input = "111111"
## "1"Search from the third string
puts input.index("1", 3)
=> 3

upcase, downcase methods

Returns a string with all lowercase letters replaced by the corresponding uppercase letters. (upcase method) Returns a string with all uppercase letters replaced by the corresponding lowercase letters. (downcase method)

input = "abcd 1234 GHJK !!"
puts input.upcase
=> "ABCD 1234 GHJK !!"

input = "abcd 1234 GHJK !!"
puts input.downcase
=> "abcd 1234 ghjk !!"

sub, gsub methods

Generates and returns a string that replaces the first matching part of the string. The gsub method will generate and return a string that replaces all the matched parts.

input = "aaaabbbb11112222"
puts input.sub("a", "A")
=> "Aaaabbbb11112222"

puts input.gsub("a", "A")
=> "AAAAbbbb11112222"

puts input.gsub("a", "A").gsub("1", "3")
=> "AAAAbbbb33332222"

length method

Returns the number of characters in the string.

input = "aaaabbbb11112222"
puts input.length
=> 16

input = "aaaa bbbb 1111 2222 !"
puts input.length
=> 21

count method

Returns the number of elements in the receiver.

input = [1, 2, 3, 4, 4]
puts input.count
=> 5

puts input.count(4)
=> 2

##Divide each element by 2 and count only 0
puts input.count {|x| x % 2 == 0}
=> 3

reverse method

Returns a string that is a left-right inverted string for each character.

input = "paiza Drank 12"
puts input.reverse
=> "21 knarD aziap"

input = ["1", 2, "3", "true"]
puts input.reverse
=> ["true", "3", 2, "1"]

shift method

Removes the first element of the array and returns it. If an argument is specified, that number is removed and it is returned as an array.

input = [1, 2, 3, 4, 5]
puts input.shift
=> 1
puts input
=> [2, 3, 4, 5]

input = ["a", "b", "c", "d"]
puts input.shift(3)
=> ["a", "b", "c"]
puts input
=> ["d"]

sum method

Returns the sum of the elements.

input = [1, 2, 3, 4, 5]
puts input.sum
=> 15

input = ["12", "45"]
p input.sum
=>error

max, min methods

Returns the largest element. (max method) Returns the smallest element. (min method)

input = [1, 2, 3, 4, 5]
puts input.max
=> 5

input = [1, 2, 3, 4, 5]
puts input.min
=> 1

input = ["abcde", "123", "ABCD"]
puts input.max
=> "abcde"

input = ["abcde", "123", "ABCD"]
puts input.min
=> "123"

odd ?, even? Method

Returns true if it is odd. Otherwise it returns false. (odd? method) Returns true if it is even. Otherwise it returns false. (even? method)

puts 5.odd?
=> true

puts 2.odd?
=> false

puts 2.even?
=> true

puts 5.even?
=> false

abs method

Returns the absolute value of the value.

puts 100.abs
=> 100

puts -100.abs
=> 100

round method

Returns the integer or real number closest to itself. It is so-called rounding, but it is not even rounding.

input = 3.141592
puts input.round
=> 3

##Rounds off to the third decimal place and returns the value
puts input.round(2)
=> 3.14

##Rounds the 4th decimal place and returns the value
puts input.round(3)
=> 3.142

ceil method

Returns the value with the decimal point rounded up

input = 3.141592
puts input.ceil
=> 4

##Rounds up to the third decimal place and returns the value
puts input.ceil(2)
=> 3.15

##Round up the 4th decimal place and return the value
puts input.ceil(3)
=> 3.142

floor method

Returns the value with decimal point truncation

input = 3.141592
puts input.floor
=> 3

##Returns the value with the third decimal place truncated
puts input.floor(2)
=> 3.14

##Returns the value with the 4th decimal place truncated
puts input.floor(3)
=> 3.141

At the end

I would be honored if you could help me even a little. If you have any mistakes, please leave a comment. If I have time, I plan to summarize the methods used in C rank!

Recommended Posts

List of methods used in PAIZA D rank
List of methods used when manipulating strings
About methods often used in devise
Differences in split methods of StringUtils
Frequently used methods in Active Record
List of members added in Java 9
How to achieve paiza rank D
List of types added in Java 9
Ruby methods often used in Rails
[rails] List of actions defined in Controller
Order of modifiers used in Swift (see SwiftLint)
Summary of frequently used commands in Rails and Docker
List of things I used without understanding well: Ruby
Get a list of classes in a Guava specific package
The objects in the List were references, right? Confirmation of
Personal summary of the guys often used in JUnit 4
List of frequently used Java instructions (for beginners and beginners)