How can I shorten:
p=gets.to_i a=gets b=gets.to_i
If my input is an integer, followed by a string, followed by an integer?
Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up.Sign up to join this community
(disclaimer: I don't know Ruby, but this works on TIO)
p,a,b=$* p=p.to_i b=b.to_i
28 26 bytes instead of 30 (thanks to Snack for pointing out the $* trick)
Answers are typically allowed as lambda functions with your input/output being the parameters/return value of the lambda, so you can do this:
If you assigned this to a variable
f then it would be called as
It's generally fine to assume the types of the inputs as well, but to be safe you can mention it in your answer.
If you need a full program with stdin/stdout io for some reason, the shortest you can do is use the
-n flag to shorten one call to
p=eval$_ a=gets b=gets.to_i
-n flag surrounds your code with
while gets ... end, so the program will loop if more input is supplied than your program consumes.