Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [newbie/2.5.1.1] Computing value of a word?

Reply
Thread Tools

[newbie/2.5.1.1] Computing value of a word?

 
 
Gilles Ganault
Guest
Posts: n/a
 
      02-16-2011
Hello,

For a game, I need to go through a wordlist, and for each word,
compute its value, ie. a=1, b=2, etc.

So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.

Before I write the obvious While loop to go through each line in the
input text file, I was wondering if Python didn't already have some
function to perform this type of computation.

Thank you.
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-16-2011
On Wed, Feb 16, 2011 at 1:17 AM, Gilles Ganault <(E-Mail Removed)> wrote:
> Hello,
>
> For a game, I need to go through a wordlist, and for each word,
> compute its value, ie. a=1, b=2, etc.
>
> So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.
>
> Before I write the obvious While loop to go through each line in the
> input text file, I was wondering if Python didn't already have some
> function to perform this type of computation.


A = ord('a') - 1
for line in your_file:
word = line.strip().lower()
score = sum(ord(letter)-A for letter in word)

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Gilles Ganault
Guest
Posts: n/a
 
      02-17-2011
On Wed, 16 Feb 2011 01:27:06 -0800, Chris Rebert <(E-Mail Removed)>
wrote:
>A = ord('a') - 1
>for line in your_file:
> word = line.strip().lower()
> score = sum(ord(letter)-A for letter in word)


Thanks much Chris.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optical Computing: special issue - Natural Computing, Springer optical supercomputing C Programming 0 01-16-2009 12:18 PM
Optical Computing: special issue - Natural Computing, Springer optical supercomputing C Programming 0 12-19-2008 12:22 PM
Computing the width of an unsigned variable from maximum value? Torsten Landschoff VHDL 8 02-22-2007 05:01 PM
Razer Tarantula Gaming Keyboard Review at XYZ Computing at XYZ Computing Silverstrand Front Page News 0 11-01-2006 05:13 PM
computing average value Raider C++ 5 02-27-2006 08:50 AM



Advertisments