[GO] Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]

Introduction

As part of learning Ruby, we will challenge "competitive programming (competitive professional)". We will output what we have learned in the learning for that purpose. This time from the second question (Triple Dots) of "At Coder Beginners Contest 168". https://atcoder.jp/contests/abc168/tasks/abc168_b

I will introduce my answer this time and the method and notation used for the answer.

problem

There is a string S consisting of lowercase letters. If the length of S is K or less, S is output as it is. If the length of S exceeds K, cut out only the K character from the beginning and add "..." to the end to output.

Constraint ・ K is an integer between 1 and 100 ・ S is a character string consisting of lowercase letters ・ The length of S is 1 or more and 100 or less.

The input is given in the following form.

K
S

Input example
7
nikoandsolstice
Output example
#In the case of the above example
=> nikoand...

answer

First is the code I wrote first.

k = gets.to_i
s = gets.chomp
print s.length > k ? "#{s[0...k]}..." : s

This is the first answer to the challenge of Atcoder using the ternary operator that I learned in the early days. Learning Ruby with AtCoder Beginners Selection [Product] Learn from various solutions Calculate the length of the string with the length method, compare it with K, and compare it with K. If it is larger than K, it is output by adding "..." using expression expansion, and if it is less than K, it is output as it is.

Then, the method and notation used this time are summarized below.

length method (String class)

Returns the length of the string.

#Example
print "test".length
=> 4

By the way, the length method of the Array class returns the number of elements.

How to get a part of the character string ① [first ... end]

Returns the range between the start position (first) and the end position (end) of the character string as a character string. The position can be specified in the form of "0" before the first character and "1" between the first and second characters.

a = "test"

#Position specification method (image)
0 t 1 e 2 s 3 t 4

#Example
print a[0...3]
=> tes
#Returns between 0 and 3 as a string in the above positioning method

How to get a part of the character string ② [first, length]

By the way, if you specify it in the form of [1, 3], 3 characters will be returned from the position of "1".

a = "test"

#Example
print a[1, 3]
=> est

Use this when you want to specify the end position in the form of the number of characters from the start position instead of specifying the position.

Finally

So far, I have introduced the methods learned from the second question (Triple Dots) of "AtCoder Beginners Contest 168".

If you have any mistakes, I would be grateful if you could point them out.

Recommended Posts

Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
AtCoder Beginner Contest 169 A, B, C with ruby
Ruby Learning # 8 Working With String
Learning Ruby with AtCoder 10 [1st Algorithm Practical Test DoubleCamelCase Sort]
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby learning 5
Solving with Ruby AtCoder ACL Beginner Contest C Union Find (DSU)
Ruby learning 3
Ruby learning 2
Ruby learning 6
Learning Ruby with AtCoder 11 How to receive frequently used standard input
Ruby learning 1
Learning Ruby with AtCoder 8 [1st algorithm practice test double check] Regular expression
Ruby Learning # 25 Comments
Ruby Learning # 13 Arrays
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)
Ruby Learning # 14 Hashes
Ruby Learning # 33 Inheritance
Solving with Ruby and Crystal AtCoder ABC 129 D
AtCoder ABC127 D hash to solve with Ruby 2.7.1
AtCoder Beginner Contest 168
Ruby Learning # 15 Methods
Learning Ruby with AtCoder 9 [1st Algorithm Practical Test 3rd] Sorting of array elements
Learning Ruby with AtCoder 14 [3rd Algorithm Practical Test Sprinkler] Hash creation, key / value addition
Solving with Ruby and Java AtCoder ABC129 D 2D array
AtCoder Beginner Contest 170 A, B, C up to ruby
Install Ruby 3.0.0 with asdf
Ruby Learning # 30 Initialize Method
Ruby learning points (basic)
Ruby Learning # 29 Classes & Objects
Ruby Learning # 20 Case Expressions
Ruby Learning # 17 If Statements
Ruby Learning # 21 While Loops
Ruby Learning # 31 Object Methods
Getting Started with Ruby
Ruby Learning # 27 Writing Files
[Rails] Learning with Rails tutorial
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
Ruby Learning # 26 Reading Files
11th, Classification with Ruby
Ruby Learning # 23 For Loops
Ruby Learning # 16 Return Statement
Evolve Eevee with Ruby
Learn Ruby with AtCoder Beginners Selection [Coins] Answer with short code
AtCoder dwango Programming Contest B in Ruby, Perl and Java
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)