首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >开放的银行问题,似乎无法弄清楚为什么我的解决方案没有通过最后一个测试用例

开放的银行问题,似乎无法弄清楚为什么我的解决方案没有通过最后一个测试用例
EN

Stack Overflow用户
提问于 2022-01-06 08:35:19
回答 1查看 56关注 0票数 -1

我想在DoSelect平台上解决一个问题。

以下是问题陈述

作为开发人员,Sid希望为Open项目做出贡献。他希望为存款、提款、增加利息以及对银行账户(在某些情况下)使用银行手续费创造功能。

此外,这类帐户的一个限制因素是,如果操作导致帐户余额为负数,则帐户余额将被重新初始化为100。例如,如果当前余额为40,您想提取60,那么由于40-60是负的,在此操作后,现在帐户有100。

因为他认为自己是一个聪明的程序员,他想为所有这些任务创建一个单一的功能。函数是"Transaction“,最多有两个参数"a”和"b“。它根据给定的参数执行不同的任务。但是Sid对OOP的概念一无所知。

代码语言:javascript
复制
class Account:
      function init (variable balance):
          //init is the standerd python __init__(self, variables) method. 
          //it initializes the account with user given balance 
          member variable balance= balance 
 
      function transaction (variable a, variable b):
          //a (if given) denotes the amount to be withdraw/deposit in the account. 
              //so add a to balance if a is positive, otherwise subtract (-a) from balance if a is negative.
          //b (if given) denotes the interest rate to give/charge to the account based on its sign.
              //so add b% of current balance (rounded to nearest integer) to the balance as an interest, if b is positive 
          //otherwise subtract (-b)% of currenct balance (rounded to nearest integer) to the balance as bank charge, if b is negative
          //after each operation, take care that, any time balance goes to negative, make it as 100. 
 
          //if neither a nor b is given:
              //do nothing
          //if a is given but b is not given:
              //add/subtract a to the balance, based on sign of a
          //if a is not given and b is given:
              //add/subtract b% of current balance to the balance, based on sign of b
          //if both a and b are given:
              //first add/subtract a to the balance, get the new balance, then add/subtract b% of the new balance to the new balance.
          //everytime take care that, if balance goes to negative, reinitialize it with 100.

以下是我所做的

代码语言:javascript
复制
class Account:
    
    def __init__(self, balance):
        self.balance = balance
        #self.balance = 100
    
    def transaction(self, a = 0, b = 0):
        if a != 0 and b != 0:
            self.balance += a
            self.balance += round((self.balance) * (b / 100))
            if self.balance < 0:
                self.balance = 100
        elif a != 0 and b == 0:
            self.balance += a
            if self.balance < 0:
                self.balance = 100
        elif a==0 and b != 0:
            self.balance += round((self.balance) * (b / 100))
            if self.balance < 0:
                self.balance = 100
        else:
            pass
        return self.balance
    
    def get_balance(self):
        return self.balance

当我在平台上验证时,它成功地验证了测试用例。然而,当我尝试提交它时,它在一个测试用例上失败了。DoSelect平台没有显示他们用来验证的测试用例,所以我找不到它。

EN

回答 1

Stack Overflow用户

发布于 2022-01-06 18:45:06

正如我在评论中所说:Account(0).transaction(-1, 10)100提供了解决方案。任务说,每当余额变为负值时,就将其重新初始化为100。如果将此规则应用于每一步,则结果应该是110 (0 -> -1 -> 100 -> 110)。

我不认为ab需要不同的if-组合。这是开发人员必须将需求转换为有意义的代码的时刻。并且不需要get_balance方法,您可以直接访问该属性。

代码语言:javascript
复制
class Account:
    def __init__(self, balance):
        self.balance = balance
        if self.balance < 0:
            self.balance = 100

    def transaction(self, a=0, b=0):
        self.balance += a
        if self.balance < 0:
            self.balance = 100
        self.balance += round(self.balance * (b / 100))
        if self.balance < 0:
            self.balance = 100


account = Account(0)
account.transaction(-1, 10)
print(account.balance)

一个“普通”的程序员会创建一个处理if self.balance < 0的方法,但是由于Sid不知道OOP的概念,所以我只是重复了代码。使用OOP,该类的代码可以是:

代码语言:javascript
复制
class Account:
    def __init__(self, balance):
        self.balance = balance
        self.adjust_balance()

    def adjust_balance(self):
        if self.balance < 0:
            self.balance = 100

    def transaction(self, a=0, b=0):
        self.balance += a
        self.adjust_balance()
        self.balance += round(self.balance * (b / 100))
        self.adjust_balance()

有了更多的经验,程序员可能会使用属性来实现平衡。

代码语言:javascript
复制
class Account:
    def __init__(self, balance):
        self._balance = balance

    @property
    def balance(self):
        return self._balance

    @balance.setter
    def balance(self, value):
        self._balance = value
        if self._balance < 0:
            self._balance = 100

    def transaction(self, a=0, b=0):
        self.balance += a
        self.balance += round(self.balance * (b / 100))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70604417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档