Got any good string exercises?

Looking for fairly simple string exercises that will familiarise myself
with all those string methods in the back of pickaxe. Dont want major
exercises like those in the ruby quiz as they demand knowledge in a lot
of other areas.

Heres one example of the sort im looking for

Rotate the words in a string around a fixed point given by a letter. Eg.
string = “Ruby is a great language”
puts Rotate(string, “a”)

output: “a great language ruby is”

ok that one is probably at upper end of the scale when it comes to
difficulty but its quite focused.

Anyone got any other string practice exercises that are short,
relatively simple and use some of the methods??

sorry when i said that was quite difficult ignore that. Its actually the
right level of difficulty and am willing to go higher as long as the
problems remain focussed on string use.

On Feb 5, 2008, at 8:05 AM, Adam A. wrote:

Eg.
string = “Ruby is a great language”
puts Rotate(string, “a”)

output: “a great language ruby is”

ok that one is probably at upper end of the scale when it comes to
difficulty but its quite focused.

Anyone got any other string practice exercises that are short,
relatively simple and use some of the methods??

Well, you could always tackle the ones that pop up continually on this
list.

Also, you will almost certainly have to know about more than just
String to do most things.

First of all, the method would be called “rotate”, not “Rotate”.
Initial capitals are an indication of a constant in Ruby. You’d
either have:

def rotate(string, to_word)

end

to define a standalone method (which you’d likely do only in irb), or:

class String
def rotate(to_word)
# do something with self, probably returning a new String

end
end

to let you say something like:

“Ruby is a great language”.rotate(“a”)

In any case, just try to solve something and if you want help, post
your attempt to the list and be specific about the amount of help that
you want (or you’ll get solutions rather than hints).

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Adam A. wrote:

Looking for fairly simple string exercises that will familiarise myself
with all those string methods in the back of pickaxe. Dont want major
exercises like those in the ruby quiz as they demand knowledge in a lot
of other areas.

Heres one example of the sort im looking for

Rotate the words in a string around a fixed point given by a letter. Eg.
string = “Ruby is a great language”
puts Rotate(string, “a”)

output: “a great language ruby is”

ok that one is probably at upper end of the scale when it comes to
difficulty but its quite focused.

Anyone got any other string practice exercises that are short,
relatively simple and use some of the methods??

irb(main):002:0> “Ruby is a great language”.reverse.split(" ").map {|w|
w.reverse}.join " "
=> “language great a is Ruby”

:slight_smile:

Best way is to just look at the string methods… String.methods -
Object.instance_methods

hth helps

On Feb 5, 2008 3:14 PM, Adam A. [email protected] wrote:

sorry when i said that was quite difficult ignore that. Its actually the
right level of difficulty and am willing to go higher as long as the
problems remain focussed on string use.

Count the number of non-capitals in a string.

Assume the string is digits and put commas in for pretty printing:
“1000” → “1000”
“10000” → “10000”
“100000” → “100,000”
“1000000000” → “1000,000,000”

Hey guys/girls

thanks for the responses and solutions to my problem. Actually that was
just posted as an example of the type of problems im requesting from the
forum. I closed my eyes whilst scrolling at those points so i have some
example answers to come back to later.

Unit testing is good. Ive been doing it a little bit thanks to a link
through ruby.about.com. Ill keep going with it. I’d also like some
exercises to practice them on though.

As for exercises involving strings and nothing but strings, obviously it
wouldnt provide many problems to work with. Stuff like arrays, loops,
flow control are all fine requisites to these problems but stuff like
inheritance etc would just be an obstacle for me at my current stage.

The number one posted above seems quite useful. Could wrap that up in a
function and use in other programes.

In the mean time keep em coming if you can think of any!

Hey Adam,

There was a suggestion by Joe at CodeMash, that really helped me. Write
unit
tests! Write unit tests for methods in the core ruby classes. This
allows
you to work with all the API’s without having to have a problem to
solve,
and its self documenting.

Currently I am writing unit tests for the Array class, and I have
methods
like test_flatten, test_flatten_with_nested_arrays, etc. Great ruby
practice
coding, you can do it over your lunch break (if you don’t work with ruby
to
pay the bills :D), and you can always keep that project with you in case
you
want to test a boundary condition. IRB works great too, but I like
journaling my discoveries with Ruby…

Hope this helps…

Raju

On Feb 5, 2008, at 9:26 AM, Adam A. wrote:

As for exercises involving strings and nothing but strings,
obviously it
wouldnt provide many problems to work with. Stuff like arrays, loops,
flow control are all fine requisites to these problems but stuff like
inheritance etc would just be an obstacle for me at my current stage.

Well, I know you said the Ruby Q. was not for you earlier and I’m
obviously biased, but I feel you should have another look.

Quizzes run the gamut of difficultly levels and what you need to
know. We’ve also had some great String focused problems.

Have a look at these, for example:

Ruby Quiz - LCD Numbers (#14)

Ruby Quiz - Word Chains (#44)

Ruby Quiz - Bracket Packing (#78)

The great thing about using the quiz is that you can compare your
solution with others when you are done.

James Edward G. II