473,513 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do I factor a number down to one digit?

I'm trying to write a numerology program where I have each letter
identified by a numerical value like
a=1
b=2
c=3
as so forth. I then input a name. How do I treat each letter as a
single value? That is, instead of print myname I have to do a print
m+y+n+a+m+e which returns a number. I next want to convert the
resulting two or three digit number to a single digit. Like 123 would
be 1+2+3 returning a 5. I hope this isn't too stupid of a question.

Feb 27 '06 #1
5 1209
Allan>I hope this isn't too stupid of a question.

It's a simple problem, but it's not a stupid question, this is a
possible solution:

data = "myname"
radix = str(sum(ord(c)-96 for c in data))
while len(radix) > 1:
radix = str(sum(int(c) for c in radix))
print "The radix of:\n", data, "\n\nIs:\n", radix

Bye,
bearophile

Feb 27 '06 #2
On Mon, 27 Feb 2006 02:31:42 -0800, Allan wrote:
I'm trying to write a numerology program where I have each letter
identified by a numerical value like
a=1
b=2
c=3
as so forth. I then input a name. How do I treat each letter as a
single value? That is, instead of print myname I have to do a print
m+y+n+a+m+e which returns a number. I next want to convert the
resulting two or three digit number to a single digit. Like 123 would
be 1+2+3 returning a 5. I hope this isn't too stupid of a question.


Sounds like homework to me.

Here is a hint:

myname = "steven"
for c in myname:
do_something_with(c)

does some work with each individual letter of myname.

You want something that takes 'a', and gives 1, 'b' which gives 2, 'c'
which gives 3, and so forth. In other words, you want to map letters of
the alphabet to numbers. Hint: experiment with dictionaries.

sum = 0
myname = "steven"
for c in myname:
sum = sum + map_letter_to_number_somehow

That's the first half of the problem. Chew on that for a while, and if you
still need help, come back and ask.
--
Steven.

Feb 27 '06 #3
>Sounds like homework to me.

Sorry Steven, you may be right, next time I'll be more careful.

Bye,
bearophile

Feb 27 '06 #4
Your tools are:

1. "map" lets you apply a function to every element of a list.
Strings are lists. (List comprehensions let you do the same thing, but
"map" is better to use if you are turning in homework).
2. "sum" lets you calculate the sum of all numbers in a list.
3. "val" and "str" let you turn strings into numbers and numbers into
strings

The only missing piece is a function that turns a letter into a number.
It shouldn't be that hard to write one.

Feb 27 '06 #5
"Allan" <al***********@hotmail.com> wrote:

as so forth. I then input a name. How do I treat each letter as a
single value? That is, instead of print myname I have to do a print
m+y+n+a+m+e which returns a number. I next want to convert the
resulting two or three digit number to a single digit. Like 123 would
be 1+2+3 returning a 5.


Hmm, in most of the rational mathematical universes I've visited, 1+2+3
returns 6, not 5.

On the other hand, numerology doesn't really have much of a place in a
rational mathematical universe.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Mar 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1716
by: jack | last post by:
Hello, I need to get the time down the the 0.1 second in a number string. Example 09-06-03 13:45 23.4 Seconds To "0906031345234"
17
5060
by: John A Grandy | last post by:
For a Hashtable that is expected to contain no more than 100 DictionaryEntry elements , what is the best load factor ? ( This Hashtable is a encapsulted in a class , an instance of which is cached and methods on which are called by n threads ( n is approx = 25 ) )
17
7660
by: rhitz1218 | last post by:
Hi, I'm trying to create a function that will sort a number's digits from highest to lowest. For example 1000 - will become 0001 or 1234 to 4321
14
7296
by: Nathan Pinno | last post by:
How do I factor a number? I mean how do I translate x! into proper Python code, so that it will always do the correct math? Thanks in advance, Nathan P.
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.