Help me with this Numerology code please...

W

Web Reservoir

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_nameNumberdetails.htm

and

http://www.numerology.googlepages.com/Numerology_nameNumber.htm

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

Thanks
 
S

Sebastian Hungerecker

Web said:
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.
=> [82, 85, 66, 89]
( unpack("C*") gives you the ASCII value of each character in the string)
=> 65
(The ASCII value of A is 65)
=> [17, 20, 1, 24]
=> [8, 2, 1, 6]
=> [9, 3, 2, 7]
=> 21
=> ["2", "1"]
1}.to_s.split(//).inject(0) {|s,x| s + x.to_i}
=> 3

HTH,
Sebastian
 
J

jazzez ravi

Web said:
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_nameNumberdetails.htm

and

http://www.numerology.googlepages.com/Numerology_nameNumber.htm

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.wordpress.com
 
W

Web Reservoir

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
 
W

Web Reservoir

jazzez said:
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
 
J

jazzez ravi

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.wordpress.com
 
K

Karl-Heinz Wild

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
 
D

David A. Black

Hi --

Web said:
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_nameNumberdetails.htm

and

http://www.numerology.googlepages.com/Numerology_nameNumber.htm

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
 
S

Shadowfirebird

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.net/ruby/

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



Hi --

Web said:
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_nameNumberdetails.htm

and

http://www.numerology.googlepages.com/Numerology_nameNumber.htm

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.rubypal.com 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
 
D

Dave Bass

Karl-Heinz Wild said:
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.
 
A

Adam Shelly

Obviously the harder to understand, the better.

There's also the small issue it gives inconsistent results.

irb(main):022:0> Name='I'
=> "I"
irb(main):023:0> Name.unpack("C*").inject(0) {|s,x| s + (x-65)%9 +
irb(main):024:1* 1}.to_s.split(//).inject(0) {|s,x| s + x.to_i}
=> 9
irb(main):025:0> Name.unpack("C*").inject(0) { |s,x| s + (x-64)%9 }%9
=> 0

And neither of these handle the case where you have to go through more
than two reductions.

The OP seems to be just learning ruby, so let's make this a learning exercise by
turning the manual process directly into code.
-Adam
---------------Numberology.rb------------------------------------
# first make a table to hold the number for every letter
number_of = {}
i = 1 #starting with 1...
('A'..'Z').each {|letter| #go through all the letters...
number_of[letter] = i #and assign a number
i+=1 #increase the number for the next letter
i=1 if i>9 #go back to 1 after 9
}
i=0 #do the same thing for ....
('0'..'9').each{|digit| #the text representation of each number
number_of[digit] = i
i+=1
}

namenumber=0 #make a variable to store the namenumber
puts "enter a name" #ask for...
name = gets().chomp #and get a name (chomp off the newline char)
name.upcase! #convert it to uppercase to match the table
letters = name.split('') #split it into an array of individual letters

#convert to numbers:
numbers = letters.map{ |letter| #'map' each letter to its number
number_of[letter] #using the table we made
} #now we have an array of numbers

#we may have to do the next part more than once, so start a loop
loop do
sum = 0 #start with 0
numbers.each{ |number| #for each number in the array...
sum+= number #add it to the sum
}
if sum <=9 #if the sum is one digit...
namenumber = sum # it is our final number
break # so break out of the loop
else #otherwise
digits = sum.to_s.split('') # split the number into single digits
numbers = digits.map{|digit| # map each text digit
number_of[digit] #to it's actual number,
} #updating the array of numbers
end
end #end of loop, go back to loop start

puts "The number of #{name} is #{namenumber}" #show result
 
T

Thomas Preymesser

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

2008/7/31 Web Reservoir said:
Hi,

I am planning to implement a Numerology code like this.

class String
def to_numer
n=1
t = {}
'A'.upto('Z') do |letter|
t[letter] = n
n += 1
n = 1 if n > 9
end
sum = 0
for i in 0...length
sum += t[upcase[i,1]] if t.has_key? upcase[i,1]
end
while sum > 9
s = sum.to_s
sum = 0
for i in 0...s.length
sum += s[i,1].to_i
end
end
sum
end
end

puts "RUBY".to_numer
 
T

Todd Benson

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 )

golfing solution...

'HELLO'.unpack('C*').inject(0){|s, b|s+(b-65)%9}

Todd
 
T

Todd Benson

I get 20 for that, but every result should be a single digit.

So 'IRREVERSIBLE' should result in 4, I suppose. In any case, I
missed the part about adding the individual numerological digits
together.

I guess I should have checked wikipedia.

thx for correction,
Todd
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top