Day8

  1. 输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)

    例如: 输入'abcd1234 ' ** 输出'bd24'**

str1 = input()

for index, each in enumerate(str1):
    if index & 1:
        print(each, end='')
  1. 输入用户名,判断用户名是否合法(用户名长度6~10位)
def validate_username(username: str) -> bool:
    if isinstance(username, str) and 6 <= len(username) <= 10:
        return True
    return False
  1. 输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)

    例如: 'abc' — 合法 '123' — 合法 ‘abc123a’ — 合法

def validate_username(username: str) -> bool:
    if not isinstance(username, str):
        return False

    for each in username:
        if not (48 <= ord(each) <= 57 or 65 <= ord(each) <= 90 or 97 <= ord(each) <= 122):
            return False
        
    return True
  1. 输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)

    例如: 'abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法

def validate_username(username: str) -> bool:
    if not isinstance(username, str):
        return False

    isCap = True if len(username) > 0 and 65 <= ord(username[0]) <= 90 else False
    hasNum = False
    hasWord = False

    for each in username:
        if not (48 <= ord(each) <= 57 or 65 <= ord(each) <= 90 or 97 <= ord(each) <= 122):
            return False
        elif 48 <= ord(each) <= 57 and not hasNum:
            hasNum = True
        elif (65 <= ord(each) <= 90 or 97 <= ord(each) <= 122) and not hasWord:
            hasWord = True


    return (isCap and hasNum and hasWord)
  1. 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串

    例如:输入'abc1shj23kls99+2kkk' 输出:'123992'

def extract_num(s: str) -> str:
    if not isinstance(s, str) and len(s) > 0:
        return None

    num_str = []
    for each in s:
        if 48 <= ord(each) <= 57:
            num_str.append(each)

    return ''.join(num_str)
  1. 输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出 (用upper方法和自己写算法两种方式实现)

    例如: 输入**'a2h2klm12+' ** 输出 'A2H2KLM12+'

def upper(username: str) -> str:
    if not isinstance(username, str) and len(username) > 0:
        return None

    new_str = []
    for each in username:
        asc = ord(each)
        if 97 <= asc <= 122:
            asc -= 32
        new_str.append(chr(asc))

    return ''.join(new_str)
  1. 输入一个小于1000的数字,产生对应的学号

    例如: 输入'23',输出'py1901023' 输入'9', 输出'py1901009' 输入'123',输出'py1901123'

def generate_id(num: int) -> str:
    if not isinstance(num, int) and 0 <= num < 1000:
        return None
    return 'py1901' + str(num).zfill(3)
  1. 输入一个字符串,统计字符串中非数字字母的字符的个数

    例如: 输入'anc2+93-sj胡说' 输出:4 输入'===' 输出:3

def count(string: str) -> int:
    if not isinstance(string, str):
        return False

    count = 0
    for each in string:
        if not (48 <= ord(each) <= 57 or 65 <= ord(each) <= 90 or 97 <= ord(each) <= 122):
            count += 1
    return count
  1. 输入字符串,将字符串的开头和结尾变成'+',产生一个新的字符串

    例如: 输入字符串'abc123', 输出'+bc12+'

def process_str(string: str) -> str:
    if not isinstance(string, str):
        return None

    new_str = list(string)
    new_str[0] = '+'
    new_str[len(new_str) - 1] = '+'

    return ''.join(new_str)
  1. 输入字符串,获取字符串的中间字符

例如: 输入'abc1234' 输出:'1' 输入'abc123' 输出'c1'

def extract_middle_str(string: str) -> str:
    if not isinstance(string, str):
        return None

    if len(string) <= 2:
        return string

    i = len(string) // 2
    middle_str = string[i] if len(string) & 1 else string[i-1: i+1]

    return middle_str
  1. 写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置)

例如: 字符串1为:how are you? Im fine, Thank you! , 字符串2为:you, 打印8

def find(str1: str, str2: str) -> int:
    if not isinstance(str1, str) and not isinstance(str2, str):
        return None

    list1 = list(str1)
    list2 = list(str2)

    for index in range(len(list1)):
        for ch1, ch2 in zip(list1[index::], list2):
            if ch2 != ch1:
                break
        else:
            return index
  1. 获取两个字符串中公共的字符

例如: 字符串1为:abc123, 字符串2为: huak3 , 打印:公共字符有:a3

def public_str(str1: str, str2: str) -> str:
    if not isinstance(str1, str) and not isinstance(str2, str):
        return None

    list1 = set(str1)
    list2 = set(str2)

    return ''.join(list1 & list2)

你可能感兴趣的:(Day8)