Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)

Introduction

Participating in competitive programming as part of learning Ruby and algorithms. Here, we will output what we have learned during learning.

This time about standard output. I've been using print mainly for some reason so far, I thought it would be better if I could make a distinction, so I will summarize it.

To summarize simply

** p method ** -Output that reflects the object type (character string type is output surrounded by ** "" **. Integer type remains as it is) ・ Line breaks for each line

** puts method ** -Converts an object to a character string and outputs it ・ Line breaks for each line

** print method ** -Converts an object to a character string and outputs it ・ ** No line breaks **

p method

Outputs that reflect the type of the object. Since the character string is output in the form surrounded by ** "" **, If the answer is a character string, the answer will be incorrect. There is no problem if you answer with an integer.

p "test"
p 123

output


"test"
123

puts method

Converts the object to a character string and outputs it. A line break occurs at the end of the string.

puts "test"
puts 123

output


test
123

print method

Converts the object to a character string and outputs it. Unlike the puts method, there are no line breaks.

print "test"
print 123

output


test123

When outputting two objects in succession as described above, the character strings are output in a connected state.

Since the output format is slightly different, it seems necessary to use it properly depending on the problem.

Problem example ①

From "Atcoder Biginners Selection" Question 2 "Product"

There are two positive integers a and b. Output Even if the product of a and b is even, and Odd if it is odd.

Input example


3 4

Answer example


a, b = gets.split(" ").map(&:to_i)
p (a*b).odd? ? "Odd" : "Even" #p method
puts (a*b).odd? ? "Odd" : "Even" #puts method
print (a*b).odd? ? "Odd" : "Even" #print method

output


"Even" #p method incorrect answer
Even #puts method correct answer
Even #print method correct answer

** "" The p method enclosed in ** was incorrect. The answer with the puts method or the print method is the correct answer.

Problem example ②

From "Atcoder Biginners Selection" Question 3 "Placing Marbles"

There is a square consisting of three squares. "0" or "1" is written on each square, and marbles are placed on the square with "1" written on it. Please answer how many squares the marbles are placed on.

Input example


101

Answer example


ary = gets.split("").map(&:to_i)
count = 0
ary.each{|i| i==1 ? count+=1 : count+=0}
p count #p method
puts count #puts method
print count #print method

output


2 #p method correct answer
2 #puts method correct answer
2 #print method correct answer

In this question, any pattern of output was correct.

Problem example ③

From "Atcoder Biginners Selection" Question 1 "Welcome to AtCoder"

Given the integers a, b, c and the string s. Output the calculation result of a + b + c and the character string s side by side with a half-width space in between.

Input example


1
2 3
test

Answer example


a = gets.to_i
b, c = gets.split(" ").map(&:to_i)
s = gets.chomp
p "#{a+b+c} #{s}" #p method
puts "#{a+b+c} #{s}" #puts method
print "#{a+b+c} #{s}" #print method

output


"6 test" #p method incorrect answer
6 test #puts method correct answer
6 test #print method correct answer

In this problem as well, as in problem example (1), only the p method was incorrect. ** For problems that output a character string and answer, it seems better to answer with the puts method or print method. ** **

Finally

That's all about how to use the standard output that is often used.

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

Recommended Posts

Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)
Learning Ruby with AtCoder 11 How to receive frequently used standard input
[Ruby] How to use standard output in conditional branching
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby print puts p printf output method
How to output standard from an array with forEach
How to use Ruby return
Ruby: How to use cookies
[For Ruby beginners] What is the difference between puts print p? Actually, there is a clear way to use it properly!
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
How to use Ruby on Rails
[Ruby] How to use any? Method
How to use mssql-tools with alpine
How to use Ruby inject method
[Ruby on Rails] How to use CarrierWave
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Ruby length, size, count How to use
[Ruby] How to use slice for beginners
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby] Difference between print, puts and p
Ruby: CSV :: How to use Table Note
From terminal to Ruby (standard input / output)
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
[Java] How to get and output standard input
How to use built-in h2db with spring boot
[Ruby] How to use gsub method and sub method
How to use Java framework with AWS Lambda! ??
Output of how to use the slice method
How to use Java API with lambda expression
AtCoder ABC127 D hash to solve with Ruby 2.7.1
How to use nfs protocol version 2 with ubuntu 18.04
How to use docker compose with NVIDIA Jetson
How to use nginx-ingress-controller with Docker for Mac
[Ruby on Rails] How to use session method
[ruby] How to receive values from standard input?
[Ruby basics] How to use the slice method
Ruby standard output
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
How to use Oracle JDK 9 EA with Travis CI
How to make LINE messaging function made with Ruby
How to use Z3 library in Scala with Eclipse
[Java] How to encrypt with AES encryption with standard library
[Ruby] How to use rbenv (version `x.x.x'is not installed)
How to use JDD library in Scala with Eclipse
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet