TypeError: object() takes no parameters

Do you mean that the input “array” would be a dict?

[2, 5, 8, 11, 15]
{2: 0, 5: 1, 8: 2, 11: 3, 15: 4}

That’s the only way you’d avoid growing with the size of the array, by not processing it at all, and only doing constant time lookups on it

2 Likes

Ah you are right! For mine to work you would have to have the input as a dict, and converting it would be O(n)!

2 Likes

I’m only sharing haskell code from here on out, just to see if I can get away with it.

…It’s not even a bad first language, like 75% of javascript questions wouldn’t exist if it was haskell.

Please tell me one more time about how undefined isn’t callable.

3 Likes

Thanks! (and to @factoradic as well!)

Need to pay attention, not just to where I’m going, but where I’ve been!

4 Likes

I blame the people who are using “algorithm is like a cooking recipe” metaphor to explain what programming is. Cooking recipes are so imperative.

Haskell is great as a first language. The syntax resembles notation known from the math classes. In high school, everyone knows how function composition works. IO is much easier to handle in the Haskell because of the encapsulation. Great language to start.


I will create another discussion to talk about this.

3 Likes

Update: This topic has been moved into the General Programming public forum. Sorry for the mix-up.

3 Likes

Here is my solution. Anyway you guys would make it better? (For run time purposes)

This code ran in 824 ms

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        count = 0
        list1 = []
        for el in nums:
            count += 1
            cp = target - el
            if cp in nums and nums.index(cp) != count -1:
                list1.append(count-1)
                list1.append(nums.index(cp))
                return list1
1 Like

Can you include a Run sequence, please? (call with arguments and print result).

def twoSum(self, nums:
                     ^

SyntaxError: invalid syntax

2 Likes

Yeah the syntax is weird. Python 3.6 apparently added ‘Typing’ for function parameters. But this syntax is the default for the problem. I did not write it.

Here: I need help interpreting a small piece of code

1 Like

My bad for running this in Python 2. It is fortuitous in that we are made aware of the significant difference.

2 Likes

Your solution is quadratic in terms of time complexity - runtime is directly proportional to the squared size of the nums. This solution will be really slow when you will have to deal with the longer lists.

This solution is quadratic because for every element of the list (for el in nums) you search the whole list (if cp in nums).

@ionatan already described an approach that will result in linear time complexity, which will result in the runtime ~60 ms.

2 Likes

Another approach:

What numbers do I have?
What numbers do I need?
What do those sets have in common? That would be a solution.

Since solutions are two numbers, they will be adding two entries for that solution. The exception is if a number adds itself only (duplicate use)

from collections import Counter


def two_sum(nums, target):
    want = Counter(target - x for x in nums)
    have = Counter(nums)
    match = want & have
    if sum(match.values()) > 1:
        a, b = min(match), max(match)
        return [nums.index(a),
                next(i for i in range(len(nums))[::-1] if nums[i] == b)]
    return []


print(two_sum([5], 10))
print(two_sum([5, 5], 10))
print(two_sum([5, 3, 7], 10))
2 Likes