+ All Categories
Home > Documents > ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection...

ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection...

Date post: 08-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
22
ENGR/CS 101 CS Session Lecture 12 Log into Windows/ACENET (reboot if in Linux) Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f14-courses/cs101.html Right-click on lecture12.py link. Save link/target to folder where your other CS 101 programs are. Right-click on numbers.dat link. Save link/target to same folder. Start Python, open lecture12.py file Lecture 12 ENGR/CS 101 Computer Science Session 1
Transcript
Page 1: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

ENGR/CS 101 CS Session

Lecture 12

Log into Windows/ACENET (reboot if in Linux)

Use web browser to go to session webpage http://csserver.evansville.edu/~hwang/f14-courses/cs101.html

Right-click on lecture12.py link. Save link/target

to folder where your other CS 101 programs are.

Right-click on numbers.dat link. Save link/target

to same folder.

Start Python, open lecture12.py file

Lecture 12 ENGR/CS 101 Computer Science Session 1

Page 2: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Outline

Questions about Homework 2?

Arrays

Python lists

Problem: Finding the range of a collection of

numbers

Finding the maximum value

Finding the minimum value

Lecture 12 ENGR/CS 101 Computer Science Session 2

Page 3: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Storing Collections

Last class we discussed searching and

sorting algorithms. Today we will discuss

how the data storage might be arranged.

First, we want the data to be stored in a

collection that is arranged to allow us easy

access to each element.

Also, we want to be able to identify an

element of the collection by using the position

(or index) of the element in the collection.Lecture 12 ENGR/CS 101 Computer Science Session 3

Page 4: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Arrays

The simplest collection in most programming

languages is the array.

Formally, an array is an ordered collection of

homogeneous elements that is accessed by

an index.

Lecture 12 ENGR/CS 101 Computer Science Session 4

Page 5: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Arrays

Ordered means that the relative position of

each element matters.

Homogeneous means all elements are of the

same type

Indexed means that we identify positions

using an integer.

Lecture 12 ENGR/CS 101 Computer Science Session 5

Page 6: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Arrays

To access an array element, you give the

array name and the index of the element.

Almost all programming languages (including

Python) use:

<name>[<index>]

This is a compound name that can be used

on either side of an assignment.

Lecture 12 ENGR/CS 101 Computer Science Session 6

Page 7: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Arrays

The indexes of an array usually start at 0 or

1. For technical reasons, most modern

programming languages (including Python)

start indexing at 0.

Here is a picture of an array of 10 integers:

Lecture 12 ENGR/CS 101 Computer Science Session 7

4523 3976 875 9216 6354anArray

[1][0] [3][2] [5][4] [7][6] [9][8]

Page 8: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Python Lists

In Python, the basic collection is called a list.

Like an array, a Python list is ordered and

indexed. Unlike an array, the elements are

heterogeneous. I.e., they may be of

different types.

Lecture 12 ENGR/CS 101 Computer Science Session 8

Page 9: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Python Lists

A list is written as comma-delimited values

enclosed in square brackets. A list with no

elements is called the empty list.>>> mylist = [3, "cat", 4.5, True]

>>> mylist

[3, "cat", 4.5, True]

>>> mylist[2]

4.5

>>> mylist[2] = 6.5

>>> mylist

[3, "cat", 6.5, True]

Lecture 12 ENGR/CS 101 Computer Science Session 9

Page 10: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Python Lists

Python lists have a number of built-in

operations including

+ , concatenate two lists together

* , concatenate a repeated number of times

len() , function that returns the number of

elements in the list

[<startIdx>:<endIdx>] , slicing operation for

extracting a part of a list starting at index <startIdx> and ending at index <endIdx>-1.

Lecture 12 ENGR/CS 101 Computer Science Session 10

Page 11: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Python Lists

Here are some examples>>> mylist+mylist

[1, 'cat', 6.5, True, 1, 'cat', 6.5, True]

>>> mylist*3

