欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

华为笔试

程序员文章站 2022-07-13 14:20:00
...
while True:
    try:
        num = int(input())
        i = 2
        res = []
        while num != 1:
            if num%i == 0:
                print(i,end = ' ')   #循环内容在一行输出
                num = num/i
            else:
                i += 1
        print('')
    except:
        break

 

num = input()
l = list(reversed(num))   #将输入的数字逆转
result=[]
for i in l:
    if i not in result:
        result.append(i)
print(''.join(result))

 

st = input()
s = set(list(st))
num = 0
for i in s:
    if ord(i) >= 0 and ord(i) <= 127:   #将字符转化成ASCII数字比较
        num += 1
print(num)

 将字符串中的单词逆序输出:

st = input().split()
print(' '.join(st[::-1]))

 字符串中的字符以不同分隔符分割:

try:
    a = input()
    for i in a:
        if not i.isalpha():
            a = a.replace(i,' ')   #将字符串中的这个字符i,用空格代替
    st = a.split()
    print(' '.join(st[::-1]))
except:
    pass

 

 对输入的n个字符串排序输出:

while True:
    try:
        n = int(input())
        words = []
        for i in range(n):
            words.append(input().strip())
        for word in sorted(words):   #注意字符串排序和列表排序的区别
            print(word)
    except:
        break

验证密码是否符合要求:

import re

#检测大写字母
def Upper(st):
    if re.search(r'[A-Z]', st):
        return 1
    return 0
#检测小写字母
def Lower(st):
    if re.search(r'[a-z]', st):
        return 1
    return 0
#检测数字
def Num(st):
    if re.search(r'[0-9]', st):
        return 1
    return 0
#检测特殊字符
def Other(st):
    if re.search(r'\W', st):
        return 1
    return 0
#检测是否有重复的相同字符长度大于2
def Same(st):
    for i in range(len(st)-5):
        if st.count(st[i:i+3])>1:
            return True
    return False

#验证程序
def varity_passwd():
    s = input()
    if len(s) <= 8:
        print ('NG')
    elif sum([fun(s) for fun in [Upper, Lower, Other, Num]]) < 3:
        print ('NG')
    elif Same(s):
        print ('NG')
    else:
        print ('OK')
while True:
    try:
        varity_passwd()
    except:
        break

 简单密码翻译:

s = input()
result = ''
for i in range(len(s)):
    if s[i] >= '0' and s[i] <= '9':
        result += s[i]
        continue
    if s[i] >= 'a' and s[i] <= 'c':
        result += '2'
        continue
    if s[i] >= 'd' and s[i] <= 'f':
        result += '3'
        continue
    if s[i] >= 'g' and s[i] <= 'i':
        result += '4'
        continue
    if s[i] >= 'j' and s[i] <= 'l':
        result += '5'
        continue
    if s[i] >= 'm' and s[i] <= 'o':
        result += '6'
        continue
    if s[i] >= 'p' and s[i] <= 's':
        result += '7'
        continue
    if s[i] >= 't' and s[i] <= 'v':
        result += '8'
        continue
    if s[i] >= 'w' and s[i] <= 'z':
        result += '9'
        continue
    if s[i] == 'Z':
        result += 'a'
        continue
    if s[i] >= 'A' and s[i] <'Z':
        result += chr(ord(s[i].lower()) + 1)
        continue
print (result)
        

一串字符串,非字母位置不变,字母的位置,排序,小写在大写的前边:(这个层序有问题)

while True:
    try:
        s=input()
        temp=list(s)     
        str1=filter(lambda x:x.isalpha(),temp)
        str1.sort(key=str.lower)    #大小写都存在的时候,小写字母排在前边
        j=0
        for i in range(len(temp)):
            if temp[i].isalpha():
                temp[i]=str1[j]
                j+=1
        print(''.join(temp))
    except:
        break

密码加密和解密(程序有问题):

try:
    s1 = input()
    s2 = input()
    add_passward = ''
    for each in s1:
        if each.isdigit():
            n = int(each)
            if n != 9:
                add_passward += str(n + 1)
            else:
                add_passard += '0'
        else:
            if each.islower():
                if each != 'z':
                    add_passward += chr(ord(each)- 31)
                else:
                    add_passward += 'A'
            else:
                if each != 'Z':
                    add_passward += chr(ord(each) + 33)
                else:
                    add_passward += 'a'
                    
    decode_passward = ''
    for each in s2:
        if each.isdigit():
            n = int(each)
            if n != 0:
                decode_passward += str(n-1)
            else:
                decode_passward += str('9')
        else:
            if each.islower():
                if each != 'a':
                    decode_passward += chr(ord(each) - 33)
                else:
                    decode_passward +='Z'
            else:
                if each != 'A':
                    decode_passward += chr(ord(each) + 31)
                else:
                    decode_passward += 'z'
                    
                    
    print(add_passward)
    print(decode_passward)
except:
    pass

将字典中的字符排序,一个是先按照关键值排序,然后按照value排序:

while True:
    try:
        d = {}
        res = ''
        line = raw_input()
        for i in line:
            if i in d:
                d[i] += 1
            else:
                d[i] = 1
        d_sorted = sorted (d.items(), key=lambda item:item[0] , reverse = False) #按照关键值排序,然后按照值排序。这样就将包含数目多的并且,ascii码相对靠前的放在了前边。
        d_sorted.sort(key = lambda d:d[1] , reverse = True)
        for i in d_sorted:
            res += i[0]
        print res
    except:
        break

sorted()在字典的应用:

my_dict = {"a":"2", "c":"5", "b":"1"}

result = sorted(my_dict)
print (result)
#默认对dict排序,不指定key参数,会默认对dict的key值进行比较排序
#result输出: ['a', 'b', 'c']

result2 = sorted(my_dict, key=lambda x:my_dict[x])
print (result2)
#指定key参数,根据dict的value排序
#result2输出:['b', 'a', 'c']