Review: A Computational View Programming language common concepts: 1. sequence of instructions -> order of operations important 2. conditional structures -> execute a group of statements based on the value of the condition(s), i.e. true or false 3. looping structures -> repeat a group of statements: for a predetermined number of times while a condition remains true until a condition becomes true
Input and Output … standard input/output input: variable = constructor (input("prompt goes here")) e.g. name = input("Enter name: ") weight = float(input("Enter package weight: ")) output (always formatted): print("string with codes".format(parameters)) e.g. print("Name is {} ".format(name)) input using formatted (customized) prompt: variable = input("str + codes".format(parameters)) e.g. count[i] = int(input(“day {}: ”.format(i)))
Input and Output … text files 1. open the file: file_variable = open(filename,mode) e.g.file_name = input("Enter file name: " ) customer_file = open(file_name, "r") sales_file = open("sales.txt", "w") 2. process the file -> next slide 3. close the file: file_variable.close() e.g.customer_file.close() sales_file.close()
Input and Output … text files read from the file (explicit): string = file_variable.readline() e.g.customer_record = customer_file.readline() typically used with a while loop: string = file_variable.readline()# priming read while string != "" and some other condition: … some code here … … that modifies the other condition …. string = file_variable.readline() … continue code here …
Input and Output … text files read from the file (implicit): for line in file_variable: e.g. for customer_record in customer_file: - the file is essentially a sequence - never use readline inside a for loop write to the file: print("string".format(parameters), file= file_variable) e.g.print( '{}'.format(items), end="", file = sales_file ) - identical to writing to the console - need to be careful of how the new line character is handled
Fundamental algorithms … I/O writing output using string format method writing sequences on separate lines, on the same line, on multiple lines by controlling the newline writing multiple lines in column justified format with column/row headers writing two dimension tables in column justified format with column/row headers reading/writing delimited (e.g. comma) data files
Fundamental Algorithms … numeric calculate an equation over a range of values (non-obvious equations will be provided) generate random numbers using the Python random library utilize constants and functions in the Python math library compute standard statistics without Python functions using a running calculation technique: count, total, average/mean, minimum, maximum read and write (formatted) one and two dimension lists of numbers
Working With Text the basic units of text: 1. Character: An individual letter, number, symbol, space, punctuation, etc. Includes visible characters and control characters in ASCII table. 2. Word: a sequence of characters delimited by the start of a line, whitespace characters (space, tab), punctuation or newline/end-of-line. 3. Line: a sequence of characters delimited by a newline or end-of-line character. 4. Field: a sequence of characters delimited by commas, tabs, spaces in a delimited file context.
Fundamental Algorithms searching is an item in the sequence? where is an item in the sequence? find the first, next, last, or all instance(s) of the item in the sequence counting all items in a sequence e.g. counting characters, words, lines, paragraphs in text; counting elements in a list counting specific items in a sequence e.g. counting occurrences of "the" in a file, how many students got 100 in the test?
Fundamental Algorithms string and substring matching find the first, next, last, and all instance(s) of the string or substring in the text e.g. "aaa" in ["abe", "555", "aaa"] "gg"in "egg" "It was" in "It was snowing yesterday. " range search does a point lie inside a given polygon? e.g. is the cell phone’s position within a repeater tower’s range does a entity with certain attributes exist? e.g. find all cereals with fiber greater than 3g and sodium less than 200mg
Fundamental Algorithms modifying a sequence: find an item insert an item replace an item delete an item combine sequences: concatenate, join, merge reformat the sequence (strings)