K
K
Kirill Gorelov2015-10-27 16:21:43
PHP
Kirill Gorelov, 2015-10-27 16:21:43

Working with strings. Algorithm?

Guys. Hello.
You need to write a script. Which will delete words from one text file "A" that are found in another text file "B".
I don’t really understand how to make this algorithm and what, let’s say, functions should be used?
But the most important thing is the algorithm!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Kovalsky, 2015-10-27
@dmitryKovalskiy

The solution is to load both files into separate arrays and check element by element A for the presence of an element in B. According to the results of the check, delete / skip. Rewrite the resulting array to file A.

M
Max, 2015-10-27
@MaxDukov

loop over each word from B, and str_replace

D
Daniel Reed, 2015-10-27
@Remasik

Recently, there seemed to be a similar question =) you also asked for a script. Yes?
file 1 (a=222, b=333, c=444, d=333), file 2 (222 333)
Result (c=444, d=333)
Super shitty code, but you get the point, I hope.

l = list()
with open('2.txt') as f:
  for line in f.readlines():
    l.append(line.replace('\n',''))

res = list()
with open('1.txt') as f2:
  for line in f2.readlines():
    res.append(line.replace('\n',''))


res = filter(lambda x: x.split('=')[-1] not in l, res)

with open('12.txt', 'w') as f2:
  for line in res:
    f2.write(line+"\n")

A
airyashov, 2015-10-28
@airyashov

to start words from file 2 sort in descending order of line lengths

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question