Need to do left padding a string using String#%

Hi,

I have some dynamic strings of size <= 10, but never > 10. Now when I
will be having a string size < 10, I want then to left padded with it
'x'.

Like If I have -

"1113wf" # => "xxxx1113wf"
"23wef55" # => "xxx23wef55"

I am thinking to code this using `String#%` (
http://www.ruby-doc.org/core-2.1.0/String.html#method-i-25 )

But I am not able to figure this out.

Can anyone help me for the same?

···

--
Posted via http://www.ruby-forum.com/.

Have you looked at String#rjust?

~ ∙ pry
[1] pry(main)> "1113wf".rjust(10, 'x')
=> "xxxx1113wf"
[2] pry(main)> "23wef55".rjust(10, 'x')
=> "xxx23wef55"
[3] pry(main)> "A very long string".rjust(10, 'x')
=> "A very long string"

Hope this helps,

Mike

···

On Jan 28, 2014, at 2:10 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Hi,

I have some dynamic strings of size <= 10, but never > 10. Now when I
will be having a string size < 10, I want then to left padded with it
'x'.

Like If I have -

"1113wf" # => "xxxx1113wf"
"23wef55" # => "xxx23wef55"

I am thinking to code this using `String#%` (
Class: String (Ruby 2.1.0) )

But I am not able to figure this out.

Can anyone help me for the same?

--
Posted via http://www.ruby-forum.com/\.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Mike Stok wrote in post #1134689:

Have you looked at String#rjust?

~ ∙ pry
[1] pry(main)> "1113wf".rjust(10, 'x')
=> "xxxx1113wf"
[2] pry(main)> "23wef55".rjust(10, 'x')
=> "xxx23wef55"
[3] pry(main)> "A very long string".rjust(10, 'x')
=> "A very long string"

Hope this helps,

Mike

@Mike - Yes, I know this. But I have been asked in an interview to do
this using `String#%`. But I couldn't answer it.

···

--
Posted via http://www.ruby-forum.com/\.

There's likely to be more than one way to do it, but this is the first
one I could come up with for padding a string with a specific character

["1113wf", "23wef55"].each {|s| puts '%s%s' % ['x'*(10-s.length),s] }
xxxx1113wf
xxx23wef55

···

--
Posted via http://www.ruby-forum.com/.

Yes, by doing it that way I may as well just stick Strings together; but
I was trying to find a way of sticking to the constraint: String#%

···

--
Posted via http://www.ruby-forum.com/.

I want to see how the interviewer does it, maybe they've phrased the
question poorly :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

If I were interviewing you, I'd turn the question around and ask
you to explain why you'd one over the other.
Again, seeking your ability think things through, not accuracy.

And if I would be the one interviewed, I'd tell the interviewer
that the solution:

  "1113wf".rjust(10, 'x')

Is much more readable than the solution:

  s = "1113wf"; puts '%s%s' % ['x'*(10-s.length),s]

If the interviewer favours the 2nd then I tell him to stop
being stupid and use perl instead. That way he can worship
random syntax being a valid program.

rjust() is just perfect for this and perfectly well suited
for the task.

I do not think there is a robust way because there is no (s)printf
flag that can change the padding character for strings. Maybe that was
the answer the interviewer was fishing for.

Perhaps, we can never know. Perhaps the interviewer was an idiot too,
we can't be certain. I am assuming the latter currently. :slight_smile:

The thing here is that .rjust() is the best answer by far. Any other
answer, as long as the code stinks as much as the above, is a much
worse answer, and it seems stupid to want to reason in favour of
code that is longer and less elegant. If there is a shorter variant
than .rjust then let's have at it - I would be all in favour of that!

There is more than one way to do it but there are also clearly
dumber ways to do it. And just because you can swim in the Atlantic
Ocean does not mean you should have to when you could use a plane
instead (and the water is damn cold and there are polar bears out
there to get you, at least towards the north pole; I always
remember that the penguins are only at the south pole, and
they are mutually exclusive!).

I ask the candidate how they would go about
implementing String#% and String#rjust. I wouldn't
necessarily expect them to know how, exactly, but
what I'm interested in is their approach.

rjust is easy but how would you want to implement %
without using sprintf()? You would have to parse the
whole input string and re-format it according to the
special tokens given. That reminds me of C, so I am
wondering what kind of crazy interviews you guys are
doing? And, can I just copy paste the actual C code
anyway? Well I guess it would serve as an answer, so
I would have to look it up ONCE, then I am prepared to
answer such stupid questions...

I'd actually want REAL questions, REAL answers
AND then whether you did decide to hire that guy or not,
because I am beginning to believe that there are a lot
of fake questions out there for no real merit to have.

You don't need to know the alternatives to build useful
and great programs - you think someone who knows how to
implement % from scratch is a better ruby programmer
than someone who spent 10 years writing ruby code,
wrote a few hundred documented gems but simply can't
be bothered to want to know every little unimportant
detail? Really??? :slight_smile:

I would be SIGNIFICANTLY more interested in asking
unspecific questions such as buildup to specific
programs, how a candidate works, how he will ensure
that his code will still work even when he is no
longer in that company, and so on and so forth. And
of course what he did already build so far and whether
he is pleased with his own code quality or not.

···

--
Posted via http://www.ruby-forum.com/\.

See `Kernel::sprintf` -- Method: Kernel#sprintf — Documentation for core (3.0.2)

There are a few examples in there about using strings, specifically the `s`
type.

···

On Tue, Jan 28, 2014 at 12:34 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Mike Stok wrote in post #1134689:
> Have you looked at String#rjust?
>
> ~ * pry
> [1] pry(main)> "1113wf".rjust(10, 'x')
> => "xxxx1113wf"
> [2] pry(main)> "23wef55".rjust(10, 'x')
> => "xxx23wef55"
> [3] pry(main)> "A very long string".rjust(10, 'x')
> => "A very long string"
>
> Hope this helps,
>
> Mike

@Mike - Yes, I know this. But I have been asked in an interview to do
this using `String#%`. But I couldn't answer it.

--
Posted via http://www.ruby-forum.com/\.

--
Ryan Cook
720.319.7660

Although I suppose then a format is not required at all as something like

    ["1113wf", "23wef55"].map { |s| 'x' * (10 - s.length) + s }
    # => ["xxxx1113wf", "xxx23wef55"]

Or if the string is guaranteed to not have spaces, then I suppose
using a format something like

    ["1113wf", "23wef55"].map { |s| ("%10s" % s).tr(' ', 'x') }
    # => ["xxxx1113wf", "xxx23wef55"]

but that seems quite a strong constraint. But given a free choice, I
suspect I'd prefer Mike's `"1113wf".rjust(10, 'x')` which seems much
neater than mine and doesn't suffer from the constraint.

Peace,
tiredpixel

···

On 28/01/2014 21:33, Joel Pearson wrote:

There's likely to be more than one way to do it, but this is the
first one I could come up with for padding a string with a specific
character

["1113wf", "23wef55"].each {|s| puts '%s%s' % ['x'*(10-s.length),s]
} xxxx1113wf xxx23wef55

Understood. :slight_smile: It was merely a musing of mine. :slight_smile:

···

On 28/01/2014 22:09, Joel Pearson wrote:

Yes, by doing it that way I may as well just stick Strings
together; but I was trying to find a way of sticking to the
constraint: String#%

I do not think there is a robust way because there is no (s)printf
flag that can change the padding character for strings. Maybe that was
the answer the interviewer was fishing for.

You can do this:

irb(main):010:0> ("%10s" % "foo").sub(/^ +/) {|m| "x" * m.length}
=> "xxxxxxxfoo"

But:
- it's not only String#%
- it's broken:

irb(main):011:0> ("%10s" % " space?").sub(/^ +/) {|m| "x" * m.length}
=> "xxxxspace?"

But output should be
=> "xx space?"

Cheers

robert

···

On Tue, Jan 28, 2014 at 8:34 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Mike Stok wrote in post #1134689:

Have you looked at String#rjust?

~ * pry
[1] pry(main)> "1113wf".rjust(10, 'x')
=> "xxxx1113wf"
[2] pry(main)> "23wef55".rjust(10, 'x')
=> "xxx23wef55"
[3] pry(main)> "A very long string".rjust(10, 'x')
=> "A very long string"

Hope this helps,

Mike

@Mike - Yes, I know this. But I have been asked in an interview to do
this using `String#%`. But I couldn't answer it.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Personally, in an interview, I ask the candidate how they would go about
implementing String#% and String#rjust. I wouldn't necessarily expect them
to know how, exactly, but what I'm interested in is their approach. I'd let
them use any approach they'd like as well, as long as they could explain
why they chose it.

On 1/30/14, Robert Klemme > I do not think there is a robust way
because there is no (s)printf

flag that can change the padding character for strings. Maybe that was
the answer the interviewer was fishing for.

and maybe the interviewee can suggest that he/she be allowed for
double reference/format like "%s%s"

... and come up with sensible reasoning. Completely agree.

Kind regards

robert

···

On Thu, Jan 30, 2014 at 4:27 AM, tamouse pontiki <tamouse.lists@gmail.com> wrote:

Personally, in an interview, I ask the candidate how they would go about
implementing String#% and String#rjust. I wouldn't necessarily expect them
to know how, exactly, but what I'm interested in is their approach. I'd let
them use any approach they'd like as well, as long as they could explain why
they chose it.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

tamouse m. wrote in post #1134890:

Personally, in an interview, I ask the candidate how they would go about

them use any approach they'd like as well, as long as they could explain
why they chose it.

For my case, I didn't think of about String#%, rather String#rjust. But
I have chosen it, as it fits with the need directly. Is there any
technical reason to use it over `String#%` ?

Please tell me.

Thanks
Arup

···

--
Posted via http://www.ruby-forum.com/\.

If I were interviewing you, I'd turn the question around and ask you
to explain why you'd one over the other.
Again, seeking your ability think things through, not accuracy.

···

On Thu, Jan 30, 2014 at 7:17 AM, Arup Rakshit <lists@ruby-forum.com> wrote:

tamouse m. wrote in post #1134890:

Personally, in an interview, I ask the candidate how they would go about

them use any approach they'd like as well, as long as they could explain
why they chose it.

For my case, I didn't think of about String#%, rather String#rjust. But
I have chosen it, as it fits with the need directly. Is there any
technical reason to use it over `String#%` ?