码迷,mamicode.com
首页 > 其他好文 > 详细

Code Signal_练习题_variableName

时间:2018-08-13 19:43:13      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:out   close   class   ring   tput   start   art   lis   dict   

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 be
      variableName(name) = true;
    • For name = "qq-q", the output should be
      variableName(name) = false;
    • For name = "2w2", the output should be
      variableName(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

标签:out   close   class   ring   tput   start   art   lis   dict   

原文地址:https://www.cnblogs.com/YD2018/p/9470193.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!