CSC 231: Introduction to Data Structures Dr. Curry Guinn.

Slides:



Advertisements
Similar presentations
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
CIS-5301 Introduction to Python Edward Loper. CIS-5302 Outline Data –strings, variables, lists, dictionaries Control Flow Working with files Modules Functions.
Introduction to Python
Recitation 1 Programming for Engineers in Python.
Fundamentals of Python: From First Programs Through Data Structures
The University of Texas – Pan American
Fundamentals of Python: First Programs
Euromasters SS Trevor Cohn Introduction to Python 1 Euromasters summer school 2005 Introduction to Python Trevor Cohn July 11, 2005.
CH Programming An introduction to programming concepts.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Introduction. 2COMPSCI Computer Science Fundamentals.
CSC – 332 Data Structures Dr. Curry Guinn. Quick Info Dr. Curry Guinn –CIS 2045 – –
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Strings in Python. Computers store text as strings GATTACA >>> s = "GATTACA" s Each of these are characters.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Let’s get started!.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
CSC 231: Introduction to Data Structures Python and Objects – Day 3 Dr. Curry Guinn.
Introduction to python programming
String and Lists Dr. José M. Reyes Álamo.
Sorts, CompareTo Method and Strings
Fundamentals of Programming I Overview of Programming
CprE 185: Intro to Problem Solving (using C)
CS1022 Computer Programming & Principles
CSc 120 Introduction to Computer Programing II Adapted from slides by
CSC 131: Introduction to Computer Science
Python Let’s get started!.
Introduction to Python
Introduction To Repetition The for loop
CSC 131: Introduction to Computer Science
Chapter 2 - Introduction to C Programming
Python: Control Structures
GC211Data Structure Lecture2 Sara Alhajjam.
CSC 131: Introduction to Computer Science
Computer Science 102 Data Structures CSCI-UA
Chapter 2 - Introduction to C Programming
Teach A level Computing: Algorithms and Data Structures
Learning to Program in Python
Introduction to Programming
CS190/295 Programming in Python for Life Sciences: Lecture 6
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
PHP.
Algorithm Discovery and Design
CS 100: Roadmap to Computing
Introduction to Programming
3 Control Statements:.
Chapter 3 – Control Structures
String and Lists Dr. José M. Reyes Álamo.
Computer Programming 1 introduction to JAVA Lecture 1 Instructor: Ruba A. Salamah Islamic University of Gaza.
Accelerated Introduction to Computer Science
Strings in Python.
Homework Reading Programming Assignments Finish K&R Chapter 1
Introduction to Python Strings in Python Strings.
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Introduction to Strings
Algorithmic complexity
Introduction to Programming
CSC 380: Design and Analysis of Algorithms
Introduction to C Programming
CS 100: Roadmap to Computing
Class code for pythonroom.com cchsp2cs
Introduction to Strings
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

CSC 231: Introduction to Data Structures Dr. Curry Guinn

Quick Info Dr. Curry Guinn –CIS 2045 – – –Office Hours: MWR: 11:00am-12:00pm and by appointment –Teaching Assistant: Paul Murray (hours TBD)

Today What is this course? The Course webpage/Syllabus How to get an ‘A’ What are Data Structures? Python review (variables, lists, conditionals, loops, input/output, functions)

What is CSC 231? Data Structures –THE Gateway Course Gateway to most 300 level CSC courses Gateway to our graduate CSC courses Not just a gateway course at UNCW – it is the pivotal course in all undergraduate computer science programs

Jobs When Google interviews, what do they ask?

Why Is It So Important? How you organize and access information on computers is fundamental Across disciplines within computer science the same sorts of operations arise again and again This course investigates the best practices of past scientists trying to solve those puzzles

How To Approach This Course Your future CSC courses and your future career in IT/Computers depends on mastering this material.

Let’s Visit the Course Webpage You’ll want to bookmark this page on your computer as you will visit it every day You can easily find it by going to my home page and clicking on the CSC 231 link on my main page 16/231/csc231.htmhttp://people.uncw.edu/guinnc/courses/Fall 16/231/csc231.htm –Note that the “Schedule” link near the top of the page is quite important

How Do I Earn an ‘A’? Read the book. Come to every lecture/lab. Write programs. –Turn in assignments on time. –Work independently –Late assignments lose 10 points per day Make use of office hours –Send me early and often.

Course and Grading Criteria Quizzes10% 1 st Midterm Exam15% 2 nd Midterm Exam15% Final35% Homework25% -10 pts. per day late up to 5 days. Then ‘F’ for the course

The Required Text Problem Solving with Algorithms and Data Structures using Python, by Miller and Ranum, second edition. Publisher: Franklin Beedle. This is available free online at /index.html /index.html All the downloads (source code, etc.)

PyCharm IDE PyCharm IDE - Requires Java and Python 3 to be installed on your machine. Follow the steps below. –Download and install the Java Development Kit (jdk), named Java Platform (JDK) 7u45 or something similar, from here.here. –Download and install Python from herehere –Download and install PyCharm from here.here. The Free Community Edition does not require a license –Configure PyCharm to use the Python 3 interpreter.

The Goal of the Course Understand and Implement Data Structures That Have Found to Be Important and Particularly Useful An example: Arrays (Python’s implementation of a list) Why are arrays useful?

Understanding Data Structures What can you do with arrays? How do you find something in an array? How do we know which algorithm for finding something in an array is “better”? What does “better” mean in this context? –Analysis of algorithms

Understanding Data Structures When are arrays bad? Which leads us to linked lists, queues, and stacks

Understanding Data Structures Which leads us to sorting Some sorting algorithms depend on recursion so we’ll need to study that too

Understanding Data Structures Linked lists are not so good for searching and sorting Which leads us to binary trees Are binary search trees always better than a linked list?

Understanding Data Structures Which leads us to studying how to balance binary trees Are there other useful tree structures?

Understanding Data Structures Linear (or sequential) search takes O(n) time Binary search takes log(n) time Hashing search takes 1 time!!! I wonder if there are any drawbacks?

The Basics of Python

The Lecture Portion of Today’s Python Review The Python Interpreter, IDEs Strings Variables Lists Conditionals Loops Input/output Functions

22 The Python Interpreter Interactive interface to Python A simple IDE that is included with Python is IDLE Enter an expression, and Python evaluates it: >>> 3*(7+2) 27

Non-interactive mode Python can also run on a file % python myfile.py 27 –Where the file 'myfile.py' contains the single line: print 3*(7+2) idle interactive GUI for interpreter, editing files and debugging PyCharm is an awesome IDE.

Strings A string is a sequence of letters (called characters). In Python, strings start and end with single or double quotes. >>> “foo” ‘foo’ >>> ‘foo’ ‘foo’

Defining strings Each string is stored in the computer’s memory as a list of characters. >>> myString = “GATTACA” myString

Accessing single characters You can access individual characters by using indices in square brackets. >>> myString = “GATTACA” >>> myString[0] ‘G’ >>> myString[1] ‘A’ >>> myString[-1] ‘A’ >>> myString[-2] ‘C’ >>> myString[7] Traceback (most recent call last): File " ", line 1, in ? IndexError: string index out of range Negative indices start at the end of the string and move left.

Accessing substrings >>> myString = “GATTACA” >>> myString[1:3] ‘AT’ >>> myString[:3] ‘GAT’ >>> myString[4:] ‘ACA’ >>> myString[3:5] ‘TA’ >>> myString[:] ‘GATTACA’

Special characters The backslash is used to introduce a special character. >>> "He said, "Wow!"" File " ", line 1 "He said, "Wow!"" ^ SyntaxError: invalid syntax >>> "He said, 'Wow!'" "He said, 'Wow!'" >>> "He said, \"Wow!\"" 'He said, "Wow!"' Escape sequence Meaning \\Backslash \’Single quote \”Double quote \nNewline \tTab

More string functionality >>> len(“GATTACA”) 7 >>> “GAT” + “TACA” ‘GATTACA’ >>> “A” * 10 ‘AAAAAAAAAA >>> “GAT” in “GATTACA” True >>> “AGT” in “GATTACA” False ←Length ←Concatenation ←Repeat ←Substring test

String methods In Python, a method is a function that is defined with respect to a particular object. The syntax is. ( ) >>> dna = “ACGT” >>> dna.find(“T”) 3

String methods >>> "GATTACA".find("ATT") 1 >>> "GATTACA".count("T") 2 >>> "GATTACA".lower() 'gattaca' >>> "gattaca".upper() 'GATTACA' >>> "GATTACA".replace("G", "U") 'UATTACA‘ >>> "GATTACA".replace("C", "U") 'GATTAUA' >>> "GATTACA".replace("AT", "**") 'G**TACA' >>> "GATTACA".startswith("G") True >>> "GATTACA".startswith("g") False

String summary Basic string operations: S = "AATTGG"# assignment - or use single quotes ' ' s1 + s2 # concatenate s2 * 3# repeat string s2[i]# index character at position 'i' s2[x:y]# index a substring len(S)# get length of string int(S) # or use float(S)# turn a string into an integer or floating point decimal Methods: S.upper() S.lower() S.count(substring) S.replace(old,new) S.find(substring) S.startswith(substring), S. endswith(substring) Printing: print(var1,var2,var3) # print multiple variables print("text",var1,"text“) # print a combination of explicit text (strings) and variables

33 Variables A variable is a name for a value. –Use "=" to assign values to variables. >>> first_name = 'John' >>> last_name = 'Smith' >>> first_name + ' ' + last_name 'John Smith' –Variable names are case sensitive –Variable names include only letters, numbers, and “_” –Variable names start with a letter or “_” –Any variable can hold any value (no typing)

34 Lists A list is an ordered set of values –Lists are written [elt 0, elt 1, …, elt n-1 ] >>> [1, 3, 8] >>> ['the', 'king', 'of', ['spain', 'france']] >>> [] >>> [1, 2, 'one', 'two'] –lst[i] is the i th element of lst. –Elements are indexed from zero >>> words = ['the', 'king', 'of', 'spain'] >>> words[0] 'the' >>> words[2] 'of'

35 Indexing Lists >>> determiners = ['a', 'b', 'c', ['d', 'e']] >>> determiners[0] # 0 th element 'a' >>> determiners[-2] # N-2 th element 'c' >>> determiners[-1][0] # sublist access 'd' >>> determiners[0:2] # elements in [0, 2) ['a', 'b'] >>> determiners[2:] # elements in [2, N) ['c', ['d', 'e']] [ 'a', 'b', 'c', ['d', 'e'] ]

36 Operations on Lists >>> determiners = ['the', 'an', 'a'] >>> len(determiners) 3 >>> determiners + ['some', 'one'] ['the', 'an', 'a', 'some', 'one'] >>> determiners ['the', 'an', 'a'] >>> determiners.index('a') 2 >>> [1, 1, 2, 1, 3, 4, 3, 6].count(1) 3

37 Operations on Lists 2: List Modification >>> determiners ['the', 'an', 'a'] >>> del determiners[2] # remove the element at 2 >>> determiners.append('every') # insert at the end of the list >>> determiners.insert(1, 'one') # insert at the given index >>> determiners ['the', 'one', 'an', 'every'] >>> determiners.sort() # sort alphabetically >>> determiners ['an', 'every', 'one', 'the'] >>> determiners.reverse() # reverse the order ['the', 'one', 'every', 'an']

38 Truth Values Every expression has a truth value 0 is False, all other numbers are True. “” is False, all other strings are True [] is False, all other lists are True >>> 5 == 3+2 # == tests for equality True >>> 5 != 3*2 # != tests for inequality True >>> 5 > 3*2 # >, =, <= test for ordering False >>> 5 > 3*2 or 5<3*2 # or, and combine truth values True

39 Control Flow ●if statement tests if a condition is true –If so, it executes a body –Otherwise, it does nothing >>> if len(determiners) > 3:... del determiners[3]... print 'deleted the determiner at index 3' –Indentation is used to mark the body. –Note the “:” at the end of the if line. body {

40 Control Flow 2 ●if-else statement >>> if len(sentence) > 3:... print sentence >>> else:... print 'too short' ●if-elif statement >>> if x<3:... print x*3 >>> elif x<6:... print x*2 >>> else:... print x

Control Flow 3 while statement >>> while x<1000:... x = x*x+3 for statement >>> for n in [1, 8, 12]:... print n*n+n range() >>> for n in range(0, 10):... print n*n

42 Control Flow 4 break statement –quits the current loop (for, while) >>> for x in range(0, 10):... print x... if x > 5:... break ●continue statement ● skips to the next iteration of the loop

43 Working with Files To read a file: >>> for line in open('corpus.txt', 'r').readlines()... print line To write to a file: >>> outfile = open('output.txt', 'w') >>> outfile.write(my_string) >>> outfile.close() Example: >>> outfile = open('output.txt', 'w') >>> for line in open('corpus.txt', 'r').readlines():... outfile.write(line.replace('a', 'some')) >>> outfile.close()

44 Functions A function is a reusable part of a program. Functions are defined with def >>> def square(x):... return x*x >>> print square(8) 64 Optional arguments: >>> def power(x, exp=2): # exp defaults to 2... if x <= 0: return 1... else: return x*power(x, exp-1)

Homework 1assignment Call your program ComputeAverage –Read in a file (called “numbers.txt”) of integers and compute and print the average –Also, compute and print the average of just the non-negative numbers. –Find and print the minimum –Find and print the maximum Submit this program on Blackboard under the appropriate link

Useful things for Homework 1 input = open("numbers.txt", "r") for line in input: x = int(line)

For Next Class, Tuesday Blackboard Quiz 1 due Tuesday No late Blackboard quizzes –No make up quizzes Homework 2 is due next Thursday –As is Blackboard Quiz 2