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.

你可能感兴趣的:(#,Python,for,Everybody,python,开发语言,学习,笔记)