[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Help me with this Numerology code please...

Web Reservoir

7/31/2008 10:07:00 AM

Hi,

I am planning to implement a Numerology code like this.

Every alphabet in Numerology is given a number like this...

--------------------------------------------------------------
1 2 3 4 5 6 7 8 9

A B C D E F G H I

J K L M N O P Q R

S T U V W X Y Z
----------------------------------------------------------

Or you can even say that

1 = A, J, S
2 = B, K, T
3 = C, L, U
4 = D, M, V
5 = E, N, W
6 = F, O, X
7 = G, P, Y
8 = H, Q, Z
9 = I, R

Now when you type RUBY you should get the total as 9+3+2+7 = (21) 2+1 =
3
where R = 9, U = 3, B = 2 and y = 7 ( as shown above )

Have a look at the working example here...

http://www.numerology.googlepages.com/Numerology_nameNumberd...

and

http://www.numerology.googlepages.com/Numerology_name...

I would like to code this in Ruby. Pl. help me with this code.

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

16 Answers

Sebastian Hungerecker

7/31/2008 10:24:00 AM

0

Web Reservoir wrote:
> Now when you type RUBY you should get the total as 9+3+2+7 = (21) 2+1 =
> 3
> where R = 9, U = 3, B = 2 and y = 7 ( as shown above )
> ...
> I would like to code this in Ruby. Pl. help me with this code.

>> "RUBY".unpack("C*")
=> [82, 85, 66, 89]
( unpack("C*") gives you the ASCII value of each character in the string)

>> ?A
=> 65
(The ASCII value of A is 65)

>> "RUBY".unpack("C*").map {|x| x - ?A}
=> [17, 20, 1, 24]

>> "RUBY".unpack("C*").map {|x| (x-65)%9}
=> [8, 2, 1, 6]

>> "RUBY".unpack("C*").map {|x| (x-65)%9 + 1}
=> [9, 3, 2, 7]

>> "RUBY".unpack("C*").inject(0) {|s,x| s + (x-65)%9 + 1}
=> 21

>> "RUBY".unpack("C*").inject(0) {|s,x| s + (x-65)%9 + 1}.to_s.split(//)
=> ["2", "1"]

>> "RUBY".unpack("C*").inject(0) {|s,x| s + (x-65)%9 +
1}.to_s.split(//).inject(0) {|s,x| s + x.to_i}
=> 3

HTH,
Sebastian
--
NP: Skillet - The Last Night
Jabber: sepp2k@jabber.org
ICQ: 205544826

Raveendran Jazzez

7/31/2008 11:41:00 AM

0

Web Reservoir wrote:
> Hi,
>
> I am planning to implement a Numerology code like this.
>
> Every alphabet in Numerology is given a number like this...
>
> --------------------------------------------------------------
> 1 2 3 4 5 6 7 8 9
>
> A B C D E F G H I
>
> J K L M N O P Q R
>
> S T U V W X Y Z
> ----------------------------------------------------------
>
> Or you can even say that
>
> 1 = A, J, S
> 2 = B, K, T
> 3 = C, L, U
> 4 = D, M, V
> 5 = E, N, W
> 6 = F, O, X
> 7 = G, P, Y
> 8 = H, Q, Z
> 9 = I, R
>
> Now when you type RUBY you should get the total as 9+3+2+7 = (21) 2+1 =
> 3
> where R = 9, U = 3, B = 2 and y = 7 ( as shown above )
>
> Have a look at the working example here...
>
> http://www.numerology.googlepages.com/Numerology_nameNumberd...
>
> and
>
> http://www.numerology.googlepages.com/Numerology_name...
>
> I would like to code this in Ruby. Pl. help me with this code.
>

Hi Reservoir,

i think this code satisfy your need...


class Reservoir
def main

a="mahalingam" # your name

val={"a"=>1,"j"=>1,"s"=>1,
"b"=>2,"k"=>2,"t"=>2,
"c"=>3,"l"=>3,"u"=>3,
"d"=>4,"m"=>4,"v"=>4,
"e"=>5,"n"=>5,"w"=>5,
"f"=>6,"o"=>6,"x"=>6,
"g"=>7,"p"=>7,"y"=>7,
"h"=>8,"q"=>8,"z"=>8,
"i"=>9,"r"=>9}


num=[]
i=0
while i < a.length do
num << val[a[i,1]]
i+=1
end
mind(num)
end


def mind(num)
no=[]
no=num
j=0

final=0
while j < no.length
final +=num[j].to_i
j+=1
end
output= final.to_s
if output.length !=1
output2=output.split(//)
mind(output2)
else
puts final
end
end
end

res=Reservoir.new
res.main


Regards,
P.Raveendran
RF,Chennai
http://raveendran.wor...

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

Web Reservoir

7/31/2008 11:54:00 AM

0

Sebastian Hungerecker wrote:
> Web Reservoir wrote:
>> Now when you type RUBY you should get the total as 9+3+2+7 = (21) 2+1 =
>> 3
Hi Sebastian,

Thanks a lot for your helping attitude. Unfortunately its too tough for
me to understand yiour code at this stage. May be after more homework,
perhaps i will be able to do it.

Meantime, i am looking at the other code given below.

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

Web Reservoir

7/31/2008 12:02:00 PM

0

jazzez ravi wrote:

> Hi Reservoir,
>
> i think this code satisfy your need...

>
> a="mahalingam" # your name

Hi Raveendran,

I will be getting most of the details from the web only.
i will be using like this..

myvariable = gets() # can be any name

I cannot depend on any particular name. The name RUBY was just an
example as a work around.

Pl. try the link, which i have given in my first thread. I would like to
work that way.

I am thankful to You and Sebastian for your help. Can you be more
specific, on how to run this code with the gets() solution.

I am trying to understand your code, but i have not understood it
completely. may be due to my less knowledge.

I hope i have made my point clear.

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

Raveendran Jazzez

7/31/2008 12:13:00 PM

0

Hi Reservior,

trail.rb

Replace the line

a="mahalingam" # your name

Add
puts "Please Enter the name"
b=gets()
a=b.downcase!

Now run the program via command prompt:

EXACT_PATH> ruby trail.rb


Regards,
P.Raveendran
RF,Chennai
http://raveendran.wor...

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

Karl-Heinz Wild

7/31/2008 12:41:00 PM

0



Karl-Heinz Wild

7/31/2008 12:44:00 PM

0

On 31.07.2008, at 12:23, Sebastian Hungerecker wrote:

> Web Reservoir wrote:
>> Now when you type RUBY you should get the total as 9+3+2+7 = (21)
>> 2+1 =
>> 3
>> where R = 9, U = 3, B = 2 and y = 7 ( as shown above )
>> ...
>> I would like to code this in Ruby. Pl. help me with this code.
>
>
>>> "RUBY".unpack("C*").inject(0) {|s,x| s + (x-65)%9 +
> 1}.to_s.split(//).inject(0) {|s,x| s + x.to_i}
> => 3

Nice code!

"RUBY".unpack("C*").inject(0) { |s,x| s + (x-64)%9 }%9
=> 3

whouldn't that be better?

regards
Karl-Heinz




David A. Black

7/31/2008 12:46:00 PM

0

Hi --

On Thu, 31 Jul 2008, jazzez ravi wrote:

> Web Reservoir wrote:
>> Hi,
>>
>> I am planning to implement a Numerology code like this.
>>
>> Every alphabet in Numerology is given a number like this...
>>
>> --------------------------------------------------------------
>> 1 2 3 4 5 6 7 8 9
>>
>> A B C D E F G H I
>>
>> J K L M N O P Q R
>>
>> S T U V W X Y Z
>> ----------------------------------------------------------
>>
>> Or you can even say that
>>
>> 1 = A, J, S
>> 2 = B, K, T
>> 3 = C, L, U
>> 4 = D, M, V
>> 5 = E, N, W
>> 6 = F, O, X
>> 7 = G, P, Y
>> 8 = H, Q, Z
>> 9 = I, R
>>
>> Now when you type RUBY you should get the total as 9+3+2+7 = (21) 2+1 =
>> 3
>> where R = 9, U = 3, B = 2 and y = 7 ( as shown above )
>>
>> Have a look at the working example here...
>>
>> http://www.numerology.googlepages.com/Numerology_nameNumberd...
>>
>> and
>>
>> http://www.numerology.googlepages.com/Numerology_name...
>>
>> I would like to code this in Ruby. Pl. help me with this code.
>>
>
> Hi Reservoir,
>
> i think this code satisfy your need...
>
>
> class Reservoir
> def main
>
> a="mahalingam" # your name
>
> val={"a"=>1,"j"=>1,"s"=>1,
> "b"=>2,"k"=>2,"t"=>2,
> "c"=>3,"l"=>3,"u"=>3,
> "d"=>4,"m"=>4,"v"=>4,
> "e"=>5,"n"=>5,"w"=>5,
> "f"=>6,"o"=>6,"x"=>6,
> "g"=>7,"p"=>7,"y"=>7,
> "h"=>8,"q"=>8,"z"=>8,
> "i"=>9,"r"=>9}
>
>
> num=[]
> i=0
> while i < a.length do
> num << val[a[i,1]]
> i+=1
> end
> mind(num)
> end
>
>
> def mind(num)
> no=[]
> no=num
> j=0
>
> final=0
> while j < no.length
> final +=num[j].to_i
> j+=1
> end
> output= final.to_s
> if output.length !=1
> output2=output.split(//)
> mind(output2)
> else
> puts final
> end
> end
> end
>
> res=Reservoir.new
> res.main

You're working waaaaay too hard :-) Let Ruby do the heavy lifting.

class Reservoir

# This technique is a little odd at first, but it's actually a
# pretty common idiom.
MAP = Hash[*('a'..'z').zip(1..26).flatten]

# Convert "abc" or 123 to [1,2,3]
# This is fairly terse code, but it's just a utility method that
# will be called from higher-level logic.
def numbers_from(obj)
obj.to_s.split(//).map {|letter| MAP[letter] || letter.to_i }
end

# The classic Ruby way to add up an array. See Array#sum in
# ActiveSupport too.
def add_up(numbers)
numbers.inject(0) {|acc,n| acc + n }
end

def numberize(object)
final = add_up(numbers_from(object))
if final > 9
numberize(final)
else
final
end
end
end

r = Reservoir.new
p r.numberize("david")
p r.numberize(1234) # extra functionality :-)

The inclusion of numberizing numbers may or may not be desired, but if
not, it should be pretty easy to get rid of.


David

--
Rails training from David A. Black and Ruby Power and Light:
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.r... for details and updates!

Shadowfirebird

7/31/2008 2:50:00 PM

0

I think the original poster may be new to coding, in which case, can I
recommend the first couple of chapters of "_Why's Poignant Guide to
Ruby"? People tend to either find it really useful, or hate it, I
think. http://poignantguide...

At the risk of adding even more meat to the pot:

def numerologise(name)
# turn name into an array of integers
arr = name.upcase.unpack("C*").map{|x| (x - ?A) % 9 + 1}

# keep going until there's only one integer in the array
until (arr.size == 1)
# add up all the integers
tot = arr.inject{|c,x| c + x}

# split the digits to make a new array
arr = tot.to_s.split(//).map{|y| y.to_i}
end

return arr
end

puts numerologise("ruby")

(This is basically Sebastian's original answer, although perhaps it's
easier to understand like this.)



On Thu, Jul 31, 2008 at 1:45 PM, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
> On Thu, 31 Jul 2008, jazzez ravi wrote:
>
>> Web Reservoir wrote:
>>>
>>> Hi,
>>>
>>> I am planning to implement a Numerology code like this.
>>>
>>> Every alphabet in Numerology is given a number like this...
>>>
>>> --------------------------------------------------------------
>>> 1 2 3 4 5 6 7 8 9
>>>
>>> A B C D E F G H I
>>>
>>> J K L M N O P Q R
>>>
>>> S T U V W X Y Z
>>> ----------------------------------------------------------
>>>
>>> Or you can even say that
>>>
>>> 1 = A, J, S
>>> 2 = B, K, T
>>> 3 = C, L, U
>>> 4 = D, M, V
>>> 5 = E, N, W
>>> 6 = F, O, X
>>> 7 = G, P, Y
>>> 8 = H, Q, Z
>>> 9 = I, R
>>>
>>> Now when you type RUBY you should get the total as 9+3+2+7 = (21) 2+1 =
>>> 3
>>> where R = 9, U = 3, B = 2 and y = 7 ( as shown above )
>>>
>>> Have a look at the working example here...
>>>
>>> http://www.numerology.googlepages.com/Numerology_nameNumberd...
>>>
>>> and
>>>
>>> http://www.numerology.googlepages.com/Numerology_name...
>>>
>>> I would like to code this in Ruby. Pl. help me with this code.
>>>
>>
>> Hi Reservoir,
>>
>> i think this code satisfy your need...
>>
>>
>> class Reservoir
>> def main
>>
>> a="mahalingam" # your name
>>
>> val={"a"=>1,"j"=>1,"s"=>1,
>> "b"=>2,"k"=>2,"t"=>2,
>> "c"=>3,"l"=>3,"u"=>3,
>> "d"=>4,"m"=>4,"v"=>4,
>> "e"=>5,"n"=>5,"w"=>5,
>> "f"=>6,"o"=>6,"x"=>6,
>> "g"=>7,"p"=>7,"y"=>7,
>> "h"=>8,"q"=>8,"z"=>8,
>> "i"=>9,"r"=>9}
>>
>>
>> num=[]
>> i=0
>> while i < a.length do
>> num << val[a[i,1]]
>> i+=1
>> end
>> mind(num)
>> end
>>
>>
>> def mind(num)
>> no=[]
>> no=num
>> j=0
>>
>> final=0
>> while j < no.length
>> final +=num[j].to_i
>> j+=1
>> end
>> output= final.to_s
>> if output.length !=1
>> output2=output.split(//)
>> mind(output2)
>> else
>> puts final
>> end
>> end
>> end
>>
>> res=Reservoir.new
>> res.main
>
> You're working waaaaay too hard :-) Let Ruby do the heavy lifting.
>
> class Reservoir
>
> # This technique is a little odd at first, but it's actually a
> # pretty common idiom.
> MAP = Hash[*('a'..'z').zip(1..26).flatten]
>
> # Convert "abc" or 123 to [1,2,3]
> # This is fairly terse code, but it's just a utility method that
> # will be called from higher-level logic.
> def numbers_from(obj)
> obj.to_s.split(//).map {|letter| MAP[letter] || letter.to_i }
> end
>
> # The classic Ruby way to add up an array. See Array#sum in
> # ActiveSupport too.
> def add_up(numbers)
> numbers.inject(0) {|acc,n| acc + n }
> end
>
> def numberize(object)
> final = add_up(numbers_from(object))
> if final > 9
> numberize(final)
> else
> final
> end
> end
> end
>
> r = Reservoir.new
> p r.numberize("david")
> p r.numberize(1234) # extra functionality :-)
>
> The inclusion of numberizing numbers may or may not be desired, but if
> not, it should be pretty easy to get rid of.
>
>
> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
> * Advancing With Rails August 18-21 Edison, NJ
> * Co-taught by D.A. Black and Erik Kastner
> See http://www.r... for details and updates!
>
>



--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

Dave Bass

7/31/2008 4:39:00 PM

0

Karl-Heinz Wild wrote:
> On 31.07.2008, at 12:23, Sebastian Hungerecker wrote:
>
>> 1}.to_s.split(//).inject(0) {|s,x| s + x.to_i}
>> => 3
>
> Nice code!
>
> "RUBY".unpack("C*").inject(0) { |s,x| s + (x-64)%9 }%9
> => 3
>
> whouldn't that be better?

Obviously the harder to understand, the better.

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