A memorial service for the library used when competing in Ruby

Introduction

I was biting a competitive pro a few years ago. I went to light blue for the time being, but after that I didn't do much, and now I have almost nothing to do. I used Ruby to solve most of the problems until it turned light blue. I thought that the library I was using at that time would be useful to someone, so I will explain the library in this article and make a memorial service for the library I was using.

The problems I solved in the past are left at https://github.com/getty104/AtCoder. It may be easier to get an image of how to use it by looking at the code.

Library list

I will stick the snippet I used at the beginning as it is. I will explain how to use these libraries and the scenes to use them.

  require 'prime'
  require 'set'
  require 'tsort'
  include Math
  ALP = ('a'..'z').to_a
  INF = 0xffffffffffffffff
  def max(a,b);              a > b ? a : b                              end
  def min(a,b);              a < b ?  a : b                             end
  def gif;                   gets.to_i                                  end
  def gff;                   gets.to_f                                  end
  def gsf;                   gets.chomp                                 end
  def gi;                    gets.split.map(&:to_i)                     end
  def gf;                    gets.split.map(&:to_f)                     end
  def gs;                    gets.chomp.split.map(&:to_s)               end
  def gc;                    gets.chomp.split('')                       end
  def pr(num);               num.prime_division                         end
  def pr?(num);              Prime.prime?(num)                          end
  def digit(num);            num.to_s.length                            end
  def array(s,ini=nil);      Array.new(s){ini}                          end
  def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}}          end
  def rep(num);              num.times{|i|yield(i)}                     end
  def repl(st,en,n=1);       st.step(en,n){|i|yield(i)}                 end

Input / output system

I think that the competition pro will receive the data in question with standard input. Since the input format of the problem has a fixed pattern, I prepared some functions so that it can be implemented quickly. I will explain them

gif gif is a function that takes input as a single integer. It can be used when the input format is one integer.

Usage example

Input format


N

Implementation


n = gif

gff gff is the Float version of` gif. Use when you want to treat the input as a float

Input example


R

Implementation


r = gff

gsf gsf is a function used when you want to receive input as one character string. This function is often used to treat the received string as a query. Use gc, which will be described later, to manipulate the received character string.

Example


S

Implementation


s = gsf

gi This function is used when gi receives multiple integers in one line. I think it's the function I use most often.

Input example 1


A B

Implementation 1


a, b = gi

Input example 2


N1 N2 ... Nk

Implementation 2


n = gi

gf Float version of gi. It is used when you want to receive multiple values ​​as Float in one line.

Input example


r1 r2

Implementation


r1, r2 = gf

gs This is the String version of gi. It is used when you want to receive multiple values ​​as a String in one line.

Input example


S1 S2

Implementation


s1, s2 = gs

gc This function can be used when you want to receive a character string as an array (like Char).

Input example


C

Implementation


c = gc

Other

In addition to input and output, there were various functions prepared to reduce the description, so I will introduce them.

max, min In ruby, there are max and min as array methods, but in AtCoder, binary comparison is often performed, so we prepared the methods min, max.

digit digit is a function that can get the number of digits of a number.

Example


digit(100) # => 3

pr pr is a function that factores a given value into prime factors. An alias for Integer # prime_division in prime of the standard Ruby module.

Example



pr(100) #=> [[2, 2], [5, 2]]

pr? pr? Is a function that determines the prime number of a given value.

Example


pr?(2) #=> true

array, darray If you are a competition pro, you may need to prepare an array with initial values. array and darray are functions that can be used in such cases. array can be used when you want to prepare a one-dimensional array, and darray can be used when you want to prepare a two-dimensional array.

Example


dp = darray(100, 100, INF)

rep, repl I feel that rep and repl are familiar functions when doing competition pros. This is a function that makes it possible to write a for statement concisely.

Example


rep 10000 do |i|
 #Some processing
end

repl 1, 10 do |i|
 #Some processing
end

INF, ALP INF is an alias for an infinite value. It is often used by default. ALP is an array of alphabets. I use it once in a while.

Summary

There are many disadvantages to doing a competitive pro in Ruby in terms of performance and library, but if you have a chance to do a pro in Ruby today, I would appreciate it if you could refer to it.

Recommended Posts

A memorial service for the library used when competing in Ruby
Escape processing when creating a URL in Ruby
I made a Ruby extension library in C
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
Count the number of occurrences of a string in Ruby
Read mp3 tag (ID3v1) in Ruby (no library used)
Calculate the difference between numbers in a Ruby array
A note for Initializing Fields in the Java tutorial
[Ruby / Rails] Set a unique (unique) value in the class
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
Hello World with Ruby extension library for the time being
Determine that the value is a multiple of 〇 in Ruby
I searched for a web framework with Gem in Ruby
Multiplication in a Ruby array
[Ruby] What is `!!` used for?
The story of making a binding for libui, a GUI library for Ruby that is easy to install
[Learning record] I got the current time in Ruby and output a different greeting for each time.
[Ruby On Rails] In the create action and destroy action, emergency measures when redirect_to action:: show cannot be (cannot be used)
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
Sorting hashes in a Ruby array
Encoding when getting in Windows + Ruby
Thinking when introducing a new library
Implement a gRPC client in Ruby
Expression used in the fizz_buzz problem
Ruby methods often used in Rails
In file'./docker-compose.yml', the service name True must be a quoted string, i.e.'True'.
Error when the member of Entity class used in SpringWebFlux is final
I thought about the best way to create a ValueObject in Ruby
[Ruby] Returns characters in a pyramid shape according to the entered numbers
[Ruby] How to prevent errors when nil is included in the operation
How to reference a column when overriding the column name method in ActiveRecord
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement