We have:

is_merge('localhost', 'local', 'host') True is_merge('localhost', 'lacol', 'tosh') True is_merge('localhost', 'loc', 'hos') False 

According to this condition, we have to do the Merge strings: I tried to concatenate, but I just need to assemble a string of letters.

 def is_merge(s, part1, part2): if (part1+ part2 == s): return True else: return False 

How to assemble a string and match a string obtained from part1 and part2 and match it with the original string s, provided that the string can contain spaces.

  • Specify, you need to check that the string in the first argument consists of the same letters (and in the same quantity) as the 2nd and 3rd arguments together? - insolor
  • Those. check without spaces? Spaces can be in any line or only in the first? - insolor
  • In letters, that is, I can type 'loca l' & 'hos t' - Basil Jimmy

1 answer 1

If you paraphrase the condition as "check that the first line consists of the same letters as the 2 and 3 lines together," you can decide, for example, by sorting the letters:

 def is_merge(s, part1, part2): return sorted(part1 + part2) == sorted(s) 

Other solutions are possible, for example, count how many letters are in each line and compare:

 from collections import Counter def is_merge(s, part1, part2): return Counter(part1) + Counter(part2) == Counter(s) 

Update. If you want a comparison without taking into account the spaces, you can filter everything from the lines except for the letters before the comparison:

 def is_merge(s, part1, part2): s, part1, part2 = (list(filter(str.isalpha, item)) for item in (s, part1, part2)) return sorted(part1 + part2) == sorted(s) 

For the second variant it is the same (and it is not necessary to convert the iterator that returned the filter to the list):

 from collections import Counter def is_merge(s, part1, part2): s, part1, part2 = (filter(str.isalpha, item) for item in (s, part1, part2)) return Counter(part1) + Counter(part2) == Counter(s) 
  • The part that normalizes the strings for comparison can be made as a parameter: is_merge = lambda s, *parts, normalized=sorted: normalized(s) == normalized(''.join(parts)) Then you just need to correct the definition of normalized , not touching the is_merge code if the format of valid input lines changes, for example: normalized = lambda s: Counter(filter(str.isalpha, s.casefold())) - jfs