Leetcode's two sums error __init__() missing 2 positional arguments












1














I'm solving leetcode's two sums and I get error __init__() missing 2 positional arguments



Here is my code:



class Solution(object):
def __init__(self, nums, target):
self.nums = nums
self.target = target
def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])









share|improve this question




















  • 1




    You probably should "shift" your parameters to the twoSum function, it has not much to do with how you define your class, more how it is "used".
    – Willem Van Onsem
    Nov 11 at 20:06






  • 1




    You must be calling Solution(), but you should be calling Solution(nums, target)
    – JacobIRR
    Nov 11 at 20:06






  • 1




    Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
    – Mikhail Stepanov
    Nov 11 at 20:48
















1














I'm solving leetcode's two sums and I get error __init__() missing 2 positional arguments



Here is my code:



class Solution(object):
def __init__(self, nums, target):
self.nums = nums
self.target = target
def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])









share|improve this question




















  • 1




    You probably should "shift" your parameters to the twoSum function, it has not much to do with how you define your class, more how it is "used".
    – Willem Van Onsem
    Nov 11 at 20:06






  • 1




    You must be calling Solution(), but you should be calling Solution(nums, target)
    – JacobIRR
    Nov 11 at 20:06






  • 1




    Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
    – Mikhail Stepanov
    Nov 11 at 20:48














1












1








1







I'm solving leetcode's two sums and I get error __init__() missing 2 positional arguments



Here is my code:



class Solution(object):
def __init__(self, nums, target):
self.nums = nums
self.target = target
def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])









share|improve this question















I'm solving leetcode's two sums and I get error __init__() missing 2 positional arguments



Here is my code:



class Solution(object):
def __init__(self, nums, target):
self.nums = nums
self.target = target
def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])






python constructor arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 4:59









Dinko Pehar

1,0372324




1,0372324










asked Nov 11 at 20:04









Quoc Anh Nguyen

83




83








  • 1




    You probably should "shift" your parameters to the twoSum function, it has not much to do with how you define your class, more how it is "used".
    – Willem Van Onsem
    Nov 11 at 20:06






  • 1




    You must be calling Solution(), but you should be calling Solution(nums, target)
    – JacobIRR
    Nov 11 at 20:06






  • 1




    Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
    – Mikhail Stepanov
    Nov 11 at 20:48














  • 1




    You probably should "shift" your parameters to the twoSum function, it has not much to do with how you define your class, more how it is "used".
    – Willem Van Onsem
    Nov 11 at 20:06






  • 1




    You must be calling Solution(), but you should be calling Solution(nums, target)
    – JacobIRR
    Nov 11 at 20:06






  • 1




    Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
    – Mikhail Stepanov
    Nov 11 at 20:48








1




1




You probably should "shift" your parameters to the twoSum function, it has not much to do with how you define your class, more how it is "used".
– Willem Van Onsem
Nov 11 at 20:06




You probably should "shift" your parameters to the twoSum function, it has not much to do with how you define your class, more how it is "used".
– Willem Van Onsem
Nov 11 at 20:06




1




1




You must be calling Solution(), but you should be calling Solution(nums, target)
– JacobIRR
Nov 11 at 20:06




You must be calling Solution(), but you should be calling Solution(nums, target)
– JacobIRR
Nov 11 at 20:06




1




1




Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 11 at 20:48




Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 11 at 20:48












1 Answer
1






active

oldest

votes


















0














The Solution class is probably missing array and target upon instantiation.



You can make it like so:



class Solution(object):

def __init__(self, nums, target):
self.nums = nums
self.target = target

def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])

nums = [1,2,3,4,5,6] # Array of numbers
target = 7 # Target
s = Solution(nums, target)
print(s.twoSum())


EDIT:



I runned code in idle. This is what I get for your code:



Idle run



But I found solution online, and it goes like this:



