Re: how do I factor a number down to one digit?

2006-03-01 Thread Tim Roberts
"Allan" <[EMAIL PROTECTED]> 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, [EMAIL PROTECTED]
  Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how do I factor a number down to one digit?

2006-02-27 Thread johnzenger
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how do I factor a number down to one digit?

2006-02-27 Thread bearophileHUGS
>Sounds like homework to me.

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

Bye,
bearophile

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how do I factor a number down to one digit?

2006-02-27 Thread Steven D'Aprano
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how do I factor a number down to one digit?

2006-02-27 Thread bearophileHUGS
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

-- 
http://mail.python.org/mailman/listinfo/python-list


how do I factor a number down to one digit?

2006-02-27 Thread Allan
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.

-- 
http://mail.python.org/mailman/listinfo/python-list