Advertisement
djangopdx

ransom.py

Aug 11th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/bin/python
  2. # Ransom note
  3. # The code will take the following input from STDIN
  4. # First line, length of the strings input
  5. # Second Line is the string representing the text of the words in the magazine to be used for creating a ransom note
  6. # Third line is the string representing the text of the words in the ransom note
  7. # Program will print Yes if the ransom note can be created from the given magazine, and No otherwise.
  8.  
  9.  
  10. def ransom_note(magazine, ransom):
  11.     magazine_dict = {}
  12.     for word in magazine:
  13.         if word not in magazine_dict:
  14.             magazine_dict[word] = magazine.count(word)
  15.     ransom_dict = {}
  16.     for word in ransom:
  17.         if word not in ransom_dict:
  18.             ransom_dict[word] = ransom.count(word)
  19.     ransom_set = set(ransom_dict.keys())
  20.     magazine_set = set(magazine_dict.keys())
  21.     if not ransom_set.issubset(magazine_set):
  22.         return False
  23.     else:
  24.         for word in ransom_set:
  25.             if ransom_dict[word] > magazine_dict[word]:
  26.                 return False
  27.         return True
  28.            
  29.    
  30.  
  31. m, n = map(int, raw_input().strip().split(' '))
  32. magazine = raw_input().strip().split(' ')
  33. ransom = raw_input().strip().split(' ')
  34. answer = ransom_note(magazine, ransom)
  35. if(answer):
  36.     print "Yes"
  37. else:
  38.     print "No"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement