1

All of my code passes check50 except for the last one. I come up with 5 matches and not 4 (ba, ar, rb, ba, az).

:) helpers.py exists

:) helpers.py compiles

:) detects no lines in common

:) detects one line in common

:) detects multiple lines in common

:) handles duplicate lines in common

:) handles no sentences in common

:) handles one sentence in common

:) handles multiple sentences in common

:) handles sentences with different punctuation

:) handles sentences with punctuation mid-sentence

:) handles duplicate sentences in common

:) handles no substrings in common

:) handles one substring in common

:) handles multiple substrings in common

:) handles substrings when strings are identical

:) handles substring length longer than string length

:( handles duplicate substrings in common Expected 4 matches, not 5

Here is my code:

from nltk.tokenize import sent_tokenize

def lines(a, b): """Return lines in both a and b"""

keep_a = a.splitlines()
keep_b = b.splitlines()
final = []

for z in keep_a:
    for y in keep_b:
        if z == y:
            final.append(z)
            keep_b.remove(y)
            break

return final

def sentences(a, b): """Return sentences in both a and b"""

keep_a = sent_tokenize(a)
keep_b = sent_tokenize(b)
final = []

for z in keep_a:
    for y in keep_b:
        if z == y:
            final.append(z)
            keep_b.remove(y)
            break

return final

def substrings(a, b, n): """Return substrings of length n in both a and b"""

final = []
keep_a = cutter(a, n)
keep_b = cutter(b, n)
print(n)

for z in keep_a:
    for y in keep_b:
        if z == y:
            final.append(y)
            keep_b.remove(y)
            break

return final

def cutter(d, s): """Returns to substrings function the substrings of length n"""

give = []
sub = ""
first = 0
sec = s

while sec <= len(d):
    sub = d[first:sec]
    give.append(sub)
    first = first + 1
    sec = sec + 1

return give

Thanks in advance for your help!

1

With your remove, you only remove the first occurrence of the element, and only from one list. Also, changing the iterable you are iterating is bad style, can lead to unexpected or sometimes even undefined behaviour.

One approach would be to remove duplicates before you enter the nested loops, which can be achieved in many ways.

I used sets, those are unordered lists that can contain any element up to one time (automatically removing duplicates). And they have an intersection operator, no loops needed :) (you'd have to convert back to list before returning, though)

3
  • Thanks for your answer! However, I don't want to remove all duplicates. If there are 2 instances of a substring in both files, I want the program to return that substring twice. Sorry if I'm not understanding your answer correctly. – Rjspareiii Jan 2 '19 at 22:59
  • What does the problem description say of that case? Link to the statement requiring you to return same substring twice, please. Maybe I'm just misremembering that part, but I think it asked for unique sentences/lines/substrings. – Blauelf Jan 2 '19 at 23:01
  • Found it at docs.cs50.net/2019/x/psets/7/similarities/…: Implement substrings in such a way that, given two strings, a and b, and an integer, n, it returns a list of all substrings of length n that are, identically, present in both a and b. The list should not contain any duplicates. – Blauelf Jan 2 '19 at 23:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .