题库高效练习

简介: 题库高效练习

 文章目录

📚前言

📰题目一:毕业生就业调查

📓题目要求

📓我的分析

📓我的答案

📰题目二:梦想的大学

📓题目要求

📓我的分析

📓我的答案

📰题目三: 拯救被污染的字符串

📓题目要求

📓我的分析

📓我的答案

📰题目四: 游乐园的门票

📓题目要求

📓我的分析

📓我的答案

📰题目五:栈和队列的实现

📓题目要求

📓我的分析

📓我的答案

📚结语


📰题目一:毕业生就业调查

📓题目要求

image.gif编辑

📓我的分析

本题难度不大,我的思路是将survey_list中每一个元素通过字典的get()方法,判断其是否在字典result_dict中。get方法具体使用如下:

描述

Python 字典 get() 函数返回指定键的值。

语法

dict.get(key[, value])
image.gif

参数

    • 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)
    image.gif

    输出结果

    image.gif编辑

    📰题目二:梦想的大学

    📓题目要求

    image.gif编辑

    示例

    输入:
    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!
    image.gif

    📓我的分析

    本题主要考察的是列表,字典,循环和内置函数sorted()的使用方法,跟着题目要求来应该可以很快得出答案,其中各种方法的具体使用方法如下

    👉内置函数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]))
    image.gif

    其中我的代码最后一段:

    print("I'am %s. I'd like to go to %s if I have the chance!" % (name,survey_dict[name]))
    image.gif

    换成以下代码也是很好的:

    print(f"I'am {name}. I'd like to go to {survey_dict[name]} if I have the chance!")
    image.gif

    📰题目三: 拯救被污染的字符串

    📓题目要求

    image.gif编辑

    📓我的分析

    本题题目知识点考察比较单一,主要是让我们复习一下字符串中join和split方法的使用,如果能够第一时间想到使用这些方法那么这题是很快可以写出,下面就来复习join和split方法的使用:

    描述

    Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

    语法

    str.join(sequence)
    image.gif

    参数

      • sequence -- 要连接的元素序列。

      返回值

      返回通过指定字符连接序列中元素后生成的新字符串。

      👉字符串中split方法的使用

      📓我的答案

      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)
      image.gif

      📰题目四: 游乐园的门票

      📓题目要求

      image.gif编辑

      示例:

      输入:
      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.
      image.gif

      📓我的分析

      本题主要考察了循环语句的使用方法和数据类型的强制转换方法,考点主要在"不同身高的人需购买不同的门票",我们这里使用" 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.')
      image.gif

      📰题目五:栈和队列的实现

      📓题目要求

      image.gif

      示例

      输入:
      5
      4
      3
      2
      1
      输出:
      [5, 4]
      [5, 4]
      [5]
      [4]
      [5, 3, 2, 1]
      [4, 3, 2, 1]
      1 2 3 5
      image.gif

      📓我的分析

      本题主要考察的是我们对栈和队列放值和取值的顺序的考察

      👉栈(先进来的后出去):是一种线性数据结构,用先进后出或者是后进先出的方式存储数据,栈中数据的插入删除操作都是在栈顶端进行

      👉队列(先进来的先出去):队列是一种只允许在一端进行插入操作,而在另一端进行删除操作的线性表

      📓我的答案

        • 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()


        相关文章
        |
        8天前
        |
        JavaScript 前端开发 小程序
        CoderGuide 程序员前后端面试题库,打造全网最高质量题库
        CoderGuide涵盖范围包括且不限于:前端面试题(Vue,React,JS,HTTP,HTML,CSS面试题等),后端面试题(Java,Python,Golang,PHP,Linux,Mysql面试题等),以及算法面试题,大厂面试题,高频面试题,校招面试题等,你想要的,这里都有!
        15 2
        |
        2月前
        |
        存储 算法 Java
        JAVA后端开发面试题库
        JAVA后端开发面试题库
        35 1
        |
        12月前
        |
        缓存 前端开发 JavaScript
        前端面试基础题库——1
        前端面试基础题库——1
        139 0
        |
        3月前
        |
        存储 算法 索引
        【数据结构入门精讲 | 第四篇】考研408、企业面试表专项习题
        【数据结构入门精讲 | 第四篇】考研408、企业面试表专项习题
        80 0
        |
        3月前
        |
        存储 搜索推荐 算法
        【数据结构入门精讲 | 第九篇】考研408排序算法专项练习(一)
        【数据结构入门精讲 | 第九篇】考研408排序算法专项练习(一)
        113 0
        |
        3月前
        |
        存储 算法
        【数据结构入门精讲 | 第十八篇】考研408、企业面试图专项练习(一)
        【数据结构入门精讲 | 第十八篇】考研408、企业面试图专项练习(一)
        33 0
        |
        3月前
        |
        设计模式 前端开发 JavaScript
        前端 面试题库
        前端 面试题库
        |
        3月前
        |
        前端开发 JavaScript 小程序
        前端入门指南前端面试题库
        前端入门指南前端面试题库
        |
        12月前
        |
        缓存 移动开发 网络协议
        前端面试基础题库——关于底层原理的面试题
        前端面试基础题库——关于底层原理的面试题
        121 0
        前端面试基础题库——关于底层原理的面试题
        |
        12月前
        |
        前端开发 JavaScript 安全
        前端面试基础题库——4
        前端面试基础题库——4