got any good string exercises?

A

Adam Akhtar

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??
 
A

Adam Akhtar

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

Rob Biedenharn

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??

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
L

Leslie Viljoen

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"
 
I

Ilan Berci

Adam said:
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"

:)

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

hth helps
 
R

Raju Gandhi

[Note: parts of this message were removed to make it a legal post.]

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
 
A

Adam Akhtar

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!
 
J

James Gray

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 Quiz 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:

http://www.rubyquiz.com/quiz14.html

http://www.rubyquiz.com/quiz44.html

http://www.rubyquiz.com/quiz78.html

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

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top