Handling String of python Version of the Enrollment and Writing Test for 2019 School

  • 2021-06-29 11:44:09
  • OfStack

Huahua 2019 online pen test, now organized as follows for future reference
GitHub

Title introduction


#################################################################
#################################################################
'''
题目描述:
-- 对输入字符串检查是否存在非法字符,输出合法字符串(去重)和非法字符串(不去重)
-- 对合法字符串循环左移10次,在进行排序输出。(举例:比如字符串"abc",循环左移1次的结果为"bca")
输入描述:
(1) 字符串中的字符集合为 '0'-'9','a'-'z','A'-'Z',其余为非法字符串(空字符串作为定界符),
 有非法字符的字符串被视为非法输入;
(2) 作为输入的字符串个数不超过100,每个字符串长度不超过64;
(3) 作为输入的连续空字符串(空格/制表符/回车/换行符)作为1个空格处理(作为定界符,字符串起始字符不能为空);
(4) 输入每行只有1个字符串
(5) 输入以空行结束
输出描述:
(1) 输出合法字符串并去重
(2) 输出所有非法字符串
(3) 对结果1的去重合法字符串循环左移10次
(4) 对结果3合法字符串字符串排序,按ASCII表字符从小到大顺序排序
注意事项:
-- 每输入1个字符后用空格跟下1个字符串隔离,作为输出的所有字符串之间只能有1个空格(作为定界符);
示例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
'''

1 Tips


##################################################################
##################################################################
'''
NOTE:
#  Notice when typing strip() , split() usage 
# extend()  Function is used at the end of the list 1 Secondary Addition 1 Multiple values in a sequence (expanding the original list with a new list) 
# ord('a')  Return Character a Of ASCII code 
# index = [m.start() for m in re.finditer(' ',x)]  Returns the index position of the space in the input string 
#  When strings are de-duplicated, new elements added to the list need to be deleted, and remove() Only the first item in the list can be removed 1 Matching elements, so we need to find the string index to be de-duplicated 
#  use pop(index) And pop back and forth.Because the list length of stored strings changes dynamically during this time, and for Loops do not dynamically change the length of arrays, so use while

# split(str="",num=string.count(str))  function 
# str_test = 'This\t\t is a\t\t\t test for split()'
#  Input: str_test.split()  #  Default Split ( delete ) All empty characters, including spaces, line breaks (\n) , Tabs (\t) etc. 
#  Output: ['This', 'is', 'a', 'test', 'for', 'split()']
#  Input: str_test.split('s') #  Split all characters s
#  Output: ['Thi', '\t\t i', ' a\t\t\t te', 't for ', 'plit()']
#  Input: str_test.split('s',2) #  Before Split 2 Characters s
#  Output: ['Thi', '\t\t i', ' a\t\t\t test for split()']
'''

Specific code


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))  # 输入的连续空字符串(空格/制表符/回车/换行符)作为1个空格处理
 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 True:
  need_remove = remove_repeated_str[-j]  # 反序遍历
  if temp_str == need_remove:
   #remove_repeated_str.remove(need_remove) # 因为remove()只能移除列表中第1个匹配的元素
   pop_index = len(remove_repeated_str) - j
   remove_repeated_str.pop(pop_index)  # 删除指定索引位置元素(反序)
   break
  else:
   j = j + 1
 return remove_repeated_str


# 保存非法字符串
def get_non_legal_string(raw_string_list: list, legal_string: list):
 non_legal_str = []
 for i in raw_string_list:
 if i in legal_string:
  continue
 non_legal_str.append(i)
 return non_legal_str


# 左移10次字符 10%len(str)
def shift_string(string_list: list):
 shift_string = []
 for shift_str in string_list:
 start = 10 % len(shift_str)
 shift_temp = ""
 shift_temp += shift_str[start:]
 shift_temp += shift_str[:start]
 shift_string.append(shift_temp)
 return shift_string


# 输出字符串结果
def output_string(string_list: list):
 output = ""
 for str_ in string_list:
 output += str_ + " "
 print(output)


def main():
 # 原始输入
 str_list = input_init()
 # 保存合法字符串
 legal_str = get_legal_string(str_list)
 # 保存非法字符串
 non_legal_str = get_non_legal_string(raw_string_list=str_list, legal_string=legal_str)
 # 保存合法字符串_去重
 remove_repeated_string = remove_repeat_string(legal_str)
 # 1.输出去重合法字符串
 output_string(remove_repeated_string)
 # 2.输出未去重的非法字符串
 output_string(non_legal_str)
 # 3.输出去重合法字符串左移10次后的结果
 shift_legal_str = shift_string(remove_repeated_string)
 output_string(shift_legal_str)
 # 4.输出对合法字符串字符串左移后排序,按ASCII表字符从小到大顺序排序
 shift_legal_str = sorted(shift_legal_str)
 output_string(shift_legal_str)


if __name__ == '__main__':
 main()

Related articles: