Last active
June 23, 2023 10:03
-
-
Save WonderfulSoap/333ccfd1b017728b928354bacdd1fa47 to your computer and use it in GitHub Desktop.
v2ex-951017 https://v2ex.com/t/951017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long_string = "This is a very long string!" | |
# 将多个if判断转化为数组,keys中多个元素为and关系 | |
conditions = [ | |
{"keys": ["Andy"], "result": "string1"}, | |
{"keys": ["Jack"], "result": "string2"}, | |
{"keys": ["Jim","Mike"], "result": "string3"}, | |
] | |
def find_result(): | |
for condition in conditions: | |
result = False | |
for key in condition["keys"]: | |
if key in long_string: | |
result = True | |
break | |
if result: | |
return condition["result"] | |
return "default string" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long_string = "This is a very long string!" | |
mydict = {"a": "string1", "b": "string2" , "c": "string3", "d":"string4"} | |
class FuckWord: | |
def __init__(self,word): | |
self.word = word | |
def pushChar(self,char): | |
# 一个个字符往里塞,当塞入的最近 len(self.word) 个字符连起来等于初始化时的self.word 的话 .hint() 返回成功,具体判断逻辑懒得写了zzz | |
pass | |
def hit(self): | |
return random.choice([True, False]) | |
def find_result2(): | |
fuckAndy = FuckWord("Andy") | |
fuckJack = FuckWord("Jack") | |
fuckJim = FuckWord("Jim") | |
fuckMike = FuckWord("Mike") | |
for char in long_string: | |
fuckAndy.pushChar(char) | |
fuckJack.pushChar(char) | |
fuckJim.pushChar(char) | |
fuckMike.pushChar(char) | |
# 防止Andy存在的时候白白完整遍历了整个字符串 | |
if fuckAndy.hit(): | |
return mydic["a"] | |
if fuckJack.hit(): | |
return mydict["b"] | |
if fuckJack.hit() and fuckMike.hint(): | |
return mydict["c"] | |
return mydict["d"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment