文章目录
📚前言
📚结语
📰题目一:毕业生就业调查
📓题目要求
编辑
📓我的分析
本题难度不大,我的思路是将survey_list中每一个元素通过字典的get()方法,判断其是否在字典result_dict中。get方法具体使用如下:
描述
Python 字典 get() 函数返回指定键的值。
语法
dict.get(key[, value])
参数
- key -- 字典中要查找的键。
- value -- 可选,如果指定键的值不存在时,返回该默认值。
返回值
返回指定键的值,如果键不在字典中返回默认值 None 或者设置的默认值。
📓我的答案
survey_list = ['Niumei','Niu Ke Le','GURR','LOLO'] result_dict = {'Niumei': 'Nowcoder','GURR': 'HUAWEI'} for people in survey_list: if result_dict.get(people) == None: #判断列表中的人是否在字典中 print('Hi, %s! Could you take part in our graduation survey?' % people) else: print('Hi, %s! Thank you for participating in our graduation survey!' % people)
输出结果
编辑
📰题目二:梦想的大学
📓题目要求
编辑
示例
输入: Tom Fudan University Yes Ben Peking University Yes Andy Tsinghua University No 输出: If you have the chance, which university do you want to go to most? What is your name? Which university do you want to go to most? Is there anyone who hasn't been investigated yet? If you have the chance, which university do you want to go to most? What is your name? Which university do you want to go to most? Is there anyone who hasn't been investigated yet? If you have the chance, which university do you want to go to most? What is your name? Which university do you want to go to most? Is there anyone who hasn't been investigated yet? I'am Andy. I'd like to go to Tsinghua University if I have the chance! I'am Ben. I'd like to go to Peking University if I have the chance! I'am Tom. I'd like to go to Fudan University if I have the chance!
📓我的分析
本题主要考察的是列表,字典,循环和内置函数sorted()的使用方法,跟着题目要求来应该可以很快得出答案,其中各种方法的具体使用方法如下
📓我的答案
survey_dict = {} while 1: print('If you have the chance, which university do you want to go to most?') print('What is your name?') name = input() print('Which university do you want to go to most?') university = input() survey_dict[name] = university print("Is there anyone who hasn't been investigated yet?") res = input() if res == 'No': break for name in sorted(survey_dict.keys()): print("I'am %s. I'd like to go to %s if I have the chance!" % (name,survey_dict[name]))
其中我的代码最后一段:
print("I'am %s. I'd like to go to %s if I have the chance!" % (name,survey_dict[name]))
换成以下代码也是很好的:
print(f"I'am {name}. I'd like to go to {survey_dict[name]} if I have the chance!")
📰题目三: 拯救被污染的字符串
📓题目要求
编辑
📓我的分析
本题题目知识点考察比较单一,主要是让我们复习一下字符串中join和split方法的使用,如果能够第一时间想到使用这些方法那么这题是很快可以写出,下面就来复习join和split方法的使用:
描述
Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
语法
str.join(sequence)
参数
- sequence -- 要连接的元素序列。
返回值
返回通过指定字符连接序列中元素后生成的新字符串。
📓我的答案
my_str = 'I am$ N$iuniu$, an$d I $am stu$dying in $Now$coder!' my_list = my_str.split('$') print(my_list) new_str = ''.join(my_list) print(new_str)
📰题目四: 游乐园的门票
📓题目要求
编辑
示例:
输入: 0.5 1.2 quit 输出: Please tell me your height! Enter 'quit' to end the program. Your admission cost is 0 yuan. Please tell me your height! Enter 'quit' to end the program. Your admission cost is 80 yuan. Please tell me your height! Enter 'quit' to end the program.
📓我的分析
本题主要考察了循环语句的使用方法和数据类型的强制转换方法,考点主要在"不同身高的人需购买不同的门票",我们这里使用" if - elif - else"语句会相对简便
📓我的答案
while 1: print("Please tell me your height!\nEnter 'quit' to end the program.") read = input() #用read变量接收我们读入的身高 if read == 'quit': break if float(read) < 1.0: print('Your admission cost is 0 yuan.') elif float(read) < 1.2: print('Your admission cost is 80 yuan.') else: print('Your admission cost is 150 yuan.')
📰题目五:栈和队列的实现
📓题目要求
示例
输入: 5 4 3 2 1 输出: [5, 4] [5, 4] [5] [4] [5, 3, 2, 1] [4, 3, 2, 1] 1 2 3 5
📓我的分析
本题主要考察的是我们对栈和队列放值和取值的顺序的考察
👉栈(先进来的后出去):是一种线性数据结构,用先进后出或者是后进先出的方式存储数据,栈中数据的插入删除操作都是在栈顶端进行
👉队列(先进来的先出去):队列是一种只允许在一端进行插入操作,而在另一端进行删除操作的线性表
📓我的答案
- lst1用来实现栈的列表,lst2用来实现队列的列表
- lst_append()函数是用来向这两个列表中添加元素
- lst_print()函数是用来打印这两个列表
lst1 = [] lst2 = [] def lst_append(n): for i in range(n): num = int(input()) lst1.append(num) lst2.append(num) def lst_print(): print(lst1) print(lst2) lst_append(2) lst_print() lst1.pop() lst2.pop(0) lst_print() lst_append(3) lst_print() while len(lst1): print(lst1[-1], end=' ') lst1.pop()