I started AtCoder in Ruby. This was fun and addictive, and it was the moment when I realized the fun of programming from the bottom of my heart.

Well, I'm not a programmer, Here are some tips you should know to get started with AtCoder. I think that you can manage to fight by challenging AtCoder with reference to the following.

Through studying AtCoder algorithms I hope it conveys the fun of Ruby and the fun of programming.

I can solve A and B normally, and sometimes C can be solved, If you look at the following and apply it, it will probably be at the same level. ..

Except for mysterious algorithms and formulas The basics are all the following applications.

```
gets.split('')
aaa
=> ["a", "a", "a", "\n"]
```

/ n is included.

```
gets.chomp.split('')
aaa
=> ["a", "a", "a"]
```

I was able to divide it into a nice feeling by inserting chomp.

```
gets.chomp
1
=> "1"
```

The input is a character string, isn't it?

```
gets.to_i
1
=> 1
```

gets is entered as a string.

```
gets.split.map(&:to_i)
1 2 3
=> [1, 2, 3]
```

Please enter a space for each number.

```
3.times.map{gets.split.map(&:to_i)}
1 2 3
1 2 3
1 2 3
=> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
```

You now have a nested array of numbers.

```
N = gets.to_i
3
=> 3
b = N.times.map{gets.to_i}
1
2
3
=> [1, 2, 3]
```

```
a = 2
=> 2
a.even?
=> true
a.odd?
=> false
```

Determine if even? Is an even number odd? Determines if it is odd.

```
a = "aaaa"
=> "aaaa"
a.size
=> 4
a.length
=> 4
a = [1,2]
=> [1, 2]
a.size
=> 2
a.length
=> 2
```

size and length are the same. You can also arrange strings.

```
a = -10
=> -10
a
=> -10
a.abs
=> 10
```

```
if true
puts "Yes"
else
puts "No"
end
#The above can be written in one line
puts true ? "Yes": "No"
```

This is often seen in Question A, so remember it!

```
a = [0, 1 ,2, 3]
=> [0, 1, 2, 3]
a.min
=> 0
a.max
=> 3
```

```
a = [1, 1, 2, 2, 3]
=> [1, 1, 2, 2, 3]
a.uniq
=> [1, 2, 3]
```

```
a = [1, 1, 2, 2, 3]
=> [1, 1, 2, 2, 3]
a.sum
=> 9
a.inject(:+)
=> 9
#Remarks: Multiplication is as follows
a.inject(:*)
=> 12
```

Either sum or inject is fine. If it is Ruby2.3.3 series, sum may not be usable!

```
a = []
=> []
a << 1
=> [1]
a << 2
=> [1, 2]
a << 3
=> [1, 2, 3]
```

```
a = [1,2,3]
=> [1, 2, 3]
a.delete_at(-1)
=> 3
a
=> [1, 2]
```

Convenient because you can specify the position of any array

```
a = [1,2,3]
=> [1, 2, 3]
a.delete(1)
=> 1
a
=> [2, 3]
a = [1,1,2,3]
=> [1, 1, 2, 3]
a.delete(1)
=> 1
a
=> [2, 3]
```

I wrote it differently from delete_at, It may be rare to delete the specified number I often see duplicate removal using uniq

```
a = [1, 1, 2, 2, 3]
=> [1, 1, 2, 2, 3]
a[-1]
=> 3
```

```
#Take out the 5th
n = 5
=> 5
a = [1, 1, 2, 2, 3]
=> [1, 1, 2, 2, 3]
a[n]
=> nil
a[n - 1]
=> 3
```

The array starts at 0, so you have to -1.

```
a = [1, 2, 3, 4]
a.each do |i|
if i == 2
puts i
break
end
end
```

By the way, the difference between Break and Return is whether to break out of the loop or the method. Return can only be used inside a method.

```
a = [2,2,2,2]
=> [2, 2, 2, 2]
a.all?{|n| n.even?}
=> true
a
=> [2, 2, 2, 2]
a.all?{|n| n == 2}
=> true
a = [2,2,2,3]
=> [2, 2, 2, 3]
a.all?{|n| n == 2}
=> false
```

Returns true because all the elements of the array are even.

```
a = [3,1,2]
=> [3, 1, 2]
a.sort
=> [1, 2, 3]
a.sort.reverse
=> [3, 2, 1]
#By the way, the character strings in the array. ..
a = [ "aaa", "ccc","bbb"]
=> ["aaa", "ccc", "bbb"]
a.sort
=> ["aaa", "bbb", "ccc"]
a.sort.reverse
=> ["ccc", "bbb", "aaa"]
```

See quite a lot

```
a = [5,6,7]
=> [5, 6, 7]
a.drop(1)
=> [6, 7]
#It doesn't seem to be destructive.
a
=> [5, 6, 7]
a.drop(1)
=> [6, 7]
a.drop(2)
=> [7]
a.drop(3)
=> []
```

drop does not seem to be destructive

```
a[0]
=> 5
a.shift
=> 5
#↑ Same?
a = [5,6,7]
=> [5, 6, 7]
a.shift
=> 5
a.shift
=> 6
a.shift
=> 7
a = [5,6,7]
=> [5, 6, 7]
a[0]
=> 5
a[0]
=> 5
```

Shift seems destructive. I thought it was the same.

```
a = [1, 2, 3, 4]
count = 0
a.each do |i|
if i == 1
count += 1
end
end
puts count
=>1
```

```
k = 2
a, b = [1, 3]
(a..b).each do |c|
if c % k == 0
puts 'OK'
break
end
if c == b
puts 'NG'
end
end
```

```
x = 2
yen = 100
count = 0
while yen > x do
yen += yen / 100
count = count + 1
end
puts count
```

While loops as long as it remains true. You don't need a conditional expression, just break yourself If you want to exit and exit, use loop do ... end! Of course, while true do ... end is fine.

```
a = ["a", "b", "c", "d"]
=> ["a", "b", "c", "d"]
a.join
=> "abcd"
```

```
a = ["a","b","a","b","a"]
=> ["a", "b", "a", "b", "a"]
a.join
=> "ababa"
a.join.scan("ab")
=> ["ab", "ab"]
#further"ab"Take out the number of.
a.join.scan("ab").length
=> 2
```

It seems that scan can be used quite well. ..

```
c = gets.chomp
alf = []
("a".."z").each do |i|
alf << i
end
#Specify the contents and index of the array as blocks
alf.each_with_index do |k, i|
if k == c
puts alf[i+1]
break
end
end
```

If only k is used, the contents of the array "a", "b" ... "z" will be extracted. You can't specify an index, right? each.with.Using index|k, i|Then You can specify the index of the array in i. It's 0 in alf [0]. The content of the actual array is k.

```
a = Array.new(3)
=> [nil, nil, nil]
a = Array.new(3).map{Array.new(3, 0)}
=> [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
a[0][1] = 1
=> 1
a
=> [[0, 1, 0], [0, 0, 0], [0, 0, 0]]
a[2][1] = 1
=> 1
a
=> [[0, 1, 0], [0, 0, 0], [0, 1, 0]]
a[2][0] = 1
=> 1
a
=> [[0, 1, 0], [0, 0, 0], [1, 1, 0]]
```

It's complicated when it comes to 2D

```
a = 2
=> 2
b = 3
=> 3
a.to_s * b
=> "222"
```

The input of a entered as a number is converted into a character string, and b is repeated 3 times.

```
a = 1.4
=> 1.4
a.floor
=> 1
a.ceil
=> 2
a.round
=> 1
a =1.5
=> 1.5
a.floor
=> 1
a.ceil
=> 2
a.round
=> 2
```

```
#Greatest common divisor
4.gcd(2)
=> 2
4.gcd(4)
=> 4
4.gcd(6)
=> 2
#Least common multiple
4.lcm(2)
=> 4
4.lcm(4)
=> 4
4.lcm(6)
=> 12
```

To conclude first, puts are good. The answer does not match because p has "".

```
a = "aaa"
=> "aaa"
p a
"aaa"
=> "aaa"
puts a
aaa
=> nil
print a
aaa=> nil
```

```
#String
a = ["3","4"]
=> ["3", "4"]
a
=> ["3", "4"]
a.index("3")
=> 0
a.index("4")
=> 1
```

```
n = gets.to_i
a = gets.split.map(&:to_i)
sum = 1
#Basically turn with each, but note that there is 0 in the multiplication
a.each do |i|
if i == 0
sum = 0
break
end
#Sum for large numbers<=Let's do 〇〇!
sum *= i if sum <= 〇〇
end
```

```
require 'bigdecimal'
require 'bigdecimal/util'
l = gets.chomp.split(" ")
a = l[0].to_i
#to_It seems that an error will occur if it is set to f
#The answer may not match.
b = l[1].to_d
puts (b * a).floor
```

```
require 'prime'
a = 100
=> 100
n = a.prime_division
=> [[2, 2], [5, 2]]
#[[2, 2], [5, 2]]Is"(2**2) * (5**2) = 100"That means
#Since it is a two-dimensional array, specify two elements
n.each do |i, k|
puts i
puts k
end
2
2
5
2
=> [[2, 2], [5, 2]]
```

```
n = 3
=> 3
a = [1,2,3]
=> [1, 2, 3]
a.permutation(n).to_a
=> [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
```

Very convenient!

```
n = "testrestdest"
=> "testrestdest"
a
=> ["test", "rest", "pest", "dest"]
a.each do |i|
n.gsub!(/#{i}/, '')
end
n
=> ""
```

All disappeared It is destructive with gsub !.

```
#Atcoder149B
#https://atcoder.jp/contests/abc149/tasks/abc149_b
a,b,k = gets.chomp.split.map(&:to_i)
k.times do
if a >= 1
a = a - 1
elsif b >= 1
b = b- 1
elsif a = 0 && b = 0
end
end
puts "#{a} #{b}"
#Time over
a, b, k = gets.split().map(&:to_i)
if a >= k
puts "#{a-k} #{b}"
elsif a+b <= k
puts "#{0} #{0}"
else
puts "#{0} #{b-(k-a)}"
end
#OK
```

```
a and b are the first coordinates
a = 1
=> 1
a = -2
=> -2
a - b
=> -5
(a - b).abs
=> 5
```

```
x = gets.to_i
puts 360.lcm(x) / x
```

If the number is divisible by 360 degrees, you can divide it normally, If the value is not divisible by 360 degrees, find the least common multiple of 360 degrees It seems that you need to break it.

Recommended Posts