- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Q.
Suppose that our banking system requires the ability to make joint accounts. Define a function
make_joint
that takes three arguments.
A password-protected
withdraw
function,The password with which that
withdraw
function was defined, andA new password that can also access the original account.
The
make_joint
function returns awithdraw
function that provides additional access to the original account using either the new or old password. Both functions draw down the same balance. Incorrect passwords provided to either function will be stored and cause the functions to be locked after three wrong attempts.Hint: The solution is short (less than 10 lines) and contains no string literals! The key is to call
withdraw
with the right password and interpret the result. You may assume that all failed attempts towithdraw
will return some string (for incorrect passwords, locked accounts, or insufficient funds), while successful withdrawals will return a number.Use
type(value) == str
to test if some value is a string:
def make_withdraw(balance, password):
"""Return a password-protected withdraw function.
>>> w = make_withdraw(100, 'hax0r')
>>> w(25, 'hax0r')
75
>>> w(90, 'hax0r')
'Insufficient funds'
>>> w(25, 'hwat')
'Incorrect password'
>>> w(25, 'hax0r')
50
>>> w(75, 'a')
'Incorrect password'
>>> w(10, 'hax0r')
40
>>> w(20, 'n00b')
'Incorrect password'
>>> w(10, 'hax0r')
"Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
>>> w(10, 'l33t')
"Your account is locked. Attempts: ['hwat', 'a', 'n00b']"
"""
incorrect_password_list = []
count = 0
def withdraw(amount, input_passwd):
nonlocal count
if count < 3:
if input_passwd == password:
nonlocal balance
if amount < balance:
balance -= amount
return balance
else:
return "Insufficient funds"
else:
nonlocal incorrect_password_list
incorrect_password_list.append(input_passwd)
count += 1
return "Incorrect password"
else:
return "Your account is locked. Attempts: ['{0}', '{1}', '{2}']".format(incorrect_password_list[0], incorrect_password_list[1], incorrect_password_list[2])
return withdraw
def make_joint(withdraw, old_passwd, new_passwd):
"""
>>> w = make_withdraw(100, 'hax0r')
>>> w(25, 'hax0r')
75
>>> make_joint(w, 'my', 'secret')
'Incorrect password'
>>> j = make_joint(w, 'hax0r', 'secret')
>>> w(25, 'secret')
'Incorrect password'
>>> j(25, 'secret')
50
>>> j(25, 'hax0r')
25
>>> j(100, 'secret')
'Insufficient funds'
"""
value = withdraw(0, old_passwd)
if type(value) == str:
return "Incorrect password"
else:
def joint_withdraw(balance, input_passwd):
if (input_passwd == old_passwd) or (input_passwd == new_passwd):
return withdraw(balance, old_passwd)
return joint_withdraw
我的问题:
除了上述解决方案 make_joint
的文档字符串中提到的测试用异常(exception),以下其他测试用例尚未通过,请提供修改 make_joint
的提示以满足这些给定的附加测试用例测试用例(如下)。
>>> j2 = make_joint(j, 'secret', 'code')
>>> j2(5, 'code')
20
>>> j2(5, 'secret')
15
>>> j2(5, 'hax0r')
10
>>> j2(25, 'password')
'Incorrect password'
>>> j2(5, 'secret')
"Your account is locked. Attempts: ['my', 'secret', 'password']"
最佳答案
让我们只关注这些行:
def joint_withdraw(balance, input_passwd):
if (input_passwd == old_passwd) or (input_passwd == new_passwd):
return withdraw(balance, old_passwd)
当input_passwd
既不是old_passwd
也不是new_passwd
时会发生什么?您的函数不执行任何操作,也没有给出任何响应。
请注意,您已经有了响应错误密码的代码。如何定义 joint_withdraw
来重用该功能?
按照我暗示的方式解决该问题,您应该能够让 j2(5, 'hax0r')
工作。
关于python - 如何修复高阶函数来模拟联名银行账户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30984193/
我有一个英国 PayPal 企业帐户。我目前正在开发一个网站支付系统,使用 Express Checkout 向这个账户付款。 到目前为止,我的 PHP 代码正在运行,我能够连接到沙箱并设置和快速结帐
如何使用自定义消息和自定义 from 参数而不是默认参数向用户发送电子邮件验证。 Meteor.methods({ sendveryficationmail: function (theuse
考虑一个场景,其中他们是 PartyA,并且 account1 是在 PartyA 中创建的。现在我们要在account1和PartyA之间发起一笔交易。是否可以有一个由账户发起、接收方为主机节点的交
我有多个 AWS 账户,根据我所在的项目目录,我想在 AWS CLI 中键入命令时使用不同的账户。 我知道 AWS 凭据可以通过环境变量传递,所以我认为一种解决方案是根据它所在的目录设置 AWS_CO
我希望能够使用 PayPal Mass Payment 向我网站的用户付款。如果他们有 PayPal 帐户,我认为这非常简单。 但是,如果他们没有 PayPal 帐户,有没有办法让他们通过我的网站注册
是否可以使用 paypal API 向任何 paypal 帐户(不仅仅是 API 凭据所有者)汇款。我知道使用 IPN 可以做到这一点,但我需要使用 SOAP。 最佳答案 您有两个主要选择:使用 Ma
这个问题在这里已经有了答案: Confirm PayPal sandbox account email (3 个答案) 关闭 2 年前。
是否可以使用 paypal API 向任何 paypal 帐户(不仅仅是 API 凭据所有者)汇款。我知道使用 IPN 可以做到这一点,但我需要使用 SOAP。 最佳答案 您有两个主要选择:使用 Ma
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8 年前关闭。 Improve this
我一直在尝试 Meteor。我想使用 OAuth 对我网站上的用户进行身份验证,因为我不想自己实现登录功能。 目前我的网站非常简单。计数器,单击按钮计数器就会加一。当用户转到另一台机器并登录其计数时,
在我看来,您可以输入任何随机的用户名/密码组合,并且嵌入式 UI 小部件和后端都会接受它作为有效的 PayPal 帐户。付款和一切都会完成。 有没有办法可以将 Braintree 沙箱设置为仅接受真实
我已经设置并一直在使用 PayPal 使用 IPN/Express Checkout 在我的主网站上接受订单。但是我还有另外 4 个完全独立的网站,我也需要在它们上使用 PayPal 处理订单。购买我
我来这里是为了尝试解决 paypal 中没有人可以帮助我们解决的看似黑洞的问题。 我们有一个企业帐户。 Paypal Express(数码商品)。已验证,解除限制等...我们有 paypal expr
我正在编写一个 Web 应用程序,过去他们使用自己的 PayPal 帐户为外部自雇人员开设的类(class)付款。然而,税收制度已经改变,慈善机构希望他们的客户通过他们的网站预订(就像以前一样),但直
让我解释一下我的网站(ruby on Rails)的当前场景: 1) 我们可以从我的网站创建多个管理员帐户。 2) 每个管理员都有自己的客户,这些客户也有他们的网站访问部分。 3) 每个客户都可以向他
我正在两个 AWS 账户之间迁移。我想从我的 Mac 临时访问两个帐户上的代码提交存储库。 我已经为两个帐户的用户生成了 HTTPS Git 凭据。当我最初访问旧帐户时,它要求我提供存储在 OSX 钥
是否可以将一笔金额从一个关联账户转移到另一个关联账户?两者都在一个 Stripe 帐户下连接。 我知道我可以在两个帐户之间拆分转账,例如 $transfer = \Stripe\Transfer::c
如果一个人有两个 AWS 账户,一个用于开发,一个用于实时(例如)我知道可以使用 terraform 工作区来管理每个环境的状态。 但是,如果我将工作区从“开发”切换到“实时”,有没有办法告诉 ter
在 Meteor 中,我想将数据(和其他 CRUD 函数)插入到 users 表中,该表是在 Meteor 中下载 account-ui 和 account-password 包时生成的。我尝试在 c
有没有办法将 ETH 添加到 Ganache 账户?我知道我通常可以通过重新启动 ganache cli 来刷新帐户,但我使用的是 --db 选项,这意味着帐户是持久的。因此,它们很快就会枯竭。 最佳
我是一名优秀的程序员,十分优秀!