CSC 221: Introduction to Programming Fall 2018

Slides:



Advertisements
Similar presentations
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Advertisements

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
With Microsoft Access 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine.
MS Access: Database Concepts Instructor: Vicki Weidler.
1 CSC 221: Introduction to Programming Fall 2011 Text processing  Python strings  indexing & slicing, len function  functions vs. methods  string methods:
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Shell Script Programming
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
1 CSC 221: Introduction to Programming Fall 2012 Text processing  Python strings  indexing & slicing, len function  functions vs. methods  string methods:
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Files Tutor: You will need ….
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Editing and Debugging Mumps with VistA and the Eclipse IDE Joel L. Ivey, Ph.D. Dept. of Veteran Affairs OI&T, Veterans Health IT Infrastructure & Security.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Development Environment
Introduction to Computing Science and Programming I
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Topic: File Input/Output (I/O)
Strings CSCI 112: Programming in C.
SQL and SQL*Plus Interaction
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Containers and Lists CIS 40 – Introduction to Programming in Python
Understanding File Management
Building Java Programs
Chapter 2: Getting Data into SAS
CSC 108H: Introduction to Computer Programming
Arrays and files BIS1523 – Lecture 15.
© Paradigm Publishing, Inc.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Designing and Debugging Batch and Interactive COBOL Programs
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Programming Logic and Design Fourth Edition, Comprehensive
File IO and Strings CIS 40 – Introduction to Programming in Python
Week 9 – Lesson 1 Arrays – Character Strings
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Encryption and Decryption
Benchmark Series Microsoft Word 2016 Level 2
T. Jumana Abu Shmais – AOU - Riyadh
Iteration: Beyond the Basic PERFORM
Word offers a number of features to help you streamline the formatting of documents. In this chapter, you will learn how to use predesigned building blocks.
CSC 221: Introduction to Programming Fall 2018
File Input and Output.
File Handling in Java January 19
CS190/295 Programming in Python for Life Sciences: Lecture 3
Functions continued.
Topics Introduction to File Input and Output
Object Oriented Programming in java
Introduction to Computer Science
Running a Java Program using Blue Jay.
Python Strings.
Microsoft Office Illustrated Fundamentals
Topics Introduction to File Input and Output
CSC 221: Introduction to Programming Fall 2018
Presentation transcript:

CSC 221: Introduction to Programming Fall 2018 Testing & files testing strategies debugging strategies codingbat exercises opening files for input & output read() vs. read(1) vs. readline(), write processing massive files file dialogs

Testing code when you design and write code, how do you know if it works? run it a few times and assume it's OK? to be convinced that code runs correctly in all cases, you must analyze the code and identify special cases that are handled then, define a test data set (inputs & corresponding outputs) that covers those cases e.g., for rotation, empty and single-char strings: ""  "" "A"  "D" multi-char string: "ET"  "HW" "BRUTE"  EUXWH" chars in the middle: "LMNOP"  "OPQRS" chars at the ends: "ABCDWXYZ"  "DEFGZABC" rotation of 0: "ET TU"  "ET TU" rotation of 52: "ET TU"  "ET TU" what about lowercase letters? non-letters?

Testing rotate how will rotate handle spaces & other non-letters? ??? what should we do with non-letters? how can we fix the code?

Handling spaces to handle spaces and non-capital letters, check each character before rotating, if not a capital letter than just leave as is what if we also wanted to rotate lowercase letters? see HW4

Debugging how do you approach your code when it doesn't work as desired?

Debugging steps first, isolate the issue in this case, which function is the source? (may be more than one) sometimes the return value is enough to identify the problem these seem ridiculously high!

Debugging steps add print statements to see values during execution potential problems: incorrect value calculated by pig_turn (unlikely, since provided) incorrect update of num_turns problem?

Debugging steps when you think you have it figured out, fix it and then test thoroughly leave in your print statements until you know it works, then remove

Debugging steps after you fix one bug, test everything again it may not be the only bug plus, your "fix" may mess up other parts of the code

Debugged version looks good! can now remove print statement QUESTION: when playing a game, does a turn stop automatically when you reach 100, or does it always go to the cutoff? How can we test this? How can we fix it?

CodingBat (or CoDingbat) a source of good practice programming problems is codingbat.com developed and maintained by Nick Parlante at Stanford problems are organized by language (Java or Python) and topic (e.g., Strings, Lists) in addition to programming practice, the site hones skills in identifying test cases try the first few Python exercises under String-1 (uses +, [ ] – no loops needed) if time, try some under String-2 (w/ loops)

Reading from a file last week, we wrote functions for manipulating/converting text when dealing with larger amounts of input, e.g., a paragraph or entire story, it is more convenient to enter into a file and read directly from the file the first line creates a file object for reading from the file named filename the read() method reads the entire contents of the file object into a string returns that string for massive files, reading and storing the entire contents may not be feasible

read() vs. read(#) vs. readline() read has an optional input, the number of characters to read e.g., infile.read(1) reads a single character returns empty string if at end of file by default, each call to print displays a new line can add end="" to override readline reads an entire line of text (incl. end-of-line chars) returns empty string if at end of file

??? Example: file stats suppose we want to generate stats on a file # of lines # of characters average # of characters per line which type of read do we need: read() vs. read(1) vs. readline() ? ???

Output files here, three different versions of a function that copies a file open an output file using "w" as the 2nd input can write to an output file object using the write method

Example: encoding a file could extend our encryption algorithms to files instead of requiring the user to enter each string, read in from a file could save encoded/decoded text in a different file or, could overwrite the original file by using it for both "r" and "w" you will similarly encode a file (using a substitution cipher) in HW4

Processing massive files in the class code folder is a file named TrumpTweets.txt contains every tweet from Donald Trump, up through10/21/2018 (one per line) we should be able to use file_stats to determine the total number of tweets (the number of lines in the file) the average length of the tweets (average line length) ???

Error handling text documents that are created using more advanced text editors may contain special characters there are optional inputs that can be provided when opening a file if you know the encoding, can specify it  encoding="utf8" if you just want to ignore unrecognized characters  errors="ignore"

Exercise download files.pyxtx (and rename it files.py) also download TrumpTweets.txt and store in the same folder call the revised file_stats function to determine how many tweets there were and their average length

Other questions? how many tweets contain an exclamation point? what percentage of tweets contain an exclamation point? how many (and what percentage) contain multiple exclamation points? how many (and what percentage) contain the word "great"? "sad"? "America"? … how many exclamation points are there total? what is the average number of exclamation points per tweet? what is the average number of "great"s per tweet? "sad"s? "America"s? … what is the ratio of uppercase to lowercase letters? MODIFY file_stats TO ANSWER THESE OR SIMILAR QUESTIONS

Filtering files the TrumpTweets.txt file is too big to print out but could filter out only those tweets that meet certain conditions write a new function named filter_file, which displays every line from the file that contains a particular keyword the comparison should be case-insensitive also, returns the number of such lines

FYI: browsing for files the open function assumes a relative file address will look for the filename in the same directory as the program can specify a path prefix, relative to the current directory open("../Data/"+filename, "r") open("csc221/Results/"+filename, "w") if you want to be able to search for files, there is a module for that tkinter.py has functions for opening dialog boxes to select files however, the Mac version is VERY BUGGY I have written a module named fileIO.py (in the class code folder) that hides the kludgy fix from you import fileIO infile = fileIO.get_input_file_name("Select the input file") outfile = fileIO.get_output_file_name("Select the output file")

File dialogs