Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

Review of Weeks 1-5 Congratulations, again! We are trying to give you fun superpowers, to have a positive impact! Review of Weeks 1-5 2 What Will We Learn? 1. Understand how to have a positive impact in the world via computing! Hence the Khoury library (for testing;...

Review of Weeks 1-5 Congratulations, again! We are trying to give you fun superpowers, to have a positive impact! Review of Weeks 1-5 2 What Will We Learn? 1. Understand how to have a positive impact in the world via computing! Hence the Khoury library (for testing; for interactive programs; for files) 2. Effective “habits” for working in teams on large software projects Hence the coding style guide! 3. Learn effective usage of common programming paradigms Imperative for now; functional & object-oriented 4. Learn how to program using Kotlin, a commonly used language Review of Weeks 1-5 3 Computation (Information → Data Representations) + Code → Impact in the World! Review of Weeks 1-5 4 Programming Paradigms Review of Weeks 1-5 5 Bad design. Period. You code User’s code var x = 0 import yourcode.* fun f() { f() if (x > 0) { g() } } Another user’s code import yourcode.* fun g() { x += 1 g() } f() 6 Some Principles of Software Design 1. DRY: Don’t Repeat Yourself, which leads to: Named values (aka. our variables) – single authoritative representation Functions – abstractions over patterns of repeated instructions 2. Test-Driven Development Tests give us confidence in our code & exemplify for others Make sure to capture representative inputs/outputs (e.g., all distinct values) ( ) Review of Weeks 1-5 7 Some Principles of Software Design (2) 3. Type-Driven Development Data types allow Kotlin to work faster and catch errors sooner. The input type to a function can help determine its structure & shape. Operations on nested, complex types should decompose to helper functions. 4. Separation of Responsibility/Concern (aka. modularity) Allows to break complex problems into smaller, independent, well-designed and tested parts (e.g., reactConsole) Review of Weeks 1-5 8 Good design? Your reactConsole. import khoury.input var attempts = 0 while (true) { println(“Enter a number:”) val input = input().toInt() attempts += 1 if (input == secret) { println(“You got it (after ${ attempts } attempts)”) break } else if (input < secret) { println(“Too low!”) else { println(“Too high!”) } } 9 Designing functions // What does it do? // // (should be one, simple thing) // ignored by , critical to fun funcName(arg1: Type1, arg2: Type2, …): returnType { … → & continue println(”Fundies!”) return value/expression } Immediately leave function & 🫴 result to calling code Review of Weeks 1-5 10 Designing functions (alternative comments) fun funcName(arg1: Type1, arg2: Type2, …): returnType { … } Review of Weeks 1-5 11 Testing functions – A Must! import khoury.EnabledTest import khoury.testSame import khoury.runEnabledTests … Label function (an “annotation”) @EnabledTest fun testFuncName() { testSame(funcName(val1, val2, …), expectedValue, "short description") … } Find & run all @EnabledTest functions in this file … runEnabledTests(this) main() Review of Weeks 1-5 12 Modeling info as fixed-size data types Numbers (e.g., , ) Small set of options (e.g., , , Int, Double ) Boolean (true/false) Text symbols enum class (task-specific values) Char sealed class (different values) Functions (e.g., ) Bundle into one value (e.g., , ) (Type1, Type2, …) -> ReturnType data class Review of Weeks 1-5 13 Modeling info as arbitrary-size data types Number sequences (e.g., , ) Lists of any single type (e.g., , , IntRange ) 1..3 List 1 until 3 IntProgression Self-referential data types 1..4 step 2 3 downTo 1 Text (e.g., , ) String Review of Weeks 1-5 14 List Representations As a contiguous array As a linked list val lst = listOf(4, 5, 8, 48) val lst = Node(data=4, next=…) 0 1 2 3 4 5 8 48 4 next 5 next 8 next 48 next End Review of Weeks 1-5 15 Lists Sources of Information Artisanal Combination "howdy" [8,6,7,5] + [3,0,9] 1..5 "8675" + "309” listOf(2, 5, 0, 0) (1..5 step 2) + 10 emptyList() Construction Conversion List(5, ::initFunc) "a,b,c".split(",") (1..3).toList() Files ["a","b","c"].joinToString(",") fileReadAsList Review of Weeks 1-5 16 Lists Access patterns Extent.length (for strings),.size, isEmpty(),.indices Element (→ value) list[index] — that is a 0-based index, right? Searching (→ Boolean) val in list or val !in list Parts (→ new lists).slice(range, progression or list of indices).substring(range) (for strings).drop(count).take(count) Review of Weeks 1-5 17 Lists Operations Produces new lists and values:.reversed(),.sorted() Review of Weeks 1-5 18 Lists Abstractions Review of Weeks 1-5 19 From Data Types to Code From String to string templates "unchanged $value ${ expression }" From Boolean to conditionals if, else if, else From options to when expressions Boolean, enum class, sealed class From nested data to helper functions data.field.subfield.method() Review of Weeks 1-5 20 From Data Types to Code From self-referential data to recursive functions sealed class LinkedList { object Empty: LinkedList() data class Node(val data: data type, val next: Node): LinkedList() } fun f(node: LinkedList) { when(node) { is LinkedList.Empty -> Final step is LinkedList.Node -> { ll.data op f(ll.next) } } Review of Weeks 1-5 21 } Iterative any for loop Recursion fun any ( fun any ( nums: List, nums: List, pred: (Int) -> Boolean pred: (Int) -> Boolean ): Boolean { ): Boolean { for (item in nums) return when (nums.isEmpty()) { if (pred(item)) true -> false return true false -> pred(nums) || return false any(nums.drop(1), pred) } } } 22 From Data Types to Function Shape Enumerations Data Class enum class Option { A, B, C } data class Pieces(val a: Int, val b: String) fun useOption(o: Option) { val p: Pieces = Pieces(1, "one") when (o) { Option.A -> … fun usePieces(p: Pieces) { Option.B -> … f( p.a …, p.b … ) Option.C -> … } } } Review of Weeks 1-5 23 From Data Types to Function Shape Enumerations Data Class enum class Option { A, B, C } data class Pieces(val a: Int, val b: String) fun useOption(o: Option) { val p: Pieces = Pieces(1, "one") when (o) { Option.A -> … fun usePieces(p: Pieces) { Option.B -> … f( p.a …, p.b … ) Option.C -> … } } } Review of Weeks 1-5 24 Interactions main() input() println(str) testSame, captureResults, CapturedResult, @EnabledTest, runEnabledTests Review of Weeks 1-5 25 Interactions: Event-Driven Framework reactConsole Review of Weeks 1-5 26 Quiz questions There are five type of questions: 1. Multiple choice – given partial code, identify types 2. Multiple choice – goal(s) of this course 3. True or false – given function & its tests, is it sufficiently tested? 4. True or false – given function & data, is it sufficiently decomposed? 5. Coding – given data & tests, write a function Review of Weeks 1-5 27 ??? is String, Int, Boolean, Unit, or…? fun f(p: ???): ??? { val temp = "howdy-$p" println(temp.length) } Review of Weeks 1-5 28 ??? is String, Int, Boolean, Unit, or…? fun f(p: Boolean or Int or String): Unit { val temp = "howdy-$p" println(temp.length) } Review of Weeks 1 & 2 29 ??? is String, Int, or Boolean? Review of Weeks 1-5 30 ??? is String, Int, or Boolean? Review of Weeks 1-5 31 ??? is…? fun f(p: ???): ??? { return p.take(2) + p[p.size - 1].drop(1) } Review of Weeks 1-5 32 ??? is…? fun f(p: ???): ??? { return p.take(2) + p[p.size - 1].drop(1) } Either (List) -> String or (List) -> List Review of Weeks 1-5 33 Is it sufficiently tested? Review of Weeks 1-5 34 Is it sufficiently tested? Missing workdays. Missing true cases. Review of Weeks 1 & 2 35 Write function welcome enum class Campus { OAKLAND, BOSTON, LONDON } enum class Currency { USD, GBP } data class WelcomeInfo(val greeting: String, val currency: Currency) fun campusToCurrency(campus: Campus): Currency { return when (campus) { Campus.OAKLAND -> Currency.USD Campus.BOSTON -> Currency.USD Campus.London -> Currency.USD } } @EnabledTest fun testQuestion() { testSame( welcome(“Alice”, Campus.OAKLAND), WelcomeInfo(“Welcome to NU, Alice!”, Currency.USD) ) testSame( welcome(“Bob”, Campus.LONDON), WelcomeInfo(“Welcome to NU, Bob!”, Currency.GBP) ) } Review of Weeks 1-5 36 Write function welcome fun welcome(name: String, campus: Campus): WelcomeInfo { return WelcomeInfo( greeting = “Welcome to NU, $name!”, currency = campusToCurrency(campus), ) } Review of Weeks 1 & 2 37 Is it a reasonable type? Our goal is to represent a color palette for an office, where each such palette requires specifying five colors. Imagine: drawOffice(p: OfficePalette) Review of Weeks 1-5 38 Is it a reasonable type? No. Our goal is to represent a color palette for an office, where each such palette requires specifying five colors. Review of Weeks 1-5 39 Is it a reasonable type? No. How to improve? Our goal is to represent a color palette for an office, where each such palette requires specifying five colors. 40 1/5 Does the following test pass? Yes. Review of Weeks 1-5 41 2/5 Does the following test pass? Yes. Review of Weeks 1-5 42 3/5 Does the following test pass? Yes. Review of Weeks 1-5 43 4/5 Does the following test pass? Yes. Review of Weeks 1-5 44 5/5 Does the following test pass? Yes. Review of Weeks 1-5 45 1/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 46 1/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [1, 2, 3, 4] Review of Weeks 1-5 47 2/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 48 2/10 List Operations After the code runs, what is the value of start and result? start: [3, 2, 1] result: [2, 1] Review of Weeks 1-5 49 3/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 50 3/10 List Operations After the code runs, what is the value of start and result? start: [8, 6, 7, 5, 3, 0, 9] result: [0, 3, 5, 6, 7, 8, 9] Review of Weeks 1-5 51 4/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 52 4/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [1, 3] Review of Weeks 1-5 53 5/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 54 5/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [2, 3, 4] Review of Weeks 1-5 55 6/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 56 6/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [3, 2, 1] Review of Weeks 1-5 57 7/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 58 7/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [2, 3] Review of Weeks 1-5 59 8/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 60 8/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [1, 2, 3, 5] Review of Weeks 1-5 61 9/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 62 9/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [2, 3] Review of Weeks 1-5 63 10/10 List Operations After the code runs, what is the value of start and result? Review of Weeks 1-5 64 10/10 List Operations After the code runs, what is the value of start and result? start: [1, 2, 3] result: [2, 3, 10] Review of Weeks 1-5 65 1/5 map, filter, any, all, or fold? Given a list of paintings, produce the years in which they were painted. Review of Weeks 1-5 66 1/5 map, filter, any, all, or fold? map Given a list of paintings, produce the years in which they were painted. Review of Weeks 1-5 67 2/5 map, filter, any, all, or fold? Given a list of paintings, produce those painted by a particular artist. Review of Weeks 1-5 68 2/5 map, filter, any, all, or fold? filter Given a list of paintings, produce those painted by a particular artist. Review of Weeks 1-5 69 3/5 map, filter, any, all, or fold? Given a list of paintings, calculate their total price. Review of Weeks 1-5 70 3/5 map, filter, any, all, or fold? fold Given a list of paintings, calculate their total price. Review of Weeks 1-5 71 4/5 map, filter, any, all, or fold? Given a list of paintings, determine if there exists one painted after a given year. Review of Weeks 1-5 72 4/5 map, filter, any, all, or fold? any Given a list of paintings, determine if there exists one painted after a given year. Review of Weeks 1-5 73 5/5 map, filter, any, all, or fold? Given a list of paintings, determine if the full list were painted by the same supplied artist. Review of Weeks 1-5 74 5/5 map, filter, any, all, or fold? all Given a list of paintings, determine if the full list were painted by the same supplied artist. Review of Weeks 1-5 75 1/2 for, while, or do-while? Print each painting to the screen. Review of Weeks 1-5 76 1/2 for, while, or do-while? for Print each painting to the screen. Review of Weeks 1-5 77 2/2 for, while, or do-while? Prompt the user for a password and keep prompting until a valid password is entered. Review of Weeks 1-5 78 2/2 for, while, or do-while? do-while Prompt the user for a password and keep prompting until a valid password is entered. Review of Weeks 1-5 79 Write code & test it Write code for a function that processes a supplied list of integers representing interest rates available from several local banks. If the list has fewer than two rates or contains numbers outside the range 0-100 (including 0 and 100), indicate the list is invalid. If the list contains even a single rate above 4, respond to start saving now! Otherwise, keep looking for better options. Review of Weeks 1-5 80 Sample solution What about testing it? Consider at least empty or non-empty list cases; and the three possible responses: [] → SavingsResponse.INVALID [3, 4] → SavingsResponse.KEEP_LOOKING [1, 5] → SavingsResponse.SAVE_NOW Review of Weeks 1-5 81 True or false? Explain. “In a memory of 100 free units, we can surely create an array representation of a list of 100 1-unit elements.” “Both functional and imperative programming paradigms champion the use of mutable state.” Review of Weeks 1-5 82 Onwards! Review and rewrite class examples (on Canvas) and home works. Ask questions! Use office hours. Review of Weeks 1-5 83

Use Quizgecko on...
Browser
Browser