URI Online Judge Solution 1257 Array Hash Using Python Programming Language.
You will be given many input lines with strings. The value of each character in input is computed as follows:
Value = (Alphabet Position) + (Element of input) + (Position in Element)
DDD
then each character value would be computed as follows:
2 = 2 + 0 + 0 : 'C' in element 0 position 0
2 = 1 + 0 + 1 : 'B' in element 0 position 1
2 = 0 + 0 + 2 : 'A' in element 0 position 2
4 = 3 + 1 + 0 : 'D' in element 1 position 0
5 = 3 + 1 + 1 : 'D' in element 1 position 1
6 = 3 + 1 + 2 : 'D' in element 1 position 2
The final hash would be 2+2+2+4+5+6 = 21.
Input
For each test case print the hash calculated according to the above explanation.
Value = (Alphabet Position) + (Element of input) + (Position in Element)
All positions are 0-based. 'A' has alphabet position 0, 'B' has alphabet position 1, ... The returned hash is the sum of all character values in input. For example, if input is:
CBADDD
then each character value would be computed as follows:
2 = 2 + 0 + 0 : 'C' in element 0 position 0
2 = 1 + 0 + 1 : 'B' in element 0 position 1
2 = 0 + 0 + 2 : 'A' in element 0 position 2
4 = 3 + 1 + 0 : 'D' in element 1 position 0
5 = 3 + 1 + 1 : 'D' in element 1 position 1
6 = 3 + 1 + 2 : 'D' in element 1 position 2
The final hash would be 2+2+2+4+5+6 = 21.
Input
The input contains many test cases. The first line of input contains an integer N that indicates the amount of test cases. Each test case begin with an integer L (1 ≤ L ≤ 100) that indicates the quantity of following lines. Each of these L lines will contains a string with up to 50 uppercase letters ('A' - 'Z').
OutputFor each test case print the hash calculated according to the above explanation.
Sample Input | Sample Output |
5 | 21 |
Solution Using Python:
T = int(input())
Dic = {'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9,'K':10,'L':11,'M':12,'N':13,'O':14,'P':15,'Q':16,'R':17,'S':18,'T':19,'U':20,'V':21,'W':22,'X':23,'Y':24,'Z':25}
while T>0:
V = E = 0
L = int(input())
for i in range(L):
S = list(input())
for j in S:
V = V + Dic[j] + i + E
E+=1
E=0
print(V)
T-=1
Comments
Post a Comment