thoga31

Challenge 9 (Python)

Aug 13th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. def solveit(file_name = "challenge9_list.txt"):
  2.     f = open(file_name, "r")
  3.     contents = [i[0] for i in f.readlines() if (ord(i[0]) >= ord('a') or ord(i[0]) <= ord('z')) or (ord(i[0]) >= ord('0') or ord(i[0]) <= ord('9'))]
  4.     f.close()
  5.     sumdigit = 0
  6.     alpha = [0 for i in range(26)]
  7.     for i in contents:
  8.         if ord(i) >= ord('0') and ord(i) <= ord('9'):
  9.             sumdigit += int(i)
  10.         else:
  11.             alpha[ord(i) - ord('a')] += 1
  12.     result = "Sum = {0}\n".format(sumdigit)
  13.     for i in range(0,26):
  14.         if alpha[i] != 0:
  15.             result += "'{0}' = {1}\n".format(chr(i + ord('a')), alpha[i])
  16.     return result
  17.  
  18. if __name__ == "__main__":
  19.     print(solveit())
  20.     input()  # pausa
Advertisement
Add Comment
Please, Sign In to add comment