You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python不使用if、列表、字典及模块,如何根据模5余数返回对应字符串

整数对5取余返回对应英文拼写的实现方案

核心思路

利用Python中布尔类型的数值特性(bool是int子类,True等价于1,False等价于0),结合字符串与整数的乘法规则(整数为0返回空字符串,整数为1返回原字符串),直接通过余数等值判断拼接结果,完全规避禁用结构。

实现代码

def get_remainder_word(num: int) -> str:
    remainder = num % 5
    return (remainder == 0) * "Zero" + \
           (remainder == 1) * "One" + \
           (remainder == 2) * "Two" + \
           (remainder == 3) * "Three" + \
           (remainder == 4) * "Four"

效果验证

  • 输入14:14 % 5 = 4,仅remainder == 4判断为True,其余均为False,最终返回结果"Four",符合要求
  • 输入15:15 % 5 = 0,仅remainder == 0判断为True,最终返回结果"Zero",符合要求

合规说明

实现过程未使用if分支、字典、列表等任何被限制的结构,未导入任何模块,仅使用基础的取余运算、布尔判断、算术运算、字符串拼接操作,完全符合题目约束。

内容的提问来源于stack exchange,提问作者alumen-

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:06:02