#πŸ”’ Recursion search algorithm error

82 messages Β· Page 1 of 1 (latest)

finite lotus
#
class Algorithms:
    def __init__(self, list) -> None:
        self.list = list
    
    def recursion_search(listn, start, end, index):
        if (end < start):
            print('wtf..')
            return ValueError('End is less than 0')
        else:
            middle_value = start + ((end - start) // 2)
            if listn[middle_value] < index: # The line that causes the problem
                return Algorithms.recursion_search(listn, middle_value+1, end, index)
            elif listn[middle_value] > index:
                return Algorithms.recursion_search(listn, start, middle_value-1, index)

When I run the code above I get this error
TypeError: 'LinkedList' object is not subscriptable

I don't know what that means please help

clear swallowBOT
#

@finite lotus

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

finite lotus
#

How do I add Syntax Highlighting?

#

Line 11 is what causing the issue.

#

listn is the list I'm trying to iterate over

tranquil thicket
#

But what did you inputted in?

finite lotus
#

A linked list

#

With a head node and more

tranquil thicket
#

And what is the linked list?

#

It's more like the issue with the linked list instead of the algorithm lul

finite lotus
#

Hold on this one is blurry

tranquil thicket
#

Tell me, how would the linkedlist could possibly do list[index]

#

Cuz I don't see where you implemented it

tranquil thicket
#

Also you cannot use binary search either since the data is not ordered

finite lotus
finite lotus
tranquil thicket
#

And also if you are searching for index(which is in ordered), why do you need value of the list at the location

tranquil thicket
finite lotus
#

I'm trying to get a specific value

tranquil thicket
#

And how have you implemented __getitem__ that allow you to use index

finite lotus
tranquil thicket
#

Since linkedlist is just a object instead of a actual list

finite lotus
#

Oh

#

Ok....

tranquil thicket
#

Let me find docs for you

#

Also you have to fully describe what the algorithm suppose to do, I suspect there is a large logical error

#

In one of the 2 way

finite lotus
#

It's a recursion algorithms

#

It's supposed to get an item in the list

#

A specific item

tranquil thicket
#

In what condition

finite lotus
#

Using the index of that item

tranquil thicket
#

Then why don't you just check every head and count the index

#

Why do you need a middle_value

finite lotus
#

I'm trying to apply an algorithm

tranquil thicket
#

But what the reason of using this algorithm

finite lotus
#

I'm making a library

finite lotus
tranquil thicket
#

I mean, why do you need this algorithm, why logically this algorithm is better then just checking it one by one

finite lotus
#

I will code more algorithms, this is just the one I'm starting with

tranquil thicket
#

...I literally don't see any advantage and I see a logical issue

#

On the algorithm

finite lotus
#

I'm learning data structures and algorithms

tranquil thicket
#

I can see it's very similar to binary search

#

But binary search is suppose to find a value if it's exist on the list

finite lotus
#

Binary search by recursion

tranquil thicket
#

And binary search value must be ordered

finite lotus
#

How do I make it ordered??

tranquil thicket
#

Either order it(which no reason since you are making linkedlist) or don't use binary search

finite lotus
#

I'm so confused.

tranquil thicket
#

And in binary search, you need to give a value and it search for a value

tranquil thicket
finite lotus
#

I am making an algorithm so I can sort through the linked list?

finite lotus
tranquil thicket
#

And also binary search takes in a value, not a index

finite lotus
#

I will send screenshots

tranquil thicket
#

(Also it's not logically reasonable to sort a list and then binary search it if it have to be sort everytime

finite lotus
tranquil thicket
#

Sorting take O(log(n)*n)
Binary search takes O(log(n))

While using just linear search take O(n)

tranquil thicket
finite lotus
#

I feel so stupid.

#

F

tranquil thicket
#

So where is sorting

finite lotus
#

How can I sort through a linked list??

tranquil thicket
#

You could but that's unreasonable

#

You can gather all node in a linkedlist

#

Into a single list

#

And then sort them by hash

#

Of the value

#

And reconstruct the linkedlist

finite lotus
#

Ok thanks

#

!close

clear swallowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.