第34行:TypeError:__ init __()需要3个参数(给定1个)

时间:2015-10-31 15:52:21

标签: python

class Solution:
    def __init__(self, secret, guess):
        self.secret = secret
        self.guess = guess
    def getHint(self):
        right=0; wrong=0
        listSecret = list(self.secret)
        listGuess = list(self.guess)
        for i in range(len(listSecret)):
            if listSecret[i] == listGuess[i]:
                right = right + 1
            else:
                wrong = wrong + 1
        return str(right)+ "A" + str(wrong) + "B"
guessG = raw_input("")
secretS = raw_input("")
print Solution(str(guessG), str(secretS)).getHint()

当我在pycharm中运行代码时,它可以工作。但是,当我将代码提交给leetcode时,它会警告"运行时错误",请参阅图片。

enter image description here

1 个答案:

答案 0 :(得分:1)

这是 leetcode 服务问题

与测试驱动程序和程序之间通过标准IO完成通信的流行格式相比,此服务提供了另一种方式。

在开始时,您将获得一个隐式声明通信接口的代码段。 您不应该更改它,即更改方法签名,包括构造函数签名。然后,您的输入作为参数传递给代码片段方法,结果期望为其方法返回值。

如何工作以及产生奇怪的错误

您在在线编辑器中传递的代码附带了一些生成该行为的其他代码。

要检查这个想法,我已经编写了一个打印自己代码的程序(通过修改给定的代码段):

class Solution(object):
    def getHint(self, secret, guess):
        """
        :type secret: str
        :type guess: str
        :rtype: str
        """

import sys        
print sys.argv ## this prints program arguments

## argv[0] is the name of file being executed. Just print it!
with open(sys.argv[0], 'r') as fin:
    print fin.read()

在codeleet服务上执行的结果是

['/usr/lib/gcc/x86_64-linux-gnu/4.9.1/.cache/prog_joined.py', '-recursion_limit', '8100']
# coding: utf-8
from precompiled.__serializer__ import __Serializer__
from precompiled.__deserializer__ import __Deserializer__
from precompiled.__utils__ import __Utils__
from precompiled.listnode import ListNode
from precompiled.interval import Interval
from precompiled.treenode import TreeNode
from precompiled.treelinknode import TreeLinkNode
from precompiled.undirectedgraphnode import UndirectedGraphNode
from precompiled.randomlistnode import RandomListNode
from precompiled.point import Point
import precompiled.__settings__
import array
import bisect
import collections
import copy
import heapq
import itertools
import math
import operator
import re
import sets
import string

# user submitted code insert below
class Solution(object):
    def getHint(self, secret, guess):
        """
        :type secret: str
        :type guess: str
        :rtype: str
        """

import sys        
print sys.argv

with open(sys.argv[0], 'r') as fin:
    print fin.read()
import sys
def _driver():
    SEPARATOR = "\x1b\x09\x1d"
    f = open("user.out", "w", 0)
    lines = __Utils__().read_lines()
    while True:
        try:
            line = lines.next()

            param_1 = __Deserializer__().to_string(line)
            line = lines.next()
            param_2 = __Deserializer__().to_string(line)

            ret = Solution().getHint(
                param_1, param_2
            )
            out = __Serializer__().serialize(ret)

            print >> f, "%s" % out
            sys.stdout.write(SEPARATOR)
        except StopIteration:
            break

if __name__ == '__main__':
    _driver()

这显示正在执行的整个程序。您可以注意到它是您自己代码的一个扩展版本。该版本有以下几行:

ret = Solution().getHint(
                    param_1, param_2
                )

它们是您问题的根源