Advertisement
djangopdx

anagram_challenge

Aug 10th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. def dict_adder(j_dict,j_char):
  2.     """Adds an element to the dict in pos parameter 1
  3.       from the character that is provided in pos parameter 2"""
  4.     if j_dict.has_key(j_char):
  5.         j_dict[j_char] += 1
  6.     else:
  7.         j_dict[j_char] = 1
  8.  
  9. def number_needed(a, b):
  10.     answer = 0
  11.     a_set = set(a)
  12.     b_set = set(b)
  13.     a_dict, b_dict = {}, {}
  14.     map(lambda x: dict_adder(a_dict,x), a)
  15.     map(lambda x: dict_adder(b_dict,x), b)
  16.     diff_set_a = a_set.difference(b_set)
  17.     diff_set_b = b_set.difference(a_set)
  18.     intersection_set_a = a_set.intersection(b_set)
  19.     if diff_set_a is not None:
  20.         answer += sum([ a_dict[j] for j in diff_set_a])
  21.     if diff_set_b is not None:
  22.         answer += sum([ b_dict[j] for j in diff_set_b])
  23.     for j in intersection_set_a:
  24.         if a_dict[j] > b_dict[j]:
  25.             answer += a_dict[j] - b_dict[j]
  26.         elif a_dict[j] < b_dict[j]:
  27.             answer += b_dict[j] - a_dict[j]
  28.     return answer
  29.  
  30.  
  31.  
  32. a = raw_input().strip()
  33. b = raw_input().strip()
  34.  
  35. print number_needed(a, b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement