Advertisement
Lonely_Wanderer

можно ли собрать вторую строку из первой

May 10th, 2023 (edited)
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.50 KB | None | 0 0
  1. s1 = "Первая строка"
  2. s2 = "строка"
  3.  
  4.  
  5. # Вариант через строки
  6. def magic(s1,s2):
  7.     for i in s2:
  8.         if not (i in s1 and s2.count(i)<=s1.count(i)):
  9.             return False
  10.     return True
  11.  
  12. #Вариант через словари
  13. def magic2(s1,s2):
  14.     m1 = {i:s1.count(i) for i in set(s1)}
  15.     m2 = {i:s2.count(i) for i in set(s2)}
  16.     for i in m2.keys():
  17.         if not (i in m1.keys() and m2[i]<=m1[i]):
  18.             return False
  19.     return True
  20.  
  21. magic(s1,s2)
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement