[RAILS] Safe numbers (ruby edition)

[Problem] Safe numbers (ruby edition)

problem

I'm trying to avoid regular numbers when thinking about 4-digit passwords. The password string s, which consists of four digits, is entered. If there are two or more of the same numbers, output "NG", otherwise output "OK".

Value to be entered

The input is given in the following format.

s

-The first line is given the password string s, which consists of four digits. -The total number of inputs is one, and one line break is inserted at the end of the last line of the input value.

Expected output

The password string s, which consists of four digits, is entered. If there are two or more of the same numbers, output "NG", otherwise output "OK".

Input example 1

2020

Output example 1

NG

Input example 2

1234

Output example 2

OK

My answer

python


a = gets.chomp.chars
if (a.count - a.uniq.count) > 0
    puts "NG"
else
    puts "OK"
end

In the chars method on the first line, if you compare it with Example 1, it is divided character by character like ["2" "0" "2" "0"].

In the second line, if the number obtained by subtracting the return value 2 of a.uniq.count from the return value 4 of a.count is greater than 0, "NG" is output.

uniq method

A method that deletes duplicate elements in the array elements and returns them as the deleted array

count method

that's all!

Recommended Posts

Safe numbers (ruby edition)
Pedometer (Ruby edition)
Fizz Buzz (Ruby edition)
Ruby Learning # 9 Math & Numbers
Minimum value (ruby edition)
Q. Elevator (Ruby edition)
ruby memorandum (basic edition)
Offline environment construction Ruby edition
[Ruby] Find numbers in arrays
Dharma doll drop of characters (ruby edition)
Ruby on Rails installation method [Mac edition]
Convert numbers to Roman numerals in Ruby
[Ruby] A program that determines prime numbers
[Ruby] Express n-ary numbers as numbers starting from 0
[Problem] Weather on consecutive holidays (Ruby edition)