A challenge to write and get the o/p in a single line

September 21, 2010 at 3:16 am (Python)

<pedro3005>we can say that each letter has a number equivalent. a = 1, b = 2, etc. Write a program that takes in a name and outputs the sum of all numbers of the equivalent letters, in one line, not counting the “import” lines

<pedro3005> It must ignore spaces and be case-insensitive

<pedro3005> Mohan_chml, like “pedro” must be 58

I cant even think about the way how to do it in a single line. Later he told me the codes

import srting
print sum([string.ascii_lowercase.index(x) + 1 for x in raw_input("> ").lower().replace(" ", "") if x in string.ascii_lowercase])

man That’s awesome

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: