55 Matching Words Python3

Back to General discussions forum

Taxometr     2016-06-17 00:25:32
User avatar

Your answer is Wrong =(((

list = input().split()
a = []
for w in list:
    if list.count(w) > 1:
        while w in list:
            list.remove(w)
        a.append(w)
a.sort()
print (' '.join(a))

What's wrong with my code? Что не так с моим кодом?

Matthew Cole     2016-06-20 15:16:30

Can you give an example data set that your solution does not pass?

Taxometr     2016-06-21 02:39:28
User avatar

Your input data: zup mos nyf zeh zux let les dyt ret gos jut rot jac ryk nuh vix jif deq rac zah mof nut zop gyf lef myc mus zuc bas byc geh jut guc zix bos rak zeq gix neh rot byx muk but zyp bip dip nic let nys noq jec lyk jyp gyq biq zup ryc jys bip vex nic zuq mef vyt zic mup jof nyp vuc gus lif zyk moh nat giq gih rux bos jas buc gyq geh jac vuf vyf rux nek gac ruc jik gis voq boh mec map geh ryf vyx bot lis zof zus bit zyq zup dux met ryx vah gac lyt nih gaq jyq bot vyx juh lis vyh bas zih zeh dex lef bep jut las nox dus gyh vic rus muh gah vuh zuc lek jeq noc zuk byq meh zut dex vac meh zix lot roc myq ryx zuf meq zuc lex jat noc ras nuc zos zyf zys ges lis moq jof jet lix bap dut jip not roh met gas get nok mok vut naf mox rop dof lac dif buq muc nif lek zyp res mih meq dep nas bas vuk duh rof nuq gax dop jah vat nis rop naq nap but guh nyc jyx jup zeq lax zof bex nyt ruf rak bys lip ruf res boc rek loq get zic jut jot zis rak zuk jyq mot nux myk ras mit zec rof but nic rat vax vup zaq gox juq zef dys niq jaf joh mup zyt jyq vaq dyq gys mot gis jeh vys nax byc zaq got nak byt jaf nat nip joh nic ret lac geq noc zah jos zux gyc dyx gys nuq jif rux dac ges goh gup nis lox zif beq gap nif loc zaf byq daf jyf nox daf vuh lok vit zec end

Your answer was: bas bip but byc byq daf dex gac geh ges get gis gyq gys jac jaf jif jof joh jut jyq lac lef let lis meq met mup nat nic nif nis noc nox nuq rak ras res ret rof rop rot ruf rux ryx vuh vyx zah zaq zec zeh zeq zic zix zuc zuk zup zux zyp

Expected answer was: bas bip bos bot but byc byq daf dex gac geh ges get gis gyq gys jac jaf jif jof joh jut jyq lac lef lek let lis meh meq met mot mup nat nic nif nis noc nox nuq rak ras res ret rof rop rot ruf rux ryx vuh vyx zah zaq zec zeh zeq zic zix zof zuc zuk zup zux zyp

Taxometr     2016-06-21 02:41:33
User avatar

PS. Problem was solved in another way. (Задачу решил другим способом.)

res = []
for x in set(l):
    if l.count(x) > 1:
        res.append(x)
res.sort()
Blanchot     2019-03-27 12:47:55

Hello everyone!

I've just completed this exercise using python 3.6 but found that the code in my for-loop wasn't behaving as expected... it missed words that it should have matched.

Sorting the input data first allowed me to match my first results with the expected test results... but on my second attempt my code failed again.

I was able to complete the exercise by sorting the input data first AND running my for-loop twice. I would very much appreciate it if some kind soul helped me identify why such kludges were necessary!

Thanks!

data= data.split()
data.sort() #if I don't sort the data first I mysteriously miss some double occurences of words



found= []
for word in data:
  if data.count(word) > 1:
    found.append(word)
    while word in data: data.remove(word) # removes all further occurences of word

#sorting the data first AND running this for-loop twice to make sure!! 
for word in data:
  if data.count(word) > 1:
   found.append(word)
   while word in data: data.remove(word) # removes all further occurences of word

found.sort()
answer_string= ' '.join(found)
print(answer_string)
Rodion (admin)     2019-03-27 13:03:53
User avatar

Hi Friend!

Well, I'm not the guru of Python, but this seems awkward:

for word in data:
    ...
    data.remove(word)

Thus you are trying to modify collection over which you are iterating. I don't think it will work properly (this is somewhat general agreement we do not modify collections used in for ... in loop in any language to avoid unpredictable behavior).

You may alternatively check if the word is already in found than you need not append it again. Or you can use set for found.

(though due to this small bug you should instead have some words more than once, not missing them - but I'm not quite sure)

Blanchot     2019-03-27 13:41:39

Thank you for your quick reply (and for creating this great site)!

Your advice about modifying collections while still iterating through them seems sound. I'll rewrite my code using a set for found and leave out that list.remove() method.

Once again many thanks!

EDIT: responding to your last sentence about having more matches rather than missing them... it's weird but I can definitely confirm that my for-loop missed some matches the first time around!

Please login and solve 5 problems to be able to post at forum