我正在研究教授给我们的一个问题,但我无法让它按照我希望的方式工作。我应该先说我是Python的初学者
本质上,我要编写一个函数,给定一个字符串返回与列表相同的字符串,列表的对象是给定字符串的不同版本
例如:Hello应该返回
Hello
hEllo
heLlo
helLo
hellO
但是,我们不允许使用python的内置函数来确定一个字符是大写还是小写,比如upper()还是lower(),在此之前,我们只在MatLab中进行过“编程”,我正在寻找一些入门帮助
到目前为止,我尝试的是这样的:
def randRetur(menBok,dicT):
menBok = list(menBok)
for i,j in enumerate(menBok):
if menBok[i] in dicT.values():
scrambledPws.append(menBok)
else:
scrambledPws.append(dicT[j])
return scrambledPws
我的字典将小写字母映射为大写字母,而DPWS只是一个空列表
然而,这并没有给出我想要的东西,而是在列表的第一个对象中,所有东西都大写了,这就是我对pythons如何处理for循环的理解与MatLab如何处理它的理解非常不同的地方。因此,我更希望得到一些指导,而不是如何去做,因为我正在努力学习
这个问题有多个部分,我可以帮你回答其中的一部分。这部分,, 给定一些字符,您需要返回大写变量,而不使用 字符串方法
upper()
。我不确定CPython的实现,但是 Ruby one在这里:https://github.com/ruby/ruby/blob/fb3c711d/regenc.c#L970-L977
下面是一个简单的Python实现:
输入应为单个字符,输出为上限变量。 请注意,这仅适用于ASCII字符
解决方案非常简单,我将字符串转换为列表,并将包含该字符的列表的索引更改为大写版本。然后我将列表返回到一个字符串,并将其添加到combos中。然后我返回了组合列表。 对不起,我的解释不太好。但希望代码能更好地解释它
见下文
我们的想法是使用2个DICT来支持从低到高&;从上到下
输出
相关问题 更多 >
编程相关推荐