diff options
| author | mo khan <mo@mokhan.ca> | 2024-06-01 13:09:02 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2024-06-01 13:09:02 -0600 |
| commit | 5ccc7d02a4daef5231b89c0cc4258ce2d7e6a12a (patch) | |
| tree | cec13104415610f71b837de1d22e9270df25f029 /assignments | |
| parent | 24c677e809f77e9798f1a07da347e620b78b7a1b (diff) | |
Complete assignment 1
Diffstat (limited to 'assignments')
| -rw-r--r-- | assignments/1-solution.md | 75 | ||||
| -rw-r--r-- | assignments/1.md | 14 | ||||
| -rw-r--r-- | assignments/a1.rb | 30 |
3 files changed, 105 insertions, 14 deletions
diff --git a/assignments/1-solution.md b/assignments/1-solution.md new file mode 100644 index 0000000..a4fa273 --- /dev/null +++ b/assignments/1-solution.md @@ -0,0 +1,75 @@ +# Assignment 1 – choose ONE exercise each from Chapters 2 and 3 + +## Chapter 2: Exercises + +4. Write an algorithm that gets the price for item A plus the quantity + purchased. The algorithm prints the total cost, including 6% sales tax. + + ```plaintext + Get values for `price` and `quantity` + Set the value of `tax` to 1.06 + Set the value of `total` to `price` multiplied by `quantity` multiplied by `tax` + Print the value of `total` + ``` + + ```ruby + def calculate_total(price, quantity, tax = 0.06) + return (price * quantity) * (1.0 + tax) + end + ``` + +## Chapter 3: Exercises + +21. Use the binary search algorithm to decide whether 35 is in the following + list: `3, 6, 7, 9, 12, 14, 18, 21, 22, 31, 43` + + ```plaintext + Get the list of numbers and assign to variable `items` + Get the target number and assign to variable `target` + Set variable `min` to 0 + Set variable `max` to total # of `items` + Set variable `mid` to `max - min / 2` + Set variable `found` to No + while (mid > min && mid < max) + Set the variable `current` to `items` at index `mid` + if current = target + Set variable `found` to Yes + Set variable `mid` to `min - 1` + else if target > current + Set variable `min` to the value of `mid` + Set the variable `mid` to `((max-min) / 2) + min` + else + Set variable `max` to the value of `mid` + Set the variable `mid` to `((max-min) / 2) + min` + end + end of loop + If Found = Yes + print Found + else + print Not Found + stop + ``` + + ```ruby + def bsearch(target, items) + min = 0 + max = items.size + mid = (max - min) / 2 + + while (mid > min && mid < max) do + current = items[mid] + + if target == current + return true + elsif target > current + min = mid + else + max = mid + end + + mid = ((max - min) / 2) + min + end + + return false + end + ``` diff --git a/assignments/1.md b/assignments/1.md index cf0d98e..fbc2886 100644 --- a/assignments/1.md +++ b/assignments/1.md @@ -23,20 +23,6 @@ heavily as a regular test. 4. Write an algorithm that gets the price for item A plus the quantity purchased. The algorithm prints the total cost, including 6% sales tax. - - ```ruby - def calculate_total(price, quantity, tax = 0.06) - return (price * quantity) * (1.0 + tax) - end - ``` - - ```plaintext - Get values for `price` and `quantity` - Set the value of `tax` to 1.06 - Set the vlue of `total` to `price` multiplied by `quantity` multiplied by `tax` - Return the value of `total` - ``` - 5. Write an if/then/else primitive to do each of the following operations: * a. Compute and display the value `x / y` if the value of `y` is not `0`. if `y` does have the value `0`, then display the message `Unable to perform the diff --git a/assignments/a1.rb b/assignments/a1.rb new file mode 100644 index 0000000..d09208d --- /dev/null +++ b/assignments/a1.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +def bsearch(target, items) + min = 0 + max = items.size + mid = (max - min) / 2 + + while (mid > min && mid < max) do + current = items[mid] + + if target == current + return true + elsif target > current + min = mid + else + max = mid + end + + mid = ((max - min) / 2) + min + end + + return false +end + +items = [3, 6, 7, 9, 12, 14, 18, 21, 22, 31, 43] +puts items.inspect +puts [1, bsearch(1, items)].inspect +puts [35, bsearch(35, items)].inspect +puts [22, bsearch(22, items)].inspect +puts [44, bsearch(44, items)].inspect |
