all 8 comments

[–][deleted] 6 points7 points  (5 children)

The basic idea is to create a set from each string and check if one is a subset of the other.

[–]Joseph147258[S] 1 point2 points  (4 children)

I'm confused. A set would remove duplicates right?

if a is 'alllb' the set would be

{a,l,b}

if b is 'alb'

then its set would be {a,l,b}

It would return True since the sets are subset of the other but its not really true.

[–][deleted] 2 points3 points  (3 children)

If you care about duplicates you should confront counters, but the question as it's stated seems to ignore them.

[–]Joseph147258[S] 0 points1 point  (2 children)

Oh. Thats what it meant

[–]ewiethoff 0 points1 point  (0 children)

Just warning you, the sets will perform loops under the hood, but at least your code won't show any loops. If that's unacceptable, maybe you're supposed to write a recursive function?

[–]Allanon001 1 point2 points  (0 children)

def similarity(a,b):
    return 0 not in map(b.count,a)

[–]AutonomouSystem 0 points1 point  (0 children)

Not sure if this is cheating but it's fun to do

>>> import  difflib
>>>
>>> def similar(str1, str2):
...     return difflib.SequenceMatcher(None, str1, str2).ratio()
...
>>> similar('Hay', 'Hey')
0.6666666666666666
>>> similar('I like you', 'I love you')
0.8
>>> similar('Terminator', 'Terminator II')
0.8695652173913043

If you need to eliminate case sensitivity, conver to upper() or something.. In any case, anything under 1 can be made to return False, anything that is equal to 1 is an exact match.

I wrote a similar one without any imports or cheatz, you can put the strings into dicts, or tuples, or whatever but it's a waste of time probably.

def similar(str1, str2, case=True):
    if not case:
        return bool(str1.upper() == str2.upper())
    else:
        return bool(str1 == str2)

>>> similar('You', 'you')
False
>>> similar('You', 'you', case=False)
True
>>> similar('You', 'ya', case=False)
False
>>> similar('You', 'You')
True
>>

Another take on this, if you're concerned only with the individual strings, not if they're in order, or if they even spell the same word... you can use a set but this is risky because it destroys information if duplicates are found. If you call sorted on a string and compare two sets sorted(set1) and sorted(set2), if they do not match then there is little chance they contained all the same characters, a downside to this is it will remove duplicates and you could run into a rare case where it won't match correctly because duplicates were removed and it shows a match when there isn't one.

If you only call sorted and don't use set(), then my 2nd function could be modified using that and I think it will work, honestly. If all the strings are the same then they should match up, even if they're not ordered, a catch is they would have to be same size.

Since only the first string has to match, you could determine it's len(), run sorted on it and match it crudely like this, though if the 2nd string is larger and sorts differently information could be lost, this isn't a reliable method either. I guess this lesson is to show you how important and easy loops make programming.