-
說明 📔
華為2019在線筆試題,現整理如下,以供之后參考
-
題目介紹 🎈
#####################################################################################################
#####################################################################################################
'''
題目描述:
-- 對輸入字符串檢查是否存在非法字符,輸出合法字符串(去重)和非法字符串(不去重)
-- 對合法字符串循環左移10次,在進行排序輸出。(舉例:比如字符串"abc",循環左移一次的結果為"bca")
輸入描述:
(1) 字符串中的字符集合為 '0'-'9','a'-'z','A'-'Z',其余為非法字符串(空字符串作為定界符),
有非法字符的字符串被視為非法輸入;
(2) 作為輸入的字符串個數不超過100,每個字符串長度不超過64;
(3) 作為輸入的連續空字符串(空格/制表符/回車/換行符)作為一個空格處理(作為定界符,字符串起始字符不能為空);
(4) 輸入每行只有一個字符串
(5) 輸入以空行結束
輸出描述:
(1) 輸出合法字符串並去重
(2) 輸出所有非法字符串
(3) 對結果1的去重合法字符串循環左移10次
(4) 對結果3合法字符串字符串排序,按ASCII表字符從小到大順序排序
注意事項:
-- 每輸入一個字符后用空格跟下一個字符串隔離,作為輸出的所有字符串之間只能有一個空格(作為定界符);
示例1:
-- 輸入
abc
def
==
acd123
44234tjg
aga'-=
ad--s
abd
123
abcdef
1234567890123456789012345678901234567890123
45678901234567890123
EDFG
SDFG
ABC
DEF
cccc
a*b=1
dd
87&&^
asdfas
234abc35
765rgfh4sd
1231
123
==
EDFG
-- 輸出
abc def acd123 44234tjg abd 123 abcdef 1234
5678901234567890123456789012345678901234567
8901234567890123 EDFG SDFG ABC DEF cccc dd
asdfas 234abc35 765rgfh4sd 1231
== aga'-= as--s a*b=1 87&&^ ==
bca efd 23acd1 234tjg44 bda 231 efabcd 1234
5678901234567890123456789012345678901234567
8901231234567890 FGED FGSD BCA EFD cccc dd
asasdf 4abc3523 765rgfh4sd 3112
1234567890123456789012345678901234567890123
45678901231234567890 231 234tjg44 23acd1 31
12 4abc3523 765rgfh4sd BCA EFD FGED FGSD as
asdf bca bda cccc dd efabcd efd
'''
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
-
一些技巧 ✅
#####################################################################################################
#####################################################################################################
'''
NOTE:
# 注意輸入時strip()、split()用法
# extend() 函數用於在列表末尾一次性追加另一個序列中的多個值(用新列表擴展原來的列表)
# ord('a') 返回字符a的ASCII碼
# index = [m.start() for m in re.finditer(' ',x)] 返回輸入字符串中空格所在索引位置
# 字符串去重時,由於需要刪除列表新加入的元素,而remove()只能移除列表中第一個匹配的元素,因此需要找到需去重的字符串索引
# 用pop(index),從后往前的彈出。由於在此期間存儲字符串的列表長度在動態改變,而for循環不能動態改變數組長度,因此用while
# split(str="",num=string.count(str)) 函數
# str_test = 'This\t\t is a\t\t\t test for split()'
# 輸入:str_test.split() # 默認分割(刪除)所有的空字符,包括空格、換行(\n)、制表符(\t)等
# 輸出:['This', 'is', 'a', 'test', 'for', 'split()']
# 輸入:str_test.split('s') # 分割所有的字符s
# 輸出:['Thi', '\t\t i', ' a\t\t\t te', 't for ', 'plit()']
# 輸入:str_test.split('s',2) # 分割前2個字符s
# 輸出:['Thi', '\t\t i', ' a\t\t\t test for split()']
'''12345678910111213141516171819
-
具體代碼 🔎
import sys
# 初始化輸入
def input_init():
string_list = []
while True:
line = sys.stdin.readline().rstrip('\n') # 逐行讀入,並去除行末的換行符
if 0 == len(line): # 輸入以空行結束,break語句較強應放在 continue語句前,不然會陷入死循環
break
if len(line) > 64: # 每個字符串長度不超過64
continue
if len(string_list) > 100-1: # 輸入字符串個數不超過100
continue
if (line.startswith(' ')) & (0 != len(line)): # 輸入字符串不能以空格開始
continue
temp_str = line.split() # split(),默認分割(刪除)所有的空字符,包括空格、換行(\n)、制表符(\t)等
string_list.append(' '.join(temp_str)) # 輸入的連續空字符串(空格/制表符/回車/換行符)作為一個空格處理
return string_list
# 保存合法字符串
def get_legal_string(string_list: list):
number_ls = list("0123456789")
letter_ls = list("abcdefghijklmnopqrstuvwxyz")
up_letter_ls = []
for letter in letter_ls:
up_letter_ls.append(letter.upper())
flag = int(0)
legal_str = []
for index in range(0, len(string_list)):
temp_str = string_list[index]
for ix in range(0, len(temp_str)):
x = temp_str[ix]
if (x in number_ls) | (x in letter_ls) | (x in up_letter_ls):
# 合法字符串
flag = 1
else:
flag = 0
break
if flag:
legal_str.append(temp_str)
return legal_str
# 去除列表中重復的字符串
def remove_repeat_string(string_list: list):
remove_repeated_str = string_list.copy()
ix = 0
while True:
temp_str = remove_repeated_str[ix]
count = remove_repeated_str.count(temp_str) # 統計重復字符串個數
if ix == len(remove_repeated_str)-1:
break
if count == 1:
ix = ix + 1
continue
while count > 1: # for循環不能動態改變數組長度,因此用while
count = count - 1
j = 1
while