Euler program (python) problem 22

Source: Internet
Author: User

Names Scoresproblem 22

Using Names.txt (Right click and ' Save link/target as ... '), a 46K text file containing over Five-thousand first names, beg In by sorting it into alphabetical order. Then working out of the alphabetical value for each name, multiply this value by it alphabetical position in the list to OBT Ain a name score.

For example, when the list is sorted to alphabetical order, COLIN, which is worth 3 + + + + 9 + = 938t H name in the list. So, COLIN would obtain a score of 938x53 = 49714.

What's the total of the "scores in the file?"


Go to the "thread for Problem" in the forum.


Python Code:

def func (name):    result=0 for    i in range (0,len (name)):        Result+=ord (Name[i])-ord (' A ') +1    return Resultfor Line in open (' P022_names.txt ', ' R '):    line.strip (' \ n ') line=line.replace (R ' "', ') lst=line.split (', ') lst=sorted (LST) result=0for I in range (0,len (LST)):    Result+=func (Lst[i]) * (i+1) print (result)

Time: <1s

Euler program (python) problem 22

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.