class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
length = len(num)
# use dict: value: index + 1
# since there is only one solution, the right value must not be duplicated
dic = {}
for i in range(0, length):
val = num[i]
if (target - val) in dic:
return (dic[target - val], i + 1)
dic[val] = i + 1



# test code
num=[2, 7, 11, 15]
t= 26
s = Solution()
print(s.twoSum(num, t))


The solution works in LeetCode interface.






share|improve this answer



















  • 1




    Hi @Dinko Pehar, I did add your part, and Leetcode still displayed the same error. Line 26: TypeError: __init__() missing 2 required positional arguments: 'nums' and 'target'
    – Quoc Anh Nguyen
    Nov 12 at 20:51












  • Check edit please
    – Dinko Pehar
    Nov 12 at 23:06






  • 1




    Hi @Dinko Pehar! Thank you so much for your help. I got so much too learn :))
    – Quoc Anh Nguyen
    Nov 14 at 0:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252712%2fleetcodes-two-sums-error-init-missing-2-positional-arguments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The Solution class is probably missing array and target upon instantiation.



You can make it like so:



class Solution(object):

def __init__(self, nums, target):
self.nums = nums
self.target = target

def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])

nums = [1,2,3,4,5,6] # Array of numbers
target = 7 # Target
s = Solution(nums, target)
print(s.twoSum())


EDIT:



I runned code in idle. This is what I get for your code:



Idle run



But I found solution online, and it goes like this:



class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
length = len(num)
# use dict: value: index + 1
# since there is only one solution, the right value must not be duplicated
dic = {}
for i in range(0, length):
val = num[i]
if (target - val) in dic:
return (dic[target - val], i + 1)
dic[val] = i + 1



# test code
num=[2, 7, 11, 15]
t= 26
s = Solution()
print(s.twoSum(num, t))


The solution works in LeetCode interface.






share|improve this answer



















  • 1




    Hi @Dinko Pehar, I did add your part, and Leetcode still displayed the same error. Line 26: TypeError: __init__() missing 2 required positional arguments: 'nums' and 'target'
    – Quoc Anh Nguyen
    Nov 12 at 20:51












  • Check edit please
    – Dinko Pehar
    Nov 12 at 23:06






  • 1




    Hi @Dinko Pehar! Thank you so much for your help. I got so much too learn :))
    – Quoc Anh Nguyen
    Nov 14 at 0:40
















0














The Solution class is probably missing array and target upon instantiation.



You can make it like so:



class Solution(object):

def __init__(self, nums, target):
self.nums = nums
self.target = target

def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])

nums = [1,2,3,4,5,6] # Array of numbers
target = 7 # Target
s = Solution(nums, target)
print(s.twoSum())


EDIT:



I runned code in idle. This is what I get for your code:



Idle run



But I found solution online, and it goes like this:



class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
length = len(num)
# use dict: value: index + 1
# since there is only one solution, the right value must not be duplicated
dic = {}
for i in range(0, length):
val = num[i]
if (target - val) in dic:
return (dic[target - val], i + 1)
dic[val] = i + 1



# test code
num=[2, 7, 11, 15]
t= 26
s = Solution()
print(s.twoSum(num, t))


The solution works in LeetCode interface.






share|improve this answer



















  • 1




    Hi @Dinko Pehar, I did add your part, and Leetcode still displayed the same error. Line 26: TypeError: __init__() missing 2 required positional arguments: 'nums' and 'target'
    – Quoc Anh Nguyen
    Nov 12 at 20:51












  • Check edit please
    – Dinko Pehar
    Nov 12 at 23:06






  • 1




    Hi @Dinko Pehar! Thank you so much for your help. I got so much too learn :))
    – Quoc Anh Nguyen
    Nov 14 at 0:40














0












0








0






The Solution class is probably missing array and target upon instantiation.



You can make it like so:



class Solution(object):

def __init__(self, nums, target):
self.nums = nums
self.target = target

def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])

nums = [1,2,3,4,5,6] # Array of numbers
target = 7 # Target
s = Solution(nums, target)
print(s.twoSum())


EDIT:



I runned code in idle. This is what I get for your code:



Idle run



But I found solution online, and it goes like this:



class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
length = len(num)
# use dict: value: index + 1
# since there is only one solution, the right value must not be duplicated
dic = {}
for i in range(0, length):
val = num[i]
if (target - val) in dic:
return (dic[target - val], i + 1)
dic[val] = i + 1



# test code
num=[2, 7, 11, 15]
t= 26
s = Solution()
print(s.twoSum(num, t))


The solution works in LeetCode interface.






share|improve this answer














The Solution class is probably missing array and target upon instantiation.



You can make it like so:



class Solution(object):

def __init__(self, nums, target):
self.nums = nums
self.target = target

def twoSum(self):
for i in range(0, len(self.nums)):
j = self.target-self.nums[i]
for a in range(i+1,len(self.nums)):
if self.nums[a]==j:
return "(%d, %d)" % (self.nums[i], self.nums[a])

nums = [1,2,3,4,5,6] # Array of numbers
target = 7 # Target
s = Solution(nums, target)
print(s.twoSum())


EDIT:



I runned code in idle. This is what I get for your code:



Idle run



But I found solution online, and it goes like this:



class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
length = len(num)
# use dict: value: index + 1
# since there is only one solution, the right value must not be duplicated
dic = {}
for i in range(0, length):
val = num[i]
if (target - val) in dic:
return (dic[target - val], i + 1)
dic[val] = i + 1



# test code
num=[2, 7, 11, 15]
t= 26
s = Solution()
print(s.twoSum(num, t))


The solution works in LeetCode interface.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 23:06

























answered Nov 11 at 20:17









Dinko Pehar

1,0372324




1,0372324








  • 1




    Hi @Dinko Pehar, I did add your part, and Leetcode still displayed the same error. Line 26: TypeError: __init__() missing 2 required positional arguments: 'nums' and 'target'
    – Quoc Anh Nguyen
    Nov 12 at 20:51












  • Check edit please
    – Dinko Pehar
    Nov 12 at 23:06






  • 1




    Hi @Dinko Pehar! Thank you so much for your help. I got so much too learn :))
    – Quoc Anh Nguyen
    Nov 14 at 0:40














  • 1




    Hi @Dinko Pehar, I did add your part, and Leetcode still displayed the same error. Line 26: TypeError: __init__() missing 2 required positional arguments: 'nums' and 'target'
    – Quoc Anh Nguyen
    Nov 12 at 20:51












  • Check edit please
    – Dinko Pehar
    Nov 12 at 23:06






  • 1




    Hi @Dinko Pehar! Thank you so much for your help. I got so much too learn :))
    – Quoc Anh Nguyen
    Nov 14 at 0:40








1




1




Hi @Dinko Pehar, I did add your part, and Leetcode still displayed the same error. Line 26: TypeError: __init__() missing 2 required positional arguments: 'nums' and 'target'
– Quoc Anh Nguyen
Nov 12 at 20:51






Hi @Dinko Pehar, I did add your part, and Leetcode still displayed the same error. Line 26: TypeError: __init__() missing 2 required positional arguments: 'nums' and 'target'
– Quoc Anh Nguyen
Nov 12 at 20:51














Check edit please
– Dinko Pehar
Nov 12 at 23:06




Check edit please
– Dinko Pehar
Nov 12 at 23:06




1




1




Hi @Dinko Pehar! Thank you so much for your help. I got so much too learn :))
– Quoc Anh Nguyen
Nov 14 at 0:40




Hi @Dinko Pehar! Thank you so much for your help. I got so much too learn :))
– Quoc Anh Nguyen
Nov 14 at 0:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252712%2fleetcodes-two-sums-error-init-missing-2-positional-arguments%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation