Download presentation
Presentation is loading. Please wait.
Published byChristiana Henry Modified over 8 years ago
1
CSC 231: Introduction to Data Structures Dr. Curry Guinn
2
Quick Info Dr. Curry Guinn –CIS 2045 –guinnc@uncw.eduguinnc@uncw.edu –www.uncw.edu/people/guinnc –962-7937 –Office Hours: MWR: 11:00am-12:00pm and by appointment –Teaching Assistant: Paul Murray (hours TBD)
3
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)
4
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
5
Jobs When Google interviews, what do they ask?
6
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
7
How To Approach This Course Your future CSC courses and your future career in IT/Computers depends on mastering this material.
8
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 http://people.uncw.edu/guinnc/courses/Fall 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
9
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 email early and often.
10
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
11
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 http://interactivepython.org/courselib/static/pythonds /index.html http://interactivepython.org/courselib/static/pythonds /index.html All the downloads (source code, etc.) http://www.pythonworks.org/pythonds http://www.pythonworks.org/pythonds
12
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 3.4.0 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.
13
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?
14
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
15
Understanding Data Structures When are arrays bad? Which leads us to linked lists, queues, and stacks
16
Understanding Data Structures Which leads us to sorting Some sorting algorithms depend on recursion so we’ll need to study that too
17
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?
18
Understanding Data Structures Which leads us to studying how to balance binary trees Are there other useful tree structures?
19
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?
20
The Basics of Python
21
The Lecture Portion of Today’s Python Review The Python Interpreter, IDEs Strings Variables Lists Conditionals Loops Input/output Functions
22
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
23
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.
24
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’
25
Defining strings Each string is stored in the computer’s memory as a list of characters. >>> myString = “GATTACA” myString
26
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.
27
Accessing substrings >>> myString = “GATTACA” >>> myString[1:3] ‘AT’ >>> myString[:3] ‘GAT’ >>> myString[4:] ‘ACA’ >>> myString[3:5] ‘TA’ >>> myString[:] ‘GATTACA’
28
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
29
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
30
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
31
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
32
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
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
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
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'] ] 0 1 2 3 -4 -3 -2 -1
36
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
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
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
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
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
41
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
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
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
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)
45
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
46
Useful things for Homework 1 input = open("numbers.txt", "r") for line in input: x = int(line)
47
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.