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

news/2024/11/25 18:32:47/

文章目录

  • 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.ppmy.cn/news/694460.html

相关文章

Redis高可用(主从复制、哨兵模式和Cluster集群)

文章目录 一、Redis高可用1.持久化2.主从复制3.哨兵4.Cluster集群 二、主从复制1.概念2.作用3.主从复制流程4.配置主从复制 三、哨兵模式1.功能2.作用3.组成4.故障转移机制7.故障模拟8.恢复故障节点 四、Cluster群集1.简介2.作用(1)数据分区(…

Java tcomm 例子_使用CICSAPI和ECI.ppt

开发(2) – 使用CICS API和ECI 刘睿 概要 TXSeries编程框架 TXSeries/CICS客户机程序 TXSeries服务器程序 TXSeries编程框架 基于TXSeries的三层应用开发环境 TXSeries的应用开发支持框架 交易(Transaction),任务(Task)和逻辑处理单元(LUW)的概念 交易(Transaction)…

ECI支持大规格实例

信息摘要: ECI支持大规格实例 适用客户: 基因计算及其他需要大规格计算资源的场景 版本/规格功能: 为满足基因计算及其他需要大规格计算资源的场景,ECI 推出了指定大规格实例,每个容器实例可使用规格见下表。 vCPU 内存…

ECI支持大规格实例

信息摘要: ECI支持大规格实例 适用客户: 基因计算及其他需要大规格计算资源的场景 版本/规格功能: 为满足基因计算及其他需要大规格计算资源的场景,ECI 推出了指定大规格实例,每个容器实例可使用规格见下表。 vCPU 内存…

ecilipes配置python_Python坐标转换ECI到ECEF

我正在使用sgp4 python包(由Brandon Rhodes开发);我正在为ISS(Zarya)输入TLE信息。我在某个日期,在ECI坐标系中获得卫星的位置。但是,我想将其转换为ECEF坐标系。 至少有两个实现了matlab函数的实例(ecitoecef,eci2ecef)和一个为RTKLIB项目实…

脑壳疼,好好的系统,为什么要分库分表?

大家好,我是小富~ 说在前边 今天是《分库分表 ShardingSphere 原理与实战》系列的开篇文章,之前写过几篇关于分库分表的文章反响都还不错,到现在公众号:程序员小富后台不断的有人留言、咨询分库分表的问题,我也没想到…

云起实验室:基于ECI的ACK集群高弹性架构实践

本场景介绍基于ECI的ACK集群高弹性架构。该架构中,在ACK集群上运行常规业务,当业务突发波动(如秒杀活动)时,让突增的业务运行在ECI实例上,随着业务波动动态创建/释放ECI实例,达到成本的最优控制…

阿里云弹性容器实例产品 ECI ——云原生时代的基础设施

阿里云弹性容器实例产品 ECI ——云原生时代的基础设施 1. 什么是 ECI 弹性容器实例 ECI (Elastic Container Instance) 是阿里云在云原生时代为用户提供的基础计算服务,是阿里云云原生时代下的云计算基础设施。ECI 改变了以往计算服务以整台机器作为交付形态的传…