簡體   English   中英

開放銀行問題,似乎無法弄清楚為什么我的解決方案沒有通過最后一個測試用例

[英]open bank problem, cant seem to figure out why my solution isnt passing one last test case

我正在嘗試解決 DoSelect 平台上的問題。

以下是問題陳述

Sid,作為一名開發人員,希望為 Open Bank 項目做出貢獻。 他想為銀行賬戶創建存款、取款、增加利息和收取銀行費用(在某些情況下)的功能。

這種類型的賬戶還有一個約束,即任何時候如果操作導致賬戶余額為負,賬戶余額會重新初始化為 100。例如,如果當前余額為 40,而您要提取 60,則由於 40-60 為負數,經過此操作,現在帳戶有 100。

由於他認為自己是一個聰明的編碼員,他想為所有這些任務創建一個 function。 function 是“事務”,最多需要兩個參數“a”和“b”。 根據給定的參數,它執行不同的任務。 但是 Sid 對 OOP 的概念一無所知。

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.

以下是我所做的

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

當我在平台上驗證時,它在測試用例上成功驗證。 但是,當我嘗試提交它時,它在 1 個測試用例上失敗。 DoSelect 平台沒有顯示他們用來驗證的測試用例,所以我無法弄清楚。

正如我在評論中所說: Account(0).transaction(-1, 10)給你的解決方案100 該任務說每次變為負數時將余額重新初始化為100 如果將此規則應用於每一步,則結果應為110 (0 -> -1 -> 100 -> 110)。

我認為ab不需要不同的 if 組合。 這是開發人員必須將需求轉換為有意義的代碼的時刻。 並且不需要get_balance方法,可以直接訪問屬性。

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,class 的代碼可以是:

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()

有了更多經驗,程序員可能會使用屬性來平衡。

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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM