It will be a memo for learning
Since two integers separated by spaces and a character string are input, output a substring of the range of two integers.
Input example 1
2 6
this is a pen
Output example 1
his i
Input example 2
2 6
Welcome to the paiza! I`m studying ruby!
Output example 2
elcom
nums = gets.chomp.split(" ")
str = gets.chomp.split("")
for i in (nums[0].to_i - 1)..(nums[1].to_i - 1)
print str[i]
end
nums = gets.chomp.split(" ")
str = gets.chomp.split("")
Nums
is the output range, and str
is the character string decomposed and assigned to the input numbers and character strings.
gets method: Receives input as a "character string" line by line. chomp method: Removes line breaks in character strings. split method: Splits a character string into an array.
for i in (nums[0].to_i - 1)..(nums[1].to_i - 1)
Convert the range from nums [0]
to nums [1]
to an integer with the for statement and assign it to the i variable.
print str[i]
Output the range of i variables in the string of str
I would appreciate it if you could point out any mistakes.
Recommended Posts