[1, 'cat', 6.5, True, 1, 'cat', 6.5, True,

1, 'cat', 6.5, True]

>>> len(mylist)

4

>>> len (mylist*4)

16

>>> mylist[1:2]

['cat'] Lecture 12 ENGR/CS 101 Computer Science Session 11

Page 12: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Python Lists

It turns out that Python also has built-in ways of searching a list (in operation) and sorting

a list (sort method).

>>> mylist = [12, 4, 63, 36, 28]

>>> 36 in mylist

True

>>> mylist.sort()

>>> mylist

[4, 12, 28, 36, 63]

Lecture 12 ENGR/CS 101 Computer Science Session 12

Page 13: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Problem: Finding the Range of

a Collection of Numbers

Dispersion is a measure of how spread out a

collection of data values are. The simplest

measure is to find the range of the data

values, which is the difference between the

maximum and minimum values of the

collection.

We would like a program that given a file of

numbers, computes and displays the range of

the numbers stored in the file.

Lecture 12 ENGR/CS 101 Computer Science Session 13

Page 14: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Main Program Design

Design

1. Create an empty list

2. Ask the user for the name of a data file

3. Open the file for reading

4. For each line in the input file

a. Convert the line to an integer

b. Append the integer to the list

5. Compute and display the range of the data in

the list

Lecture 12 ENGR/CS 101 Computer Science Session 14

Page 15: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

In-class Program

lecture12.py is the start of program we

will use to show how to compute various

statistical quantities on a collection of random

numbers.

The provided code handles asking the user

for a file name, opening the file and

accessing each line. (Steps 2-4)

The pass statement is used as a placeholder for

code bodies. This allows the file to be compiled.

Lecture 12 ENGR/CS 101 Computer Science Session 15

Page 16: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Creating a Python List

In order to compute statistics on the data, we

need to store the data into a list.

First we create an empty list.

items = []

Then in the loop, we convert each line into an

integer

number = int(line)

And we append each number to the list

items.append(number)Lecture 12 ENGR/CS 101 Computer Science Session 16

Page 17: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Finding the Maximum Value

The first thing we need is to find the

maximum value in the list. We will write a

function that does this.

Analysis

Received: list to be searched

Returned: maximum value

Lecture 12 ENGR/CS 101 Computer Science Session 17

Page 18: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

Finding the Maximum Value

The basic idea is to go through the list

keeping track of the current maximum value.

Start by assuming the first element is the

maximum value.

Use a for-loop and an if-statement to check

the rest of the elements.

Lecture 12 ENGR/CS 101 Computer Science Session 18

Page 19: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

findMax Function Code

def findMax (aList):

# assume first item is max

maxSoFar = aList[0]

# look at the rest of the items

for item in aList[1:]:

if item > maxSoFar:

# a new max

maxSoFar = item

return maxSoFar

Lecture 12 ENGR/CS 101 Computer Science Session 19

Page 20: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

In-class Exercise

In the main program, write a function call to findMax using the items list and display

the result. Run the program and make sure

you are getting the correct answer, then

delete this code.

Write a function findMin that finds and

returns the minimum value in a received list.

Hint: how does this differ from finding the

maximum value?

Lecture 12 ENGR/CS 101 Computer Science Session 20

Page 21: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

In-class Exercise

In the main program, write a function call to findMin using the items list and display the

result. Again, make sure you are getting the

correct answer, then delete the code.

Write a function computeRange that receives

a list, computes the range of the values using the findMax and findMin functions, and

returns the range of the values.

Lecture 12 ENGR/CS 101 Computer Science Session 21

Page 22: ENGR/CS 101 Computer Science Sessionmr63/Courses/Engr...Problem: Finding the Range of a Collection of Numbers Dispersion is a measure of how spread out a collection of data values

In-class Exercise

Finish the main program by calling computeRange and displaying the range of

the data values.

Lecture 12 ENGR/CS 101 Computer Science Session 22


Recommended