How do I write a method, which returns whether the letters in a word
occur in alphabetical order?
Thank you
How do I write a method, which returns whether the letters in a word
occur in alphabetical order?
Thank you
On Sat, Jun 8, 2013 at 4:31 AM, Tario C. [email protected] wrote:
How do I write a method, which returns whether the letters in a word
occur in alphabetical order?
You start by thinking of a good name for the method and the argument.
Once
you have the name you write “def name(arg) end”. Now you think of an
algorithm to solve your problem. Write it between “)” and “end”. Do
some
testing. Done.
robert
Hi Robert,
Is there a reference directory of algorithms? How do I know the
functionality of every algorithm?
Thank you
Hi
good question and similar to a previous one posed here
for a method to count the consonants in a text or a speech
a regular expression was the basis of a solution in that example
Tario C. wrote in post #1111683:
How do I write a method, which returns whether the letters in a word
occur in alphabetical order?
Tip: you can use recursion – check only the first two characters of the
string, then ask your function to check the rest.
(It won’t really be efficient in Ruby, because strings aren’t lists, but
it’d be cute. (On the other hand, splicing a string in Ruby perhaps uses
copy-on-write so it may not be inefficient.))
On Sun, Jun 9, 2013 at 2:10 PM, Albert S. [email protected]
wrote:
Well, let me “correct” myself, just for the record: even if splicing a
string copies their content unconditionally, it wouldn’t be horrible
since (1) our code is intended for "word"s, which are relatively short;
How do you know? That’s just an assumption.
and (2) this is a scripting language and the copying, done in C, is
negligible.
Depends on how often you do it. And, btw. often the expensive part
isn’t
the copying but the memory management (GC bookkeeping and collection).
Cheers
robert
Albert S. wrote in post #1111786:
Tario C. wrote in post #1111683:
How do I write a method, which returns whether the letters in a word
occur in alphabetical order?Tip: you can use recursion – check only the first two characters of the
string, then ask your function to check the rest.(It won’t really be efficient in Ruby, because strings aren’t lists, but
it’d be cute. (On the other hand, splicing a string in Ruby perhaps uses
copy-on-write so it may not be inefficient.))
Well, let me “correct” myself, just for the record: even if splicing a
string copies their content unconditionally, it wouldn’t be horrible
since (1) our code is intended for "word"s, which are relatively short;
and (2) this is a scripting language and the copying, done in C, is
negligible.
BTW (and sorry if it’s somewhat off-topic), Bjarne Stroustrup favors
vectors
over linked lists: Bjarne Stroustrup: Why you should avoid Linked Lists - YouTube
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs