Lecture 18: Lists II. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork
|
|
- Georgia Sherman
- 5 years ago
- Views:
Transcription
1 Lecture 18: Lists II CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork
2 Summary More on Python s lists. Sorting and reversing. List comprehensions. 1
3 Some useful list functions lst.sort() lst.reverse() Rearrange elements within s in increasing order. Reverse the order of the elements within s. [4, 2, 1, 3].sort() ==> [1, 2, 3, 4] [2, 2, 1, 2].sort() ==> [1, 2, 2, 2] [1, 2, 3, 4].reverse() ==> [4, 3, 2, 1] NB: Both re-arrange elements of lst in place. 2
4 sort vs sorted Suppose x = [3, 6, 2, 1, 5, 4] sort Rearranges elements in place; returns None sorted leaves original untouched; returns copy of x in increasing order Syntax x after val. returned x.sort() [1, 2, 3, 4, 5, 6] None sorted(x) [3, 6, 2, 1, 5, 4] [1, 2, 3, 4, 5, 6] For back-to-front copy of list x, list(reversed(x)) 3
5 Palindromes def is list palindrome ( lst ): # Return True if list ' lst ' reads the same # left to right as it does right to left. mirror = lst.copy() mirror. reverse () return lst == mirror Note: function copy makes a (shallow) copy of a list 4
6 Palindromes cont d def is palindrome ( text ): # Return True if string ' text ' is a palindrome.q lst = list (text) return is list palindrome ( lst ) Note: function list transform string into list of its constituent characters. For example list("python") ==> ["P", "y", "t", "h", "o", "n"] 5
7 Some more list functions lst.insert(i, x) Insert new element with value x at index i. lst.pop(i) Remove the ithe element and return its value lst.remove(x) Remove the first (from left) occurrence of value x within s. [2, 3, 5, 7].insert(0, 20) ==> [20, 2, 3, 5, 7] [2, 3, 5, 7].insert(2, 30) ==> [2, 3, 30, 5, 7] [2, 3, 5, 7].pop(1) ==> [2, 5, 7] (returns 3) [2, 3, 5, 7].pop(3) ==> [2, 3, 5] (returns 7) [3, 1, 4, 1, 5, 9].remove(1) ==> [3, 4, 1, 5, 9] 6
8 Example: anagrams Write a Python function that checks whether two words are anagrams of each other (same number of each letter occurring) Example: "silent", "listen" "monty", "python" ==> True ==> False 7
9 Example: anagrams Write a Python function that checks whether two words are anagrams of each other (same number of each letter occurring) Example: "silent", "listen" "monty", "python" ==> True ==> False def are anagrams(word1, word2): # Return True if 'word1' and 'word2' are anagrams i.e # contain the same letters in the same multiplicity. lst1 = list (word1.upper()) lst2 = list (word2.upper()) lst1. sort () lst2. sort () return lst1 == lst2 7
10 List comprehensions
11 List comprehensions Create a list of the squares of the first few integers Doing it the straightforward way: squares = [] for n in range(10): squares.append(n 2) 8
12 List comprehensions Create a list of the squares of the first few integers Doing it the straightforward way: squares = [] for n in range(10): squares.append(n 2) Using a list comprehension: squares = [n 2 for n in range(10)] 8
13 List comprehensions with filters Create a list of the odd squares of the first few integers: odd squares = [n 2 for n in range(10) if n % 2 == 1] This is equivalent to odd squares = [] for n in range(10): if n % 2 == 1: odd squares.append(n 2) 9
14 Example: acronyms Write a Python function that generates an acronym for a phrase by taking the first letter of each word Example: "Random access memory" ==> "RAM" 10
15 Example: acronyms Write a Python function that generates an acronym for a phrase by taking the first letter of each word Example: "Random access memory" ==> "RAM" def get acronym(phrase): # Return a acronym for 'phrase' words = phrase. split () initials = [w[0].upper() for w in words] return. join ( initials ) 10
16 Calculating most common value in a list def most common(lst): # Return the most commonly occurring value in list ' lst ' # ( ties broken arbitrarily ). List must be non empty. multiplicities = [ lst. count(elem) for elem in lst ] largest = max( multiplicities ) return lst [ multiplicities. index( largest )] 11
17 Calculating most common value in a list lst: [1, 2, 3, 1, 4, 1] Count multiplicity (number of appearances) of each element in list multiplicities = [ lst. count(elem) for elem in lst ] Determine the largest multiplicity largest = max( multiplicities ) Return the corresponding list element return lst [ multiplicities. index( largest )] multiplicities: [3, 1, 1, 3, 1, 3] largest: 3 index is 0 lst[...] ==> 1 12
18 What does this do? print(,. join (reversed([n for n in input(). split ()]))) 13
19 What does this do? print(,. join (reversed([n for n in input(). split ()]))) Admittedly a bit cryptic, but demonstrates Python s power. 13
20 Dictionary comprehensions 14
21 Back Material
22 Notes and Acknowledgements Reading Code Acknowledgements 15
Lecture 11: while loops CS1068+ Introductory Programming in Python. for loop revisited. while loop. Summary. Dr Kieran T. Herley
Lecture 11: while loops CS1068+ Introductory Programming in Python Dr Kieran T. Herley Python s while loop. Summary Department of Computer Science University College Cork 2017-2018 KH (24/10/17) Lecture
More informationLecture 8: Simple Calculator Application
Lecture 8: Simple Calculator Application Postfix Calculator Dr Kieran T. Herley Department of Computer Science University College Cork 2016/17 KH (27/02/17) Lecture 8: Simple Calculator Application 2016/17
More informationLecture 15: Dictionaries
Lecture 15: Dictionaries CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork Summary Python s dictionary concept. 1 Dictionaries
More informationLecture 10: Strings. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork
Lecture 10: Strings CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork Summary 1 Python text: data type str String sequences of
More informationCS205: Scalable Software Systems
CS205: Scalable Software Systems Lecture 3 September 5, 2016 Lecture 3 CS205: Scalable Software Systems September 5, 2016 1 / 19 Table of contents 1 Quick Recap 2 Type of recursive solutions 3 Translating
More informationLecture 2: Writing Your Own Class Definition
Lecture 2: Writing Your Own Class Definition CS6507 Python Programming and Data Science Applications Dr Kieran T. Herley 2017-2018 Department of Computer Science University College Cork Summary Writing
More informationLecture 4: Simple Input-Calculate-Output Programs
Lecture 4: Simple Input-Calculate-Output Programs CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2017/18 Department of Computer Science University College Cork input Statement Python s
More informationLecture 2: Python Arithmetic
Lecture 2: Python Arithmetic CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork Basic data types in Python Python data types Programs
More informationIntroduction to programming using Python
Introduction to programming using Python Matthieu Choplin matthieu.choplin@city.ac.uk http://moodle.city.ac.uk/ Session 4 1 Objectives To come back on the notion of object and type. To introduce to the
More informationEXTERNAL SORTING. CS 564- Spring ACKs: Dan Suciu, Jignesh Patel, AnHai Doan
EXTERNAL SORTING CS 564- Spring 2018 ACKs: Dan Suciu, Jignesh Patel, AnHai Doan WHAT IS THIS LECTURE ABOUT? I/O aware algorithms for sorting External merge a primitive for sorting External merge-sort basic
More informationCS1 Lecture 12 Feb. 11, 2019
CS1 Lecture 12 Feb. 11, 2019 HW4 available tomorrow, due next Wed. Discussion sections this week will be closely tied to one of the homework problems. Exam 1, Thursday evening, 2/21, 6:30-8:00pm HW2 scores
More informationLecture 7: Functions. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork
Lecture 7: Functions CS1068+ Introductory Programming in Python Dr Kieran T. Herley 2018/19 Department of Computer Science University College Cork Summary Functions in Python. Terminology and execution.
More informationLecture 10: Boolean Expressions
Lecture 10: Boolean Expressions CS1068+ Introductory Programming in Python Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (12/10/17) Lecture 10: Boolean Expressions
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 38: Icon Exercises Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2004 Christian Collberg [1] Expressions
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 36 : Icon Exercises Christian Collberg Department of Computer Science University of Arizona collberg+372@gmail.com Copyright c 2005 Christian Collberg November
More informationLecture 5: Regular Expression and Finite Automata
Lecture 5: Regular Expression and Finite Automata Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (28/09/17) Lecture 5: Regular Expression and Finite Automata 2017-2018
More informationCS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1
CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1 Goals of this tutorial You should be able to... understand
More informationAlastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python
Outline Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer Universität des Saarlandes October 6th, 2009 Outline Outline Today s Topics: 1 More Examples 2 Cool Stuff 3 Text Processing
More informationLISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University
LISTS WITH PYTHON José M. Garrido Department of Computer Science May 2015 College of Computing and Software Engineering Kennesaw State University c 2015, J. M. Garrido Lists with Python 2 Lists with Python
More informationCSI33 Data Structures
Department of Mathematics and Computer Science Bronx Community College Outline Chapter 5: Stacks and 1 Chapter 5: Stacks and Chapter 5: Stacks and A Queue ADT A Container Class for First-In-First-Out Access
More informationCS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013
CS61A Lecture 16 Amir Kamil UC Berkeley February 27, 2013 Announcements HW5 due tonight Trends project due on Tuesday Partners are required; find one in lab or on Piazza Will not work in IDLE New bug submission
More informationLists CS 1111 Introduction to Programming Fall 2018
Lists CS 1111 Introduction to Programming Fall 2018 [The Coder s Apprentice, 12] 1 Overview: Lists List = ordered sequence of values Mutable data type Because of the ordering, an element in a list can
More informationCS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013
CS61A Lecture 16 Amir Kamil UC Berkeley February 27, 2013 Announcements HW5 due tonight Trends project due on Tuesday Partners are required; find one in lab or on Piazza Will not work in IDLE New bug submission
More informationCS 1110: Introduction to Computing Using Python Loop Invariants
CS 1110: Introduction to Computing Using Python Lecture 21 Loop Invariants [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements Prelim 2 conflicts due by midnight tonight Lab 11 is out Due
More informationWorking with Lists 4
CS 61A Lecture 10 Announcements Lists ['Demo'] Working with Lists 4 Working with Lists >>> digits = [1, 8, 2, 8] 4 Working with Lists >>> digits = [1, 8, 2, 8] >>> digits = [2//2, 2+2+2+2, 2, 2*2*2] 4
More informationLecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions
Lecture 4: Applications CS2504/CS4092 Algorithms and Linear Data Structures Dr Kieran T. Herley Department of Computer Science University College Cork Summary. Postfix notation for arithmetic expressions.
More informationRandom Numbers; Object-Oriented Programming 11/14/2017
Random Numbers; Object-Oriented Programming 11/14/2017 1 Announcements PS 5 is ready Today: Random Numbers; Object-Oriented Programming Reading: Chapter 7 of Conery Acknowledgement: Some of this lecture
More informationArrays. Friday, November 9, Department of Computer Science Wellesley College
Arrays Friday, November 9, CS Computer Programming Department of Computer Science Wellesley College Arrays: Motivation o o o Lists are great for representing a collection of values, but can only access
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions
More informationThe Practice of Computing Using PYTHON
The Practice of Computing Using PYTHON William Punch Richard Enbody Chapter 6 Lists and Tuples 1 Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures 2 Data Structures
More informationLecture 11. Lecture 11: External Sorting
Lecture 11 Lecture 11: External Sorting Lecture 11 Announcements 1. Midterm Review: This Friday! 2. Project Part #2 is out. Implement CLOCK! 3. Midterm Material: Everything up to Buffer management. 1.
More informationCIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani
CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvania) CIS 192 Fall Lecture 2 September 6, 2017 1 / 34
More informationLecture 4: Basic I/O
Lecture 4: Basic I/O CS1068+ Introductory Programming in Python Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (21/09/17) Lecture 4: Basic I/O 2017-2018 1 / 20
More informationLecture 2: SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases. Brief Note on Naming Conventions. Our Running Example.
Lecture 2: SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases Dr Kieran T. Herley Summary Review of relation model. Simple SELECT-FROM and SIMPLE-FROM-WHERE queries. SQL s operators.
More informationPython Tutorial. CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi
Python Tutorial CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi 1 Python2 vs Python3 Python syntax Data structures Functions Debugging Classes The NumPy Library Outline 2 Many examples adapted from
More informationCSC148H Week 3. Sadia Sharmin. May 24, /20
CSC148H Week 3 Sadia Sharmin May 24, 2017 1/20 Client vs. Developer I For the first couple of weeks, we have played the role of class designer I However, you are also often in the opposite role: when a
More informationSimple Lexical Analyzer
Lecture 7: Simple Lexical Analyzer Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (03/10/17) Lecture 7: Simple Lexical Analyzer 2017-2018 1 / 1 Summary Use of jflex
More informationCS F-11 B-Trees 1
CS673-2016F-11 B-Trees 1 11-0: Binary Search Trees Binary Tree data structure All values in left subtree< value stored in root All values in the right subtree>value stored in root 11-1: Generalizing BSTs
More informationLists, loops and decisions
Caltech/LEAD Summer 2012 Computer Science Lecture 4: July 11, 2012 Lists, loops and decisions Lists Today Looping with the for statement Making decisions with the if statement Lists A list is a sequence
More informationPython Lists and for Loops. Learning Outcomes. What s a List 9/19/2012
Python Lists and for Loops CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17 1 Learning Outcomes Be aware that multiple items can be stored in a list. Become
More informationMUTABLE LISTS AND DICTIONARIES 4
MUTABLE LISTS AND DICTIONARIES 4 COMPUTER SCIENCE 61A Sept. 24, 2012 1 Lists Lists are similar to tuples: the order of the data matters, their indices start at 0. The big difference is that lists are mutable
More informationLecture 2: SQL Basics
Lecture 2: SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (19/09/17) Lecture 2: SQL Basics
More informationUniversity of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013
University of Washington CSE 140 Data Programming Winter 2013 Final exam March 11, 2013 Name: Section: UW Net ID (username): This exam is closed book, closed notes. You have 50 minutes to complete it.
More informationCS150 - Sample Final
CS150 - Sample Final Name: Honor code: You may use the following material on this exam: The final exam cheat sheet which I have provided The matlab basics handout (without any additional notes) Up to two
More informationAnnouncements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.
Announcements Project 5 is on the street. Second part is essay questions for CoS teaming requirements. The first part you do as a team The CoS essay gets individually answered and has separate submission
More informationProgramming in Python
Programming in Python Michael Schroeder Sebas0an Salen0n Lecture 2: Sequences Slides derived from Ian Holmes, Department of Sta0s0cs, University of Oxford 1 Updates by Andreas Henschel Summary: Scalars
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 04 / 25 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Balanced Binary Search trees AVL trees / Compression Uses binary trees Balanced
More informationDepartment of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,
More informationTopic 7: Lists, Dictionaries and Strings
Topic 7: Lists, Dictionaries and Strings The human animal differs from the lesser primates in his passion for lists of Ten Best H. Allen Smith 1 Textbook Strongly Recommended Exercises The Python Workbook:
More informationCIS192 Python Programming
CIS192 Python Programming Data Types Joseph Cappadona University of Pennsylvania September 03, 2015 Joseph Cappadona (University of Pennsylvania) CIS 192 September 03, 2015 1 / 32 Outline 1 Data Types
More informationModule 08: Searching and Sorting Algorithms
Module 08: Searching and Sorting Algorithms Topics: Searching algorithms Sorting algorithms 1 Application: Searching a list Suppose you have a list L. How could you determine if a particular value is in
More informationModule 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists
Module 04: Lists Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10 1 Consider the string method split >>> name = "Harry James Potter" >>> name.split() ['Harry',
More informationLecture 1: Relational Databases
Lecture 1: Relational Databases CS1106/CS5021/CS6503 Introduction to Relational Databases Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (12/09/17) Lecture 1: Relational
More informationCSC236H Lecture 5. October 17, 2018
CSC236H Lecture 5 October 17, 2018 Runtime of recursive programs def fact1(n): if n == 1: return 1 else: return n * fact1(n-1) (a) Base case: T (1) = c (constant amount of work) (b) Recursive call: T
More informationCS 323 Lecture 1. Design and Analysis of Algorithms. Hoeteck Wee
{ CS 323 Lecture 1 } Design and Analysis of Algorithms Hoeteck Wee hoeteck@cs.qc.cuny.edu http://cs323.qwriting.org/ Algorithmic ideas are pervasive APPLICATIONS. Economics, auctions and game theory Biology,
More informationProgramming Languages ML Programming Project Due 9/28/2001, 5:00 p.m.
Programming Languages ML Programming Project Due 9/28/2001, 5:00 p.m. Functional Suite Instead of writing a single ML program, you are going to write a suite of ML functions to complete various tasks.
More informationCS1 Lecture 11 Feb. 9, 2018
CS1 Lecture 11 Feb. 9, 2018 HW3 due Monday morning, 9:00am for #1 I don t care if you use 1, 2, or 3 loops. Most important is clear correct code for #3, make sure all legal situations are handled. Think
More informationChapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments
Chapter 6: List What we will learn: List definition Syntax for creating lists Selecting elements of a list Selecting subsequence of a list What you need to know before: Data types Assignments List Sub-list
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2008S-19 B-Trees David Galles Department of Computer Science University of San Francisco 19-0: Indexing Operations: Add an element Remove an element Find an element,
More informationCMPSCI 105: Lecture #12 Searching, Sorting, Joins, and Indexing PART #1: SEARCHING AND SORTING. Linear Search. Binary Search.
CMPSCI 105: Lecture #12 Searching, Sorting, Joins, and Indexing PART #1: SEARCHING AND SORTING Linear Search Binary Search Items can be in any order, Have to examine first record, then second record, then
More informationIntroduction to Computer Programming for Non-Majors
Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 11 Part 1 Instructor: Long Ma The Department of Computer Science Chapter 11 Data Collections Objectives: To understand the
More informationList Processing Patterns and List Comprehension
List Processing Patterns and Review: Lists Summary of what we know about lists. A list is a sequence type (like strings and tuples), but that differently from them is mutable (it can change). Lists can
More informationLecture 3: More SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases. Single-Criterion WHERE Conditions. SQL Conditions.
Lecture 3: More SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases Dr Kieran T. Herley Summary Composite conditions using AND, OR and NOT. The IN operator. Ordering results. Renaming
More informationIntroduction to Python
Introduction to Python Reading assignment: Perkovic text, Ch. 1 and 2.1-2.5 Python Python is an interactive language. Java or C++: compile, run Also, a main function or method Python: type expressions
More informationCS Lecture 19: Loop invariants
CS 1110 Lecture 19: Loop invariants Announcements Prelim 2 conflicts Today (April 2) is two weeks before the prelim, and the deadline for submitting prelim conflicts. Instructor travel This week and the
More informationCS264: Homework #1. Due by midnight on Thursday, January 19, 2017
CS264: Homework #1 Due by midnight on Thursday, January 19, 2017 Instructions: (1) Form a group of 1-3 students. You should turn in only one write-up for your entire group. See the course site for submission
More informationCS150 Sample Final. Name: Section: A / B
CS150 Sample Final Name: Section: A / B Date: Start time: End time: Honor Code: Signature: This exam is closed book, closed notes, closed computer, closed calculator, etc. You may only use (1) the final
More informationTUPLES AND RECURSIVE LISTS 5
TUPLES AND RECURSIVE LISTS 5 COMPUTER SCIENCE 61A July 3, 2012 1 Sequences From the Pig project, we discovered the utility of having structures that contain multiple values. Today, we are going to cover
More informationDownload the forcomp.zip handout archive file and extract it somewhere on your machine.
Anagrams: Instructions When you're ready to submit your solution, go to the assignments list. Download the forcomp.zip handout archive file and extract it somewhere on your machine. In this assignment,
More informationSequence Types FEB
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
More informationF28PL1 Programming Languages. Lecture 14: Standard ML 4
F28PL1 Programming Languages Lecture 14: Standard ML 4 Polymorphic list operations length of list base case: [] ==> 0 recursion case: (h::t) => 1 more than length of t - fun length [] = 0 length (_::t)
More informationCS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:
CS112 Spring 2012 Dr. Kinga Dobolyi Exam 2 Do not open this exam until you are told. Read these instructions: 1. This is a closed book exam. No calculators, notes, or other aids are allowed. If you have
More informationShell CSCE 314 TAMU. Haskell Functions
1 CSCE 314: Programming Languages Dr. Dylan Shell Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions can
More informationCS 11 python track: lecture 3. n Today: Useful coding idioms
CS 11 python track: lecture 3 Today: Useful coding idioms Useful coding idioms "Idiom" Standard ways of accomplishing a common task Using standard idioms won't make your code more correct, but more concise
More informationDecorate, sort, undecorate pattern
Decorate, sort, undecorate pattern In a previous example, we read a file that contained data about Words with Friends games between me and my sister. Suppose we want to read this data again, and this time
More informationCS205: Scalable Software Systems
CS205: Scalable Software Systems Lecture 4 September 14, 2016 Lecture 4 CS205: Scalable Software Systems September 14, 2016 1 / 16 Quick Recap Things covered so far Problem solving by recursive decomposition
More informationList Processing Patterns and List Comprehension
List Processing Patterns and List Comprehension Review: Lists Summary of what we know about lists. A list is a sequence type (like strings and tuples), but that differently from them is mutable (it can
More informationLecture 3: More SQL Basics
Lecture 3: More SQL Basics CS1106/CS5021/CS6503 Introduction to Relational Databases Dr Kieran T. Herley Department of Computer Science University College Cork 2018/19 KH (11/09/18) Lecture 3: More SQL
More informationLecture I: Shortest Path Algorithms
Lecture I: Shortest Path Algorithms Dr Kieran T. Herley Department of Computer Science University College Cork October 201 KH (21/10/1) Lecture I: Shortest Path Algorithms October 201 1 / 28 Background
More informationIntroduction to Computer Programming for Non-Majors
Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 11 Part 1 The Department of Computer Science Objectives Chapter 11 Data Collections To understand the use of lists (arrays)
More informationLecture 2: Getting Started
Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,
More informationLecture 8: Context Free Grammars
Lecture 8: Context Free s Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (12/10/17) Lecture 8: Context Free s 2017-2018 1 / 1 Specifying Non-Regular Languages Recall
More informationPractice Question. Omar Khan / Atif Alvi. October 10, 2016
Practice Question Omar Khan / Atif Alvi October 10, 2016 1 Chapter 1 Loops 1.1 While Loop Iterating through a string 1. Print the first 10 integers 1 i = 0 2 n = 10 3 while i
More informationName CPTR246 Spring '17 (100 total points) Exam 3
Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in
More informationDM502 Programming A. Peter Schneider-Kamp.
DM502 Programming A Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm502/! PROJECT PART 1 2 Organizational Details 2 possible projects, each consisting of 2 parts for 1 st part,
More informationLecture 4: SQL Data Manipulation Basics
Lecture 4: SQL Data Manipulation Basics Data Manipulation Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (26/09/17) Lecture 4: SQL Data Manipulation Basics 2017-2018
More informationPython review. 1 Python basics. References. CS 234 Naomi Nishimura
Python review CS 234 Naomi Nishimura The sections below indicate Python material, the degree to which it will be used in the course, and various resources you can use to review the material. You are not
More informationCS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community
CS-141 Final Exam Review December 8, 2017 Presented by the RIT Computer Science Community http://csc.cs.rit.edu Python Basics 1. Although most in the industry will be reasonably forgiving when it comes
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College September 18, 2017 Outline Outline 1 Chapter 4: Linked Structures and Iterators Outline 1 Chapter 4: Linked Structures and
More informationProgramming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.
Plan for the rest of the semester: Programming We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. We saw earlier that computers
More informationPython Lists, Tuples, Dictionaries, and Loops
Python Lists, Tuples, Dictionaries, and Loops What you need to Know For this lecture you need to know: 1. How to write and run basic python programs 2. How to create and assign data to variables 3. How
More informationLab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018
CS18 Integrated Introduction to Computer Science Fisler, Nelson Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018 Contents 1 Heapsort 2 2 Quicksort 2 3 Bubble Sort 3 4 Merge Sort 3 5 Mirror Mirror
More informationMath236 Discrete Maths with Applications
Math236 Discrete Maths with Applications P. Ittmann UKZN, Pietermaritzburg Semester 1, 2012 Ittmann (UKZN PMB) Math236 2012 1 / 19 Degree Sequences Let G be a graph with vertex set V (G) = {v 1, v 2, v
More informationModule 8: Binary trees
Module 8: Binary trees Readings: HtDP, Section 14 We will cover the ideas in the text using different examples and different terminology. The readings are still important as an additional source of examples.
More informationWorking with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).
Working with recursion Readings: HtDP, sections 11, 12, 13 (Intermezzo 2). We can extend the idea of a self-referential definition to defining the natural numbers, which leads to the use of recursion in
More informationCS 307 Final Fall 2009
Points off 1 2 3 4 5 6 Total off Net Score CS 307 Final Fall 2009 Name UTEID login name Instructions: 1. Please turn off your cell phones. 2. There are 6 questions on this test. 3. You have 3 hours to
More informationWorking with recursion
Working with recursion Readings: HtDP, sections 11, 12, 13 (Intermezzo 2). We can extend the idea of a self-referential definition to defining the natural numbers, which leads to the use of recursion in
More informationCS-141 Final Exam Review May 16, 2015 Presented by the RIT Computer Science Community
CS-141 Final Exam Review May 16, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu Python Basics 1. Although most in the industry will be reasonably forgiving when it comes to
More informationUniversity of Texas at Arlington, TX, USA
Dept. of Computer Science and Engineering University of Texas at Arlington, TX, USA Part of the science in computer science is the design and use of data structures and algorithms. As you go on in CS,
More information15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.
UNIT 5B Binary Search 1 Binary Search Required: List L of n unique elements. The elements must be sorted in increasing order. Result: The index of a specific element (called the key) or None if the key
More information