Sequence Types FEB

Similar documents
22C:16 CS:1210 Exam 2

CPSC 217 Midterm (Python 3 version)

CMSC201 Computer Science I for Majors

Python I. Some material adapted from Upenn cmpe391 slides and other sources

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Teaching London Computing

Programming Training. Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving

[301] JSON. Tyler Caraza-Harter

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

CSI33 Data Structures

Topic 7: Lists, Dictionaries and Strings

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Module 08: Searching and Sorting Algorithms

Python and Bioinformatics. Pierre Parutto

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Statements 2. a operator= b a = a operator b

CPSC 217 L01 Midterm

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Introduction to Python. Data Structures

CMSC201 Computer Science I for Majors

Binary Search APRIL 25 TH, 2014

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Introduction to Python

Lecture #12: Immutable and Mutable Data. Last modified: Mon Feb 22 16:33: CS61A: Lecture #12 1

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Final Exam Practice Questions

Visualize ComplexCities

CS 115 Exam 3, Spring 2014

Language Reference Manual

Data Structures. Lists, Tuples, Sets, Dictionaries

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

18.1. CS 102 Unit 18. Python. Mark Redekopp

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1

Computational Programming with Python

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Programming Training. Arrays / Lists Traversal. Input a List. Main Points: - Working with strings. - Working with Files - Problem Solving.

Overview of List Syntax

Types, lists & functions

PYTHON- AN INNOVATION

EZ- ASCII: Language Reference Manual

COMP1730/COMP6730 Programming for Scientists. Strings

Using Lists (Arrays) Notes

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

CMSC201 Computer Science I for Majors

Introduction to programming using Python

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CMSC201 Computer Science I for Majors

CS 1301 CS1 with Robots Summer 2007 Exam 1

QUark Language Reference Manual

Question 1. Part (a) Part (b) December 2013 Final Examination Marking Scheme CSC 108 H1F. [13 marks] [4 marks] Consider this program:

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited

Python: Short Overview and Recap

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Introduction to Python! Lecture 2

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Script language: Python Data structures

Babu Madhav Institute of Information Technology, UTU 2015

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

MEIN 50010: Python Flow Control

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Key Differences Between Python and Java

Advanced Python. Executive Summary, Session 1

Lists, loops and decisions

Python Intro GIS Week 1. Jake K. Carr

egrapher Language Reference Manual

MUTABLE LISTS AND DICTIONARIES 4

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS 2316 Exam 1 Spring 2014

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Introduction to Bioinformatics

MELODY. Language Reference Manual. Music Programming Language

Lecture #12: Mutable Data. Last modified: Sun Feb 19 17:15: CS61A: Lecture #12 1

The Big Python Guide

Python Mini Lessons last update: May 29, 2018

Python Lists. What is not a Collection. A List is a kind of Collection. friends = [ 'Joseph', 'Glenn', 'Sally' ]

CSC148 Fall 2017 Ramp Up Session Reference

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Practical Questions CSCA48 Week 6

Introduction to Problem Solving and Programming in Python.

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Begin to code with Python Obtaining MTA qualification expanded notes

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

CS61A Lecture 15. Amir Kamil UC Berkeley February 25, 2013

lina Documentation Release Author

CMSC201 Computer Science I for Majors

Notebook. March 30, 2019

Python: common syntax

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

DINO. Language Reference Manual. Author: Manu Jain

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Control of Flow. There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests.

ENGR/CS 101 CS Session Lecture 12

Lessons on Python Functions

Introduction to Computer Programming for Non-Majors

Transcription:

Sequence Types FEB 23-25 2015

What we have not learned so far How to store, organize, and access large amounts of data? Examples: Read a sequence of million numbers and output these in sorted order. Read a text, correct all spelling errors in the text, and output the corrected text. Programming languages typically provide tools and techniques to store and organize data. In Python we can use sequence types to do this.

Strings and Lists are examples of Sequence Types A string is a sequence of characters enclosed in quotes. Examples: hello, 8.397, 7, 34 (The quotes can be single or double quotes) A list is a sequence of objects enclosed in square brackets. Examples: [0, 1, 2, 3], [ Alice, Bob, Catherine ], [ hello, 4.567, True, bye ] (Objects of different types can be part of the same list) Lists are more general than strings; strings can be viewed as special instances of lists.

Accessing items in lists and strings L = [ hi, 10, bye, 100, -20, 123, 176, 3.45, 1, it ] hi 10 bye 100-20 123 176 3.45 1 it 0 1 2 3 4 5 6 7 8 9 One of the most useful features of sequence types is that elements in a sequence can be accessed efficiently and conveniently using their position in the sequence. This type of access is called random access. It refers to the fact that the amount of time to access an element via its index is independent of the value of the index or the size of the sequence. Example: L[0] is hi, L[1] is 10, L[2] is bye,, L[9] is it

An example with lists and strings Example: L = [ hello, Pavan, [22, 0], 15] L[0][4] = o L[1][0] = 22 L[0][1] = e L[2] = 15

The len function Python has a built-in function len(l) that returns the length, i.e., the number of elements, in list L. len(s) returns the length of a string s. Examples: len([]) is 0, len([34, 12, 45]) is 3, len( hello ) = 5. Thus the elements of a list L are indexed from 0 through len(l)-1. This simple observation is quite useful in iterating through a list.

Example 1: Iterating through a list This program walks through the list, printing each element. The program uses the positions of the elements to index into the list. L = ["hi", 109, "go", 111, 1.16, [122,30], "hello"] i = 0 while i < len(l): print(l[i]) i = i + 1

Example 2: Testing membership in a list # tests if a given element is a member of a given list. # Returns True if element is a member; False otherwise. def ismember(l, elem): i = 0 # i serves as the index into list L # Iterate through the elements of the list # comparing each of them with elem while i < len(l): if elem == L[i]: return True i = i + 1 return False

The in operator The ismember function is rendered useless by the Python in operator. The in operator is used as x in L, where x is an object and L is a list. This expression evaluates to True if x is an element in L; evaluates to False otherwise. Examples: 67 in [34, 12, 45] evaluates to False hi in [] evaluates to False, etc. This works on strings as well. Examples: hi in history evaluates to True ei in piece evaluates to False ace in Wallace evaluates to True

Example 3: Finding location of an element # searches for a given element in a given list and # returns the index of the first occurrence of the # element, if it is present in the list. Otherwise, # returns -1. def search(l, elem): i = 0 # i serves as the index into list L # Iterate through the elements of the list # comparing each of them with elem while i < len(l): if elem == L[i]: return i i = i + 1 return -1

The + operator on lists Just like we use the + operator for string concatenation, we can use the + operator for concatenating lists. Examples: [10, 20, 30] + [10, 13] evaluates to [10, 20, 30, 10, 13] [10, 20, 30] + [ hello ] evaluates to [10, 20, 30, hello ] [10, 20, 30] + [] evaluates to [10, 20, 30] In the following code, L is enlarged by the addition of three elements 10, 20, and 30 at the end of the list: L = L + [10, 20, 30]

The * operator on lists The * operator can be used on lists (or strings) to make larger lists (or strings) obtained by concatenating many copies of a list (or string). Example: If L = [2, 3, 7], then L*2 (or 2*L) evaluates to [2, 3, 7, 2, 3, 7]. Example: If s = cashew, then 2*s (or s*2) evaluates to cashewcashew.

Programming Problem 4 Read a file containing some number of nonnegative integers and output the number of distinct integers in the file. There is no specific format to the file there could be several integers in a line or none, consecutive integers are separated by one or more white spaces (blanks, tabs, returns).

Example Input File (test.txt) 23 78 4567 123 789 230 1236765 78798 6768 678 678 78

Algorithm 1. masterlist = [] 2. Read a line of the file as a string. 3. Parse the line to extract a list numbersinline of integers from the line. 4. Walk through list numbersinline and for each element in numbersinline, not in masterlist, add it to masterlist. 5. Go back to Line (2), if there are more lines to process. 6. Output the length of masterlist.

Main Program # Open a file called test.txt for read only and read the first line f = open("test.txt", "r") line = f.readline() masterlist = [] # keeps track of the list of distinct integers in the file # Process each line, if line is non-empty while line!= : # Parse the line to extract a list of numbers in the line numbersinline = parse(line) # Extend the masterlist by appending to it all the new # numbers in the line. masterlist = uniqueextend(masterlist, numbersinline) # Read the next line line = f.readline() f.close() print(len(masterlist))

The function uniqueextend # Takes two lists L1 and L2 and returns the list obtained # by appending to L1, all elements in L2 that are not in L1 def uniqueextend(l1, L2): index = 0 # serves as index into list L2 # Loop to walk through elements of L2 while index < len(l2): # If current element of L2 is not in L1, then append it if not(l2[index] in L1): L1 = L1 + [L2[index]] index = index + 1 return L1

The function parse # Takes a string consisting of non-negative integers and # returns a list containing all the integers in the line. # The integers in the line are separated by 1 or more blanks. def parse(s): listofnumbers = [] # maintains the list of numbers in strings s currentnumber = "" # The function oscillates between two states: in one state # it is processing the digits of an integer and the other state # it is processing the white spaces between consecutive integers. # The boolean variable numberbeingprocessed is used to keep track # of this state. numberbeingprocessed = False i = 0 # serves as an index into the string s while i < len(s): # if the current character is a digit if s[i] >= "0" and s[i] <= "9": numberbeingprocessed = True currentnumber = currentnumber + s[i] # else if the current character is a non-digit # immediately following a number elif numberbeingprocessed: listofnumbers = listofnumbers + [int(currentnumber)] numberbeingprocessed = False currentnumber = 0 i = i + 1 return listofnumbers