#🔒 How do I make this LinkedList work

14 messages · Page 1 of 1 (latest)

crimson umbra
#

class LinkedList:
# TODO: items needs a default value that IS NOT mutable
# e.g. DO NOT use an empty list
def init(self, items=None):
'initialize a new LinkedList w/ optional collection items'
if items is None:
self.items = []
_len = len(items)
else:
self.items = items

def add_first(self, item):
    'adds item to beginning of linked list'

    # create a new node pointed at self._head
    self._head = item

    # update self._head
    item = _head

    # update len
    _len = len(items)
sacred topazBOT
#

Hey @crimson umbra!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
sacred topazBOT
#

@crimson umbra

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.

crimson umbra
rare dawn
#

Do you have any test cases?

crimson umbra
#
    'Test cases specific to LinkedList Class'

    def test_1_init(self):
        'initialize with or without a collection'
        # Initialize empty LL
        ll1 = LinkedList()
        self.assertEqual(len(ll1), 0)

        # Initialize an LL w/ 10 items
        ll2 = LinkedList(range(10))
        self.assertEqual(len(ll2), 10)

        L = [item for item in ll2]
        self.assertEqual(L, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    def test_2_add_remove_first(self):
        'adds and removes 100 items to/from beginning of LL'
        ll1 = LinkedList()
        n = 100

        # repeat a few times to make sure removing the last item doesn't break
        # anything
        for j in range(10):

            for i in range(n):
                self.assertEqual(len(ll1), i)
                ll1.add_first(i)

            for i in range(n):
                self.assertEqual(len(ll1), n-i)
                self.assertEqual(ll1.remove_first(), n-1-i)
rare dawn
#

Is this a homework?

crimson umbra
#

yeah

#

I've been trying to figure this out for a few days

rare dawn
#

Do you you see how you could implement __init__ by calling self.add_first multiple times?

crimson umbra
#

I don't

#

Could you explain

sacred topazBOT
#
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.