Number of maximal PAIRS-values I had an interview question like this:


  In a company there are different people. One can measure how well they
  suits for pair coding as follows:
  
  First, let us compute the PAIRS-value which is the number of letters
  P, A, I, R, S given in two people names. Then compute the sum of
  consecutive digits and sum of digits if the result is over 9. 
  
  Then do the same for the numbers one got in previous step: sum two
  adjacent numbers and again, compute the sum of digits of every number
  if that number is > 10. Repeat this until you have only two digits,
  which measures the goodness of pairs. For example, "Erkki Esimerkki"
  and "Matti Meikäläinen" has PAIRS-value 58:
  
  First [P = 0, A = 1, I = 6, R = 2, S = 1] and then 

[0 + 1 = 1, 1 + 6 = 7, 6 + 2 = 8, 2 + 1 = 3]
[1 + 7 = 8, 7 + 8 = 15 =&gt; 1 + 5 = 6, 8 + 3 = 11 =&gt; 1 + 1 = 2]
[8 + 6 = 14 =&gt; 1 + 4 = 5, 6 + 2 = 8]
[5, 8] = 58%

  
  Now, there are many worker names given in
  http://reaktor.fi/wp-content/uploads/2014/08/fast_track_generoitu_nimilista.txt
  . Find the number of perfect pairs, i.e. those who have PAIRS-value
  99%.


Is there a faster way to solve as the following code:

import urllib.request

def sum_digits(number):
    while number &gt; 9:
       temp = number
       y1 = number//10
       y2 = temp % 10
       number = y1 + y2
    return number

def compute_percent(pairs):
    x1 = sum_digits(pairs[0] + pairs[1])
    x2 = sum_digits(pairs[1] + pairs[2])
    x3 = sum_digits(pairs[2] + pairs[3])
    x4 = sum_digits(pairs[3] + pairs[4])
    x5 = sum_digits(x1 + x2)
    x6 = sum_digits(x2 + x3)
    x7 = sum_digits(x3 + x4)
    x8 = sum_digits(x5 + x6)
    x9 = sum_digits(x6 + x7)
    return 10*x8+x9

if __name__ == '__main__':
    address1 = 'http://reaktor.fi/wp-content/uploads/2014/08/'
    address2 = 'fast_track_generoitu_nimilista.txt'
    address = address1 + address2
    agents1 = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36' 
    agents2 = '(KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'
    agents = agents1 + agents2

    req = urllib.request.Request(address, headers={'User-Agent': agents})
    r = urllib.request.urlopen(req)
    datas = r.read().decode('utf8')

    name = ""
    names = list()
    pairs = [0] * 5
    matches = list()
    for i in range(0,len(datas)):
        if (datas[i] == "\n"):
            names.append(name)
            name = ""
        else:
            name += datas[i]

    for i in range(0,len(names)):
        for j in range(i + 1,len(names)):
            common = names[i].lower() + names[j].lower()
            for i in range(0,len(common)):
                if (common[i]) == "p":
                    pairs[0] += 1
                if (common[i]) == "a":
                    pairs[1] += 1
                if (common[i]) == "i":
                    pairs[2] += 1
                if (common[i]) == "r":
                    pairs[3] += 1
                if (common[i]) == "s":
                    pairs[4] += 1
                for i in range(0,len(pairs)):
                    pairs[i] = sum_digits(pairs[i])

                percent = compute_percent(pairs)
                if percent == 99:
                    pari = "" + names[i] + " and " + names[j]
                    matches.append(pari)
    matches = list(set(matches))
    print(len(matches))
There is a significant flaw in your code; this line:

pairs = [0] * 5


only occurs once, and I couldn't spot pairs being emptied anywhere.



The code to create the list of names is very inefficient, this:

name = ""
names = list()
...
for i in range(0,len(datas)):
    if (datas[i] == "\n"):
        names.append(name)
        name = ""
    else:
        name += datas[i]


Could be written as:

names = data.split("\n")


Note also that for char in data would have been a more Pythonic loop construct. 



You can use itertools.combinations to get pairs of names:

for name1, name2 in combinations(names, 2):




You can use collections.Counter to simplify getting the initial numbers:

counter = Counter(name1.lower())
counter.update(name2.lower())
pairs = [sum_digits(counter[c]) for c in "pairs"]




Your compute_percent only works when exactly five letters are chosen, you can make it much more general (and shorter):

def compute_percent(pairs):
    while len(pairs) &gt; 2:
        pairs = [sum_digits(n1+n2) for n1, n2 in zip(pairs, pairs[1:])]
    return (10 * pairs[0]) + pairs[1]




I would split the if __name__ == "__main__" section into more, smaller functions, something like:


get_names to extract the names from the file;
perfect_pairs to pair them off; and
score_pair to calculate the score.


Then all you have in the last block is:

print(len(perfect_pairs(get_names())))