Code Signal_练习题_variableName
Posted yd2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code Signal_练习题_variableName相关的知识,希望对你有一定的参考价值。
Correct variable names consist only of English letters, digits and underscores and they can‘t start with a digit.
Check if the given string is a correct variable name.
Example
- For
name = "var_1__Int"
, the output should bevariableName(name) = true
; - For
name = "qq-q"
, the output should bevariableName(name) = false
; - For
name = "2w2"
, the output should bevariableName(name) = false
.
我的解答:
def variableName(name): dict = {‘word‘:‘qwertyuiopasdfghjklzxcvbnm‘, ‘digit‘:‘0123456789‘, ‘underline‘:‘_‘} if name[0].lower() in dict[‘word‘] or name[0] in dict[‘underline‘]: for i in name: if i.lower() in dict[‘word‘] or i in dict[‘underline‘] or i in dict[‘digit‘]: pass else: return False return True else: return False
def variableName(name): return name.isidentifier()
以上是关于Code Signal_练习题_variableName的主要内容,如果未能解决你的问题,请参考以下文章
Code Signal_练习题_stringsRearrangement