1 / 49

CSE 115

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

albertjohn
Download Presentation

CSE 115

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 115 Introduction to Computer Science I

  2. 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

  3. 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

  4. "use strict" Invokes JavaScript strict mode Autograder uses strict mode Avoids many UBInfinite problems

  5. Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises

  6. 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

  7. 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"

  8. 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!

  9. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Keywords Matter! • Python's for…in • is analogous to • JavaScript's for…of

  10. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW JavaScript for… loops • for…ofto get values • for…into get indicies HIGH ACCIDENT ZONE AHEAD

  11. REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Changing Collection's Length

  12. 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"

  13. Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises

  14. 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

  15. 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)

  16. 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

  17. 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

  18. 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)

  19. 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

  20. Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises

  21. 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;}

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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…

  28. 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

  29. Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises

  30. Sequences of values • str: a sequence of characters • list: a sequence of (arbitrary) values • range: a sequence of numeric values

  31. 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

  32. 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

  33. Today's Plan Review • Wednesday Exercises, revisited • Patterns in Code • Python Range • Exercises

  34. Exercises

  35. 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.

  36. Exercise 1 • Test cases?

  37. 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"]

  38. 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; • }

  39. 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.

  40. Exercise 2 • Test cases? • remove_vowels("") •  "" • remove_vowels("cat") •  "ct" • remove_vowels("aardvark") •  "rdvrk" • remove_vowels("vowels") •  "vwls"

  41. Exercise 2 • Code? • def remove_vowels( s ) : • answer = "" • for e in s : • if …e is not a vowel… : • answer = answer + e • return answer

  42. 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

  43. 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.

  44. Exercise 3 • Test cases? • odds([ ]) •  [ ] • odds([ 2 ]) •  [ ] • odds([ 1 ]) •  [ 1 ] • odds([ 5, 4, 7, 3, 14, 8, 9]) •  [ 5, 7, 3, 9]

  45. Exercise 3 • Code? • def odds( lst ) : • answer = [ ] • for num in lst : • if num % 2 == 1 : • answer.append(num) • return answer

  46. 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.

  47. Exercise 4 • Test cases? • squares([ ]) •  [ ] • squares([ 2 ]) •  [ 4 ] • squares([ 9, 3, -2, 8 ]) •  [ 81, 9, 4, 64 ]

  48. Exercise 4 • Code? • def squares( lst ) : • answer = [ ] • for num in lst : • answer.append(num*num) • return answer

  49. See Our Progress • Open browser's JavaScript console & run: • for (let elem of document.all) { • elem.style.fontFamily = "courier"; • }

More Related