Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 131: Introduction to Computer Science

Similar presentations


Presentation on theme: "CSC 131: Introduction to Computer Science"— Presentation transcript:

1 CSC 131: Introduction to Computer Science
Dr. Curry Guinn

2 Quick Info Dr. Curry Guinn CIS 2025 guinnc@uncw.edu
Office Hours: MTR: 10:00am-11:00m and by appointment Teaching Assistant: Zach Krepps Tuesday, 8:00am-9:30am, 12:45pm-3:45pm Thursday, 8:00am-9:30am Office: GTA office – off of CIS 2004 lab

3 CSC Tutoring Lab! A free CSC Tutoring Lab has been created this semester for CSC 131, CSC 133, and CSC 231. It will be located in CIS 2004 on the following schedule: The tutors are Steve Lawrence, Tori Pruim, and Daniel Orsa. At least 2 tutors will be present during those hours. Monday 5pm-9pm Tuesday 6pm-9pm Wednesday 5pm-8pm Thursday

4 IT Career Day today! Today! Tuesday, September 19, in the CIS building between 3:30-7:00. Find an internship! Find a job! Learn about possible career paths. Network! Link: Please register.

5 RTP Bus Trip (Also free!)
Visit companies in Research Triangle Link: Registration required. Seats will fill. Do it today!

6 For Next Class, Thursday
Homework 5 is due Thursday, 11:59pm, September 21 Midterm 1 is next Tuesday/Thursday (Sep 26/28) Midterm 1 Study Guide

7 Today File I/O String processing

8 Let’s program Let’s create a program that opens a file for writing.
Let’s loop 10,000 times and write a single random integer on each line of the file. Remember that “write” must take a string as an argument rather than an integer. How do we convert that integer to a string. Then close the file. Open the file in a text editor and look at it.

9 Let’s make another program
The goal of this program is to open the file you just created and compute the average of the numbers. It opens the file that you just created for reading. It reads in each line, converts that string to an int, adds it to a running sum, and increases a counter keeping track of how many lines have been read. When the loop is done, take that sum and divide it by the count. Don’t forget to close the file when you are done with it.

10 String Processing The information in a text file, as with all information, is most likely going to be searched, analyzed, and/or updated. Collectively, the operations performed on strings is called string processing. We have already seen some operations on strings—for example, str[k], for accessing individual characters, and len(str) for getting the length of a string. We revisit sequence operations that apply to strings, and look at additional string-specific methods. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

11 String Traversal We saw in Chapter 4 how any sequence can be traversed, including strings. This is usually done by the use of a for loop. For example, if we want to read a line of a text file and determine the number of blank characters it contains, we could do the following. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

12 We also saw that the traversal can be done more simply without the explicit use of an index variable, Given the ability to traverse a string, each character can be individually “looked at” for various types of string processing. We look at some string processing operations next. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

13 String-Applicable Sequence Operations
Because strings (unlike lists) are immutable, sequence-modifying operations are not applicable to strings. For example, one cannot add, delete, or replace characters of a string. Therefore, all string operations that “modify” a string return a new string that is a modified version of the original string. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

14 Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

15 Recall that as we saw with lists, the slice operator s[start:end] returns the substring starting with index start, up to but not including index end. Also, s.index(chr) returns the index of the first occurrence of chr in s. Finally, min and max as applied to strings return the smallest (largest) character based on the underlying Unicode encoding. Thus, for example, all lowercase letters are larger (have a larger Unicode value) than all uppercase letters. We give examples of each of these operations for s = 'Hello Goodbye!'. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

16 String Methods There are a number of methods specific to strings in addition to the general sequence operations. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

17 Checking the Contents of a String
There are times when the individual characters in a string (or substring) need to be checked. For example, to check whether a character is an appropriate denotation of a musical note, we could do the following, Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

18 Since the in operator can also be applied to strings, we can also do the following,
We could take a similar approach for determining if a given character is a lowercase or uppercase letter or digit character, Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

19 Since checking for uppercase/lowercase and digit characters is common in programming, Python provides string methods isalpha, isdigit, isupper, and islower (among others). For example, to perform error checking on an entered credit card number could be done as follows, The method isdigit returns True if and only if each character in string credit_card is a digit. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

20 If only part of a string is to be checked, then a method can be applied to a slice of a string. For example, if the part numbers of a given company all begin with three letters, a check for invalid part numbers could be done as follows, in which isalpha returns True if and only if the first three characters in part_num are letters. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

21 Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

22 Searching and Modifying Strings
String processing involves search. For example, to determine the user name and domain parts of an address, the ampersand character separating the two would be searched for. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

23 The find method returns the index location of the first occurrence of a specified substring. Since in Python strings are immutable, to update the address, a new string would be constructed with the desired replacement as shown. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

24 The replace method produces a new string with every occurrence of a given substring within the original string replaced with another. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

25 Python also provides a strip method that “strips off” leading and trailing characters from a string. This is especially useful for stripping off the newline character, \n, from the end of a line in text processing if needed. Intoduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 8.3 String Processing

26 String processing of a file
Suppose a file contains LastName FirstName GPA How would you find the person with the highest GPA?

27 Algorithm Open the file Loop through each line
Split each line into its 3 components Convert the 3rd component to a double See if it is bigger than the biggest GPA you’ve seen so far If it is save it, along with the first and last name of the student Print out the saved first name, last name and gpa

28 For Next Class, Thursday
Homework 5 is due Thursday, 11:59pm, September 21 Midterm 1 is next Tuesday/Thursday (Sep 26/28)


Download ppt "CSC 131: Introduction to Computer Science"

Similar presentations


Ads by Google