[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

got any good string exercises?

Adam Akhtar

2/5/2008 1:06:00 PM

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??
--
Posted via http://www.ruby-....

8 Answers

Adam Akhtar

2/5/2008 1:15:00 PM

0



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.
--
Posted via http://www.ruby-....

Rob Biedenharn

2/5/2008 1:25:00 PM

0

On Feb 5, 2008, at 8:05 AM, Adam Akhtar 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??

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://agileconsult...
Rob@AgileConsultingLLC.com



Leslie Viljoen

2/5/2008 1:25:00 PM

0

On Feb 5, 2008 3:14 PM, Adam Akhtar <adamtemporary@gmail.com> 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"

Ilan Berci

2/5/2008 2:39:00 PM

0

Adam Akhtar 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"

:)

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

hth helps
--
Posted via http://www.ruby-....

Raju Gandhi

2/5/2008 2:56:00 PM

0

[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

On Feb 5, 2008 9:39 AM, Ilan Berci <coder68@yahoo.com> wrote:

> Adam Akhtar 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"
>
> :)
>
> Best way is to just look at the string methods.. String.methods -
> Object.instance_methods
>
> hth helps
> --
> Posted via http://www.ruby-....
>
>


--
Raju

Adam Akhtar

2/5/2008 3:26:00 PM

0

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!




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

James Gray

2/5/2008 3:44:00 PM

0

On Feb 5, 2008, at 9:26 AM, Adam Akhtar 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 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/q...

http://www.rubyquiz.com/q...

http://www.rubyquiz.com/q...

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

James Edward Gray II


Thomas Wheat (35), 1131 Evans Dr., Santa Rosa, CA 95405, 707-542-2288

6/16/2012 11:48:00 PM

0

On Sat, 16 Jun 2012 22:13:18 -0400, Leon Manfredi
<mandrel@fastmail.fm> wrote:

>On 6/16/2012 6:46 PM, bustyourface@rocketmail.com wrote:
>> On Fri, 15 Jun 2012 22:57:07 -0400, Leon Manfredi
>> <mandrel@fastmail.fm> wrote:
>>
>>>>
>>> CURIOUS...... DID HE HAVE AN EARLIER WIFE....... WIVES.......????
>>
>> Why don't you go find out???????????
>>
>> Why haven't you figured out that you live in a deadbeat efficiency
>> apartment on Social Security and that's all you have to show for your
>> whole life BECAUSE YOU'RE A LAZY NIGGER?????????????
>>
>> HAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!
>>
>> "Curious......"????????
>>
>> Nope, just LAZY!!!!!!!!!!!!!!!!!!!!!!
>>
>> HAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!
>> HAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!
>> HAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!
>>
>OOOPS...... !!!!!!!!! DON'T SEEM TO FIT ANY OF THOSE, BUT IF I WERE OF
>COLOR,
>I WOULDN'T DENY, I'D BE PROUD................. BUT AS AN ADDED NOT, IT
>PLACES
>YOU IN THE SENSE 3 CATEGORY BELOW...
>
>Main Entry:nig?ger
>Pronunciation:*ni-g*r
>Function:noun
>Etymology:alteration of earlier neger, from Middle French negre, from
>Spanish or Portuguese negro, from negro black, from Latin niger
>Date:1786
>
>1 usually offensive; see usage paragraph below : a black person
>2 usually offensive; see usage paragraph below : a member of any
>dark-skinned race
>3 : a member of a socially disadvantaged class of persons *it's time
>for somebody to lead all of America's niggersTall the people who feel
>left out of the political process ? Ron Dellums*
>usage Nigger in senses 1 and 2 can be found in the works of such writers
>of the past as Joseph Conrad, Mark Twain, and Charles Dickens, but it
>now ranks as perhaps the most offensive and inflammatory racial slur in
>English. Its use by and among blacks is not always intended or taken as
>offensive, but, except in sense 3, it is otherwise a word expressive of
>racial hatred and bigotry.
>
>
>
>
>
>
>
>
>
>
>