490 likes | 511 Views
CSE 115. Introduction to Computer Science I. UBInfinite requirements for lab. For Lab EXAM this week Up to and including Module 1 PreLab For Lab Activity next week ALL of M2 JavaScript from Expressions to Module 2 - JS. Lab Exam grading reminders. Every lab exam offers a make-up
E N D
CSE 115 Introduction to Computer Science I
UBInfiniterequirements for lab For Lab EXAM this week Up to and including Module 1 PreLab For Lab Activity next week ALL of M2 JavaScript from Expressions to Module 2 - JS
Lab Examgrading reminders Every lab exam offers a make-up Lab Exam 1 make-up is paired with Lab Exam 2 Higher of two scores counts as lab exam grade
"use strict" Invokes JavaScript strict mode Autograder uses strict mode Avoids many UBInfinite problems
Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Simple vs Compound expressions statements values compound 13.79 * 12 compound if { … } else { … } compound [ 42, 17, -318 ] simple 13.79 simple x = e simple 13.79
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Arrays in JavaScript • How can we conceptualize an array's structure? • let x = ['cat', 'dog', 'lizard'] 3 "cat" "dog" "lizard"
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Loop Statement Subgoal • loop1) Determine the collection whose values you want to process • loop2) Write the loop headera) Write the for keywordb) Write an open parenthesis • c) Write the let keyword & name of the loop variable • d) Write the of keyword • e) Write the name of the collectionf) Write a close parenthesis • loop3) Write the { and } delimiters • loop4) Write the loop body inside the delimiters • a) The loop variable is assigned a value BEFORE the body is executed: do not assign a value to the loop variable!
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Keywords Matter! • Python's for…in • is analogous to • JavaScript's for…of
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW JavaScript for… loops • for…ofto get values • for…into get indicies HIGH ACCIDENT ZONE AHEAD
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Changing Collection's Length
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Strings are immutable collections • Strings are compound values: they consist of a sequence of characters. • The characters in a string cannot be altered. • Both concatenation (+) and character replacement produce new strings. • Use brackets to access elements:"abc"[0] has value "a""abc"[1] has value "b"
Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises
implode • Want function which takes:array of String/list of strReturn string containing the entries in order • implode(['a','b','c'])returns"abc" • Work with neighbors and be prepared to:come up with additional test cases,state update to accumulation variable (in English), specify identity value to initialize accumulator,write function in JavaScript and/or Python
implode • Possible declaration: • Sample test cases: • Identity value & accumulator: function implode(lst) { // Code will go here } def implode(lst): # Code will go here implode(['a','b','c']) should return "abc"implode(['a']) should return "a"implode([ ]) should return "" Accumulator update operation: +(string catenation) Identity value: ""(empty string)
implode • Possible solutions: function implode(lst) { let result = ""; for (let v of lst) { result = result + v; } return result;} def implode(lst): result = "" for v in lst: result = result + v return result
explode • Want function which takes:String/strReturn array/list where each letter is entry • explode("abc")returns['a','b','c'] • Work with neighbors and be prepared to:come up with more test cases,state update to accumulation variable (in English), specify identity value to initialize accumulator,write function in JavaScript and/or Python
explode • Possible declaration: • Sample test cases: • Identity value & accumulator: function explode(str) { // Code will go here } def explode(st): # Code will go here explode("abc") should return ['a','b','c']explode("a") should return ['a']explode("") should return [ ] Accumulator update operation: push(JS) or append(Python) Identity value: [](empty array/empty list)
explode • Possible solutions: function explode(str) { let result = []; for (let v of str) {result.push(v); } return result;} def explode(st): result = [] for v in st:result.append(v) return result
Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises
Patterns in Code function cat(arr) { let acc = ""; for (let n of arr) {acc = acc + n; } return acc;} function sum(arr) { let acc = 0; for (let n of arr) {acc = acc + n; } return acc;} function prod(arr) { let acc = 1; for (let n of arr) {acc = acc * n; } return acc;}
Patterns in Code function sum(arr) { let acc = 0; for (let n of arr) {acc = acc + n; } return acc;} function prod(arr) { let acc = 1; for (let n of arr) {acc = acc * n; } return acc;} function cat(arr) { let acc = ""; for (let n of arr) {acc = acc + n; } return acc;} Functions start by declaringaccumulator and assigning to identity value
Patterns in Code function sum(arr) { let acc = 0; for (let n of arr) {acc = acc + n; } return acc;} function prod(arr) { let acc = 1; for (let n of arr) {acc = acc * n; } return acc;} function cat(arr) { let acc = ""; for (let n of arr) {acc = acc + n; } return acc;} Loop definedallowing action on each important value
Patterns in Code function sum(arr) { let acc = 0; for (let n of arr) {acc = acc + n; } return acc;} function prod(arr) { let acc = 1; for (let n of arr) {acc = acc * n; } return acc;} function cat(arr) { let acc = ""; for (let n of arr) {acc = acc + n; } return acc;} Action(s) update accumulator for current value
Patterns in Code function sum(arr) { let acc = 0; for (let n of arr) {acc = acc + n; } return acc;} function prod(arr) { let acc = 1; for (let n of arr) {acc = acc * n; } return acc;} function cat(arr) { let acc = ""; for (let n of arr) {acc = acc + n; } return acc;} Once loop completes,return accumulator
Patterns in Code function sum(arr) { let acc = 0; for (let n of arr) {acc = acc + n; } return acc;} function prod(arr) { let acc = 1; for (let n of arr) {acc = acc * n; } return acc;} function cat(arr) { let acc = ""; for (let n of arr) {acc = acc + n; } return acc;} Identity value changes to match accumulation operator
Patterns in Code function sum(arr) { let acc = 0; for (let n of arr) {acc = acc + n; } return acc;} function prod(arr) { let acc = 1; for (let n of arr) {acc = acc * n; } return acc;} function cat(arr) { let acc = ""; for (let n of arr) {acc = acc + n; } return acc;} Code pattern("template")found everywhere…
Patterns in Code def sum(arr) :acc = 0 for n in arr :acc = acc + n return acc def prod(arr) :acc = 1 for n in arr :acc = acc * n return acc def cat(arr) :acc = "" for n in arr :acc = acc + n return acc … and in nearly everyprogramming languages
Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises
Sequences of values • str: a sequence of characters • list: a sequence of (arbitrary) values • range: a sequence of numeric values
range • Type representing a fixed sequence of numbersOften used to control for...in loop executionrange([start, ]stop[, step])0 default value of start; 1 default value of step • If step is positive, range determined by formula:r[i] = start + step*i, from i = 0& while r[i] <stop • If step is negative, range determined by formula:r[i] = start + step*i, from i = 0& while r[i] >stop Lightly edited from https://docs.python.org/3/library/stdtypes.html#range
range • range([start, ]stop[, step])0 default value of start; 1 default value of step • Examples range(5)consists of values 0, 1,2,3, and 4 range(3, 7)consists of values 3, 4,5,and 6 range(3, 7, 2)consists of values 3and 5 range(3, 8, 2)consists of values 3, 5,and 7
Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises
Exercise 1 • Define a function named keep_long_stringswhich takes • an array • a list • of strings and returns a new array/list containing only those strings from the argument which contain more than four characters. • Maintain the (relative) order of items.
Exercise 1 • Test cases?
Exercise 1 • Test cases?(Remember: keep text more than 4 characters) • keep_long_strings([ ]) • [ ] • keep_long_strings(["lamb"]) • [ ] • keep_long_strings(["aardvark"]) • ["aardvark"] • keep_long_strings(["cat","aardvark","lizard","dog"]) ["aardvark", "lizard"]
Exercise 1 • Code? • def keep_long_strings( lst ) : • answer = [ ] • for st in lst : • if len(st) > 4 : • answer.append(st) • return answer • function keep_long_strings( lst ) { • let answer = [ ]; • for (let st of lst) { • if (st.length > 4) { • answer.push(st); }} • return answer; • }
Exercise 2 • Define a function named remove_vowelswhich takes • a String • a str • a new String/str with the vowels removed from the argument. • Maintain the (relative) order of characters.
Exercise 2 • Test cases? • remove_vowels("") • "" • remove_vowels("cat") • "ct" • remove_vowels("aardvark") • "rdvrk" • remove_vowels("vowels") • "vwls"
Exercise 2 • Code? • def remove_vowels( s ) : • answer = "" • for e in s : • if …e is not a vowel… : • answer = answer + e • return answer
Exercise 2 • Code? • def isVowel(ch) : • c = ch.lower() • return c=='a' or c=='e' or c=='i' or c=='o' or c=='u' • def remove_vowels( s ) : • answer = "" • for e in s : • if not isVowel(e) : • answer = answer + e • return answer
Exercise 3 • Define a function named odds which takes • an array • a list • of whole numbers and returns a new array/list containing only the odd numbers from the argument. • Maintain the (relative) order of numbers.
Exercise 3 • Test cases? • odds([ ]) • [ ] • odds([ 2 ]) • [ ] • odds([ 1 ]) • [ 1 ] • odds([ 5, 4, 7, 3, 14, 8, 9]) • [ 5, 7, 3, 9]
Exercise 3 • Code? • def odds( lst ) : • answer = [ ] • for num in lst : • if num % 2 == 1 : • answer.append(num) • return answer
Exercise 4 • Define a function named square which takes • an array • a list • of numbers and returns a new array/list containing the squares of the numbers in the argument. • Maintain the (relative) order of numbers.
Exercise 4 • Test cases? • squares([ ]) • [ ] • squares([ 2 ]) • [ 4 ] • squares([ 9, 3, -2, 8 ]) • [ 81, 9, 4, 64 ]
Exercise 4 • Code? • def squares( lst ) : • answer = [ ] • for num in lst : • answer.append(num*num) • return answer
See Our Progress • Open browser's JavaScript console & run: • for (let elem of document.all) { • elem.style.fontFamily = "courier"; • }