Quiz 12: Regular Expressions | Python for Everybody 配套练习_解题记录

文章目录

  • Python for Everybody
  • 课程简介
    • Regular Expressions
    • 单选题(1-8)
    • 操作题
      • Regular Expressions


Python for Everybody


课程简介

Python for Everybody 零基础程序设计(Python 入门)

  • This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
  • We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
  • The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
  • This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
  • This course covers Python 3.

在这里插入图片描述

coursera

Python for Everybody 零基础程序设计(Python 入门)

Charles Russell Severance
Clinical Professor

在这里插入图片描述

个人主页
Twitter

在这里插入图片描述

University of Michigan


课程资源

coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站

PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网



Regular Expressions

Regular Expressions allow us to search for patterns in strings and extract data from strings using the regular expression programming language.


单选题(1-8)

  1. What character do you add to the “+” or “*” to indicate that the match is to be done in a non-greedy manner?
  • ^
  • ++
  • ?
  • g
  • $
  • **
  1. What will the ‘$’ regular expression match?
  • An empty line
  • The end of a line
  • The beginning of a line
  • A dollar sign
  • A new line at the end of a line
  1. What would the following mean in a regular expression? [a-z0-9]
  • Match anything but a lowercase letter or digit
  • Match a lowercase letter or a digit
  • Match any number of lowercase letters followed by any number of digits
  • Match any text that is surrounded by square braces
  • Match an entire line as long as it is lowercase letters or digits
  1. What is the type of the return value of the re.findall() method?
  • A string
  • A boolean
  • A list of strings
  • A single character
  • An integer
  1. What is the “wild card” character in a regular expression (i.e., the character that matches any character)?
  • +
  • ^
  • $
  • ?
  • *
  • .
  1. What is the difference between the “+” and “*” character in regular expressions?
  • The “+” matches upper case characters and the “*” matches lowercase characters
  • The “+” matches the actual plus character and the “*” matches any character
  • The “+” matches at least one character and the “*” matches zero or more characters
  • The “+” matches the beginning of a line and the “*” matches the end of a line
  • The “+” indicates “start of extraction” and the “*” indicates the “end of extraction”
  1. What does the “[0-9]+” match in a regular expression?
  • Several digits followed by a plus sign
  • Any mathematical expression
  • One or more digits
  • Zero or more digits
  • Any number of digits at the beginning of a line
  1. What does the following Python sequence print out?
x = 'From: Using the : character'
y = re.findall('^F.+:', x)
print(y)
  • :
  • [‘From:’]
  • ^F.+:
  • From:
  • [‘From: Using the :’]

操作题

Regular Expressions

Finding Numbers in a Haystack

In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers.

Data Files
We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.

  • Sample data: http://py4e-data.dr-chuck.net/regex_sum_42.txt
    (There are 90 values with a sum=445833)
  • Actual data: http://py4e-data.dr-chuck.net/regex_sum_1577741.txt
    (There are 78 values and the sum ends with 783)

These links open in a new window. Make sure to save the file into the same folder as you will be writing your Python program. Note: Each student will have a distinct data file for the assignment - so only use your own data file for analysis.

Data Format
The file contains much of the text from the introduction of the textbook except that random numbers are inserted throughout the text. Here is a sample of the output you might see:

Why should you learn to write programs? 7746
12 1929 8827
Writing programs (or programming) is a very creative 
7 and rewarding activity.  You can write programs for 
many reasons, ranging from making your living to solving
8837 a difficult data analysis problem to having fun to helping 128
someone else solve a problem.  This book assumes that 
everyone needs to know how to program ...

The sum for the sample text above is 27486. The numbers can appear anywhere in the line. There can be any number of numbers in each line (including none).

Handling The Data
The basic outline of this problem is to read the file, look for integers using the re.findall(), looking for a regular expression of ‘[0-9]+’ and then converting the extracted strings to integers and summing up the integers.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/3599.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用Nginx的反向代理来访问服务器例子——Nginx笔记

因为网站上的视频加载过慢,想使用nginx服务器实现HLS视频播放服务。顺便记录一下通过Nginx的方向代理来访问服务器。这里在原先的项目上进行改造。原先的项目已经部署在公网,使用tomcat服务器,可以直接用地址进行访问。 1.这里使用的8080端口…

springboot详细整合mybatisplus

SpringBoot详细整合mybatisPlus 文章目录 SpringBoot详细整合mybatisPlus一、引入mybatis_plus依赖二、修改mybatis_plus的yml配置三、添加mybatis_plus的其他配置以及包扫描四,修改mybatis的配置(这一步根据实际情况修改) 无奈,一…

stm32读取DHT11温湿度传感器

stm32读取DHT11温湿度传感器 一.序言二.DHT11响应数据格式三.DHT11通讯过程3.1 产生起始信号3.2 读取数据03.3 读取数据1DHT11停止信号 四.代码实例4.1读取DHT11源文件4.2 读取DHT11头文件 五.结语5.1 总结整体思路5.2 对读者的期望 一.序言 我们知道DHT11是单总线协议&#x…

Harris和Shi-tomasi角点检测笔记(详细推导)

角点 一般来说,角点就是极值点,在某些属性上强度最大或者最小的孤立点、线段的终点或拐点等。其实理解角点可以按照我们的直觉来理解,以下图为例,图中用颜色标注的地方都是角点: 原图地址:理解经典角点检测…

数据结构--栈(Stack)的基本概念

数据结构–栈(Stack)的基本概念 线性表是具有相同数据类型的n ( n ≥ 0 n\ge0 n≥0)个数据元素的有限序列,其中n为表长,当n 0时线性表是一个空表。若用L命名线性表,则其一般表示为: L ( a 1 , a 2 . . . , a i , a i 1 , . . …

Unity编辑器开发——特性(常用特性、自定义特性)

特性在Unity开发中是非常好用的。 常用特性: 下面记录下我常用的C#预置的特性: 一、程序集级别 二、脚本级别 三、脚本成员级别 1.字段 (1)Int/Float (2)string (3)Enum (4)List (5)Range(滑动条,其实也是对Int/Float) …

【SpringBoot】一、SpringBoot3新特性与改变详细分析

前言 本文适合具有springboot的基础的同学。 SpringBoot3改变&新特性 一、前置条件二、自动配置包位置变化1、Springboot2.X2、Springboot3.X 三、jakata api迁移1、Springboot2.X2、Springboot3.X3、SpringBoot3使用druid有问题,因为它引用的是旧的包 四 新特…

Redis 缓存数据库双写不一致怎么处理?

一、概述: Redis 缓存数据库可能会出现双写不一致的情况,这是因为在进行缓存更新时,同时有多个线程或进程对同一个缓存键进行读写操作,导致了数据的不一致性。 具体来说,假设有两个线程 A 和 B 都要对同一个缓存键进…

chatgpt赋能python:下载Python的方法及使用指南

下载Python的方法及使用指南 Python是一种高级编程语言,被广泛应用于各种领域。如果你是一名程序员或者对编程有兴趣,那么学习Python会是一个不错的选择。本文将介绍Python的下载方法,并提供使用Python的基础指南。 Python的下载方法 Pyth…

贪心算法详解

一.贪心算法详解 一、什么是贪心算法?二、贪心算法的应用场景三、使用Java代码实现贪心算法四、总结 前言 1.贪心算法(Greedy Algorithm)是一种经典的解题思路,它通过每一步的局部最优解,来达到全局最优解的目的。 贪心…

windows10 Linux子系统 Ubuntu 文件互相访问

ubuntu 访问Windows windows的磁盘被挂载到了/mnt下,可以看到我的电脑的c,d,e,f盘, windows 访问 ubuntu 在文件夹输入\wsl$ 再点击Ubuntu-22.04,进入文件夹

将mp4视频推流rtsp,并转为http直播流,在前端显示

最近有个需求,在vue页面的video组件播放直播流,本来想用flv.js,但是必须要flv格式才行,所以还是用原生video播放http直播流。 1. 将本地mp4推流rtsp 下载并解压EasyDarwin,双击EasyDarwin.exe运行,在控制…