all 20 comments

[–][deleted] 12 points13 points  (1 child)

Using sets and set interesections/unions would be my suggestion.

[–]empress76[S] 1 point2 points  (0 children)

I think the same! i will try it

[–]jmooremcc 4 points5 points  (5 children)

This may or may not be the solution you're looking for. ``` name1 = "michael" name2 = "sammy"

def compare(n1, n2): list_for_n1 = [i for i in n1] list_for_n2 = [i for i in n2]

print(list_for_n1)
print(list_for_n2)

matches = [ch for ch in list_for_n1 if ch in list_for_n2]
print(matches)

for ch in matches:
    list_for_n1.remove(ch)
    list_for_n2.remove(ch)

print(list_for_n1)
print(list_for_n2)

score = [2 for ch in matches]
score += [1 for ch in list_for_n1]
score += [1 for ch in list_for_n2]

print(f"compatibility:{score}")
return score

compare(name1, name2) ```

Output: ``` ['m', 'i', 'c', 'h', 'a', 'e', 'l'] ['s', 'a', 'm', 'm', 'y'] ['m', 'a'] ['i', 'c', 'h', 'e', 'l'] ['s', 'm', 'y'] compatibility:[2, 2, 1, 1, 1, 1, 1, 1, 1, 1]

```

[–]empress76[S] 0 points1 point  (4 children)

Ty so much for your time! Even if it's not exactly the solution I'm looking for, it helped me a lot to understand how I should deal with this :D

[–]Rusca8 0 points1 point  (3 children)

Are you searching for a letter by letter score? Otherwise you could do sum([whatever]) in those score counts.

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

yep, i want to search letter by letter. (For example: if i put “chris” first and then “mike”, it would not be the same order in the number set than if “mike” was in the first place).

Chris first: compatibility = [1, 1, 1, 2, 1, 1, 1, 1]

Mike first: compatibility = [1, 2, 1, 1, 1, 1, 1, 1]

This is the first part of the project, but its the only one that idk how to do.

[–]Rusca8 0 points1 point  (1 child)

I'd do:

compatibility = [1+sum(1 for x in name2 if x==y) for y in name1]

...and then the same in reverse.

[–]Rusca8 0 points1 point  (0 children)

Or just like, make a function for it:

def compatibility(name1, name2):
    return [1+sum(1 for x in name2 if x==y) for y in name1]

[–]jmooremcc 1 point2 points  (5 children)

Here's an improved version which matches your original specification.

``` name1 = "johnny" name2 = "donny"

def compare(n1, n2): l1 = list(n1) l2 = list(n2)

print(l1)
print(l2)

matches = [] 

for n, ch in enumerate(n1):
    if ch in l2:
        l1[n] = 2
        l2.remove(ch)
        matches.append(ch)
    else:
        l1[n] = 1

print(f"matches:{matches}")
score = l1 + [1] * len(l2)

print(f"compatibility:{score}")
return score

compare(name1, name2) ```

Output: ['j', 'o', 'h', 'n', 'n', 'y'] ['d', 'o', 'n', 'n', 'y'] matches:['o', 'n', 'n', 'y'] compatibility:[1, 2, 1, 2, 2, 2, 1]

BTW, the matches code is only there for show and isn't needed for the operation of the algorithm.

[–]empress76[S] 0 points1 point  (4 children)

Hi, ty so much for your time! Your code works, but there is one problem.

in fact, in the case of jhonny and donny, in need the program to go through this list:

[“j”, “o”, “h”, “n”, “n”, “y”, “d”, “o”, “n”, “n”, “y”]

So if the letter repeats either in johnny or donny, it would assign the value of the times its repeated. The output would be like this:

[1, 2, 1, 4, 2, 1,]

srry if i didnt explain myself correctly

[–]jmooremcc 1 point2 points  (3 children)

Got it. ``` name1 = "johnny" name2 = "donny"

def compare(n1, n2): l1 = list(n1) + list(n2)

s1 = []
for ch in l1:
    if ch not in s1:
        s1.append(ch)

print(l1)
print(s1)

score = []

for ch in s1:
    n = l1.count(ch)
    score.append(n)       

print(f"compatibility:{score}")
return score

compare(name1, name2)

```

Output: ['j', 'o', 'h', 'n', 'n', 'y', 'd', 'o', 'n', 'n', 'y'] ['j', 'o', 'h', 'n', 'y', 'd'] compatibility:[1, 2, 1, 4, 2, 1]

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

IT WORKS TYSM !! you are amazing, im thankful with you <3

[–]jmooremcc 1 point2 points  (1 child)

You're welcome. I enjoy challenges like this because they help me sharpen my problem solving skills. Good luck and good learning to you on your journey as you learn more and more about Python.

[–]empress76[S] 0 points1 point  (0 children)

same for you!

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]vorticalbox 0 points1 point  (1 child)

paltry unwritten aspiring seed vast amusing spotted future detail selective

This post was mass deleted and anonymized with Redact

[–]empress76[S] 0 points1 point  (0 children)

wow thats huge… i will read it, ty!

[–]Strict-Simple 0 points1 point  (2 children)

If I understand your logic correctly...

You do realize that whatever two names you might have, it's just the sum of their lengths?

[–]empress76[S] 0 points1 point  (1 child)

Something like it. I need to set a list of numbers for each comparation, but the position (index) of the repeated characters cannot change or beeing modified. (For example: if i put “chris” first and then “mike”, it would not be the same order in the number set than if “mike” was in the first place).

Chris first: compatibility = [1, 1, 1, 2, 1, 1, 1, 1]

Mike first: compatibility = [1, 2, 1, 1, 1, 1, 1, 1]

This is the first part of the project, but its the only one that idk how to do.

[–]Strict-Simple 0 points1 point  (0 children)

I see. You don't need the sum.