Help me with this Numerology code please

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

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

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

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

Sebastian H. wrote:

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

On 31.07.2008, at 12:23, Sebastian H. 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?

regards
Karl-Heinz

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

Hi –

On Thu, 31 Jul 2008, jazzez ravi wrote:

A B C D E F G H I
3 = C, L, U

val={“a”=>1,“j”=>1,“s”=>1,
num=[]
no=[]
output2=output.split(//)
mind(output2)
else
puts final
end
end
end

res=Reservoir.new
res.main

You’re working waaaaay too hard :slight_smile: 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 :slight_smile:

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

David

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

On Thu, Jul 31, 2008 at 1:45 PM, David A. Black [email protected]
wrote:

Every alphabet in Numerology is given a number like this…

9 = I, R

class Reservoir
“g”=>7,“p”=>7,“y”=>7,
mind(num)
final +=num[j].to_i
end
MAP = Hash[*(‘a’…‘z’).zip(1…26).flatten]
def add_up(numbers)
end
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

Karl-Heinz Wild wrote:

On 31.07.2008, at 12:23, Sebastian H. 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.

On 7/31/08, Dave B. [email protected] wrote:

whouldn’t that be better?

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

From: [email protected] [mailto:[email protected]]

tough for me to understand yiour code at this stage.

May be after more homework, perhaps i will be able to do it.

sometimes, it’s easy if you just code what you think. I assume you read
maps :slight_smile: You are basically, mapping, so just use a map. A linear map of
string is very visible to the eye and head

eg,

M1=“ABCDEFGHIJKLMNOPQRSTUVWXYZ”
M2=“12345678912345678912345678”

class String
def getM2(map)
M2[map.index(self)].chr.to_i
end
def getM2sum(map)
sum=0
each_byte do |c|
sum += c.chr.getM2(map)
end
sum
end
def numerology
getM2sum(M1).to_s.getM2sum(M2)
end
end

p “RUBY”.numerology

3
#=> nil

in essence, i just mapped M1 to M2, and fr there, mapped again the
result using M2 to M2 (mapping to self). In ruby1.9 it becomes simpler
since you do not need the #chr conversion.

hth.
kind regards -botp

2008/7/31 Web R. [email protected]

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

On Thu, Jul 31, 2008 at 5:06 AM, Web R. [email protected]
wrote:

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

Hi –

On Sat, 2 Aug 2008, Todd B. wrote:

A B C D E F G H I
3 = C, L, U

golfing solution…

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

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

David

On Fri, Aug 1, 2008 at 1:55 PM, Todd B. [email protected]

golfing solution…

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

Oh, I didn’t see Sebastian’s previous solution. I also forgot the +1
after the %9.

Todd

On Fri, Aug 1, 2008 at 2:18 PM, David A. Black [email protected]
wrote:

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

Oh this is so boring, so long codes. I don’t really like long codes. :memo::sleeping:
So 11 years later I have a sweeter approach.

Code :woman_technologist::man_technologist:

#!/usr/bin/ruby -w
hash = 'AJS BKT CLU DMV ENW FOX GPY HQZ IR'.split.zip(1..9)
	.each_with_object({}) { |arg, h| arg[0].each_char { |a| h.merge!(a[0] => arg[1]) } }

puts('ruby'.upcase.each_char.map { |char| hash[char] }.sum.to_s.chars.map(&:to_i).sum)

Note :warning:

If my code is raising NoMethodError for sum, you might be using an older version of Ruby. In that case, just replace the sum with reduce(:+)


Now, instead ‘ruby’, try STDIN.gets or whatever.

Does this answer your question?