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?

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 R. [email protected] wrote:

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/.

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Mike S. 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.

See Kernel::sprintfhttp://rubydoc.info/stdlib/core/Kernel:sprintf

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

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 P. 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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJS6CbyAAoJEOFolTkanF7VyXwH/jbxFMUh2UK4LX2S/EELZVlh
q2s9lBMoqBAs3sCHS+yK7O9fq902tNxddZHkevdTAG6iYUMZvo7lEv1ewJH5q7e1
ULtbkg8HpAxcJ+HSd1CIEc/EBeq0fHUIDHcW1v61HWK+TrVh1+vyUv9SyU0cC7F2
7ccT9CM68uuT12/Mfg+U+V/635IZo0EDfVRoBAnzwa+EERPBHkHXYiBtjMxoyR9Q
ffnqYP3tSuJMZubQngIoMhJjwgGqj8ZMYaVTPemrU2HXoRmbtmwPEsUPb9J2v8oS
L4gX6v4Iku3Bbfio+MXZFRKNnGv5w/TYTBaS3iwt8waPJFK28KhA8rTkkPzm4Dg=
=U7VF
-----END PGP SIGNATURE-----

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#%

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 28/01/2014 22:09, Joel P. 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#%

Understood. :slight_smile: It was merely a musing of mine. :slight_smile:
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJS6CywAAoJEOFolTkanF7V/v8IAKwdwTt4bfkb2lJXWdnMrnBl
2rIYMTlNWfmVAKn3FC5xbVp1Kraw+Q045jbJPf5A9uU74mGodoiBqY+D4QkXWfdm
mPGNJ6jhm7DGCTW3IO15iPCgLjjiwYF0NndZm3k73ubysewloJmTRJjm04IctpOD
M8fuB0zhBqokzG1vwBwT0nGjfKJgdS7zEYqP1ou5rcxMA0EuhLvqbmFcMmz8ANBE
NsCsUlloCrJDIAw7k6mybPeiYRR1n+eRK1bnXqMIac9XZ32j6BAJfCF55NVnCACy
10R+jA6vnDfw2iKRV829FiTQ9QEnjsCMMEMx2NmrhblNPHK0EX4y0l4kKQ3+bhU=
=5ikl
-----END PGP SIGNATURE-----

On Tue, Jan 28, 2014 at 8:34 PM, Arup R. [email protected]
wrote:

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.

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 1/30/14, Robert K. > 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”

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 Thu, Jan 30, 2014 at 4:27 AM, tamouse pontiki
[email protected] 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.

… and come up with sensible reasoning. Completely agree.

Kind regards

robert

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

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

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.

On Thu, Jan 30, 2014 at 7:17 AM, Arup R. [email protected]
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#% ?

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs