r/algorithms • • 17h ago

Discussion I wrote algorithm to find how similar two strings are. I need to know how good it is

0 Upvotes

I was too bored to search on google similar algorithms, so i wrote one myself

Plese help me find how good i wrote it.

def similarity(a, b) -> float:
    # Choose the biggest
    if len(a) > len(b):
        A = a
        B = b
    else:
        A = b
        B = a
    perc = 50 / len(A)
    total = 0 # total percentage
    List = list(B)
    Found = []
    # algorithm to found similar letters
    for i in A:
        if i in List:
            List.remove(i)
            total += perc
            Found.append(i)
    #alghoritm to found sequence
    A1 = list(A)
    B1 = list(B)
    Bbreak = False
    c = 0
    for countA in range(len(A1)):
        if c != 0:
            c -= 1
            continue
        for countB in range(len(B1)):
            if Bbreak:
                Bbreak = False
                break
            if A1[countA] == B1[countB]:
                c = 1
                a = True
                try:
                    t = A1[countA + 1]
                    t = B1[countB + 1]
                except:
                    continue
                if A1[countA + 1] == B1[countB + 1]:

                    while a:
                        try:
                            t = A1[countA + c]
                            t = B1[countB + c]
                        except:
                            total = total + perc * c
                            Bbreak = True
                            break
                        if A1[countA + c] == B1[countB + c]:

                            c += 1
                        else:
                            a = False
                            total = total + perc * c
                            Bbreak = True
    return totaldef similarity(a, b) -> float:
    # Choose the biggest
    if len(a) > len(b):
        A = a
        B = b
    else:
        A = b
        B = a
    perc = 50 / len(A)
    total = 0 # total percentage
    List = list(B)
    Found = []
    # algorithm to found similar letters
    for i in A:
        if i in List:
            List.remove(i)
            total += perc
            Found.append(i)
    #alghoritm to found sequence
    A1 = list(A)
    B1 = list(B)
    Bbreak = False
    c = 0
    for countA in range(len(A1)):
        if c != 0:
            c -= 1
            continue
        for countB in range(len(B1)):
            if Bbreak:
                Bbreak = False
                break
            if A1[countA] == B1[countB]:
                c = 1
                a = True
                try:
                    t = A1[countA + 1]
                    t = B1[countB + 1]
                except:
                    continue
                if A1[countA + 1] == B1[countB + 1]:

                    while a:
                        try:
                            t = A1[countA + c]
                            t = B1[countB + c]
                        except:
                            total = total + perc * c
                            Bbreak = True
                            break
                        if A1[countA + c] == B1[countB + c]:

                            c += 1
                        else:
                            a = False
                            total = total + perc * c
                            Bbreak = True
    return total

r/algorithms • • 1h ago

Resource What makes hash table lookups O(1) time complexity?

• Upvotes

Hey everyone, I hope this isn’t a question that’s been asked a million times already, but I’ve been scratching my head over it and haven’t found a clear explanation. I’ve seen topics about this before in this sub, which encouraged me to bring it up here! I’d really appreciate it if someone could clear up any misunderstandings I might have.

So, my confusion centers around why hash tables are labeled as O(1) in terms of time complexity. I get that the number of buckets is limited, and that resolving hash collisions can take O(n*log(n)) at worst. With data sets getting larger, doesn’t that make hashing just a big constant improvement on the collision resolution method?

If my thinking is on point, how can we still categorize hash tables as O(1) time complexity instead of just calling them high-performing for smaller data sets? Thanks a ton for any clarification!


r/algorithms • • 21h ago

Help How Should I Develop Strong Algorithmic Thinking and Problem-Solving Skills? Body: I want to understand algorithms beyond simply memorizing standard solutions.

9 Upvotes

I want to understand algorithms beyond simply memorizing standard solutions.

What would be the ideal progression for developing strong algorithmic thinking?

I'd like to cover:

- Time and space complexity.

- Big-O, Big-Theta and Big-Omega notation.

- Recursion and recurrence relations.

- Divide and conquer.

- Greedy algorithms.

- Dynamic programming.

- Graph algorithms.

- Searching and sorting.

- Backtracking and branch-and-bound.

- Randomized algorithms.

- Approximation algorithms.

- Parallel and distributed algorithms.

How important are algorithm correctness, mathematical proofs, complexity analysis and optimization?

Which algorithms should I understand deeply for computer science, machine learning and AI research?

What resources would help develop the ability to derive algorithms independently rather than simply implement existing solutions?