[Solved] COT5930 Homework5-IndexedSeq

$25

File Name: COT5930_Homework5-IndexedSeq.zip
File Size: 263.76 KB

SKU: [Solved] COT5930 Homework5-IndexedSeq Category: Tag:
5/5 - (1 vote)

Problem 1.

Write the solution in a file called p1.scala, in an object called p1.

isSorted tha
  1. Write a functiont checks that an IndexedSeq object is in sorted order using a function argument less: (A,A) => Boolean.
  2. Consider the quicksort function you wrote for Homework 4. Define a set of properties for the quicksort function, including checking for ascending order using the isSorted function from part a). Address all cases: empty vectors, vectors of one element, and vectors of more than one element. Also, compare results with the standard library sorted

Write a program that tests those properties using the Prop/Gen classes from the Gen.scala file. The solution is graded also on the correct definition of properties.

(Hint: Gen.listOf generates lists and quicksort works with IndexedSeq. One could define a Gen and a sized generator for Vector or just convert back and forth between List and Vector.)

Problem 2.

Write the solution in a file called p2.scala, in an object called p2. Click for file Monoid.scala. a) Write a function with this signature:

def getMinMax[A](v: IndexedSeq[A])(less: (A,A) => Boolean): MinMax[A]

getMinMax returns the minimum and the maximum values of an object IndexedSeq[A] wrapped in a

MinMax[A] object defined like this: case class MinMax[A](min:Option[A], max:Option[A])

If the sequence is empty, then the result is MinMax[A](None,None).

If the sequence has one element x, then the result is MinMax[A](Some(x),Some(x)).

Example: getMinMax for Vector(1, -3, 0, 5, 2, 4) using less = _ < _ returns

MinMax[A](Some(-3),Some(5)).

To get any credit your solution must do the following:

  • define a monoid for the MinMax[A] type, using the less: (A,A) => Boolean) function given as argument to getMinMax.
  • Use the foldMapV function described in the tetbook/notes to compute the MinMax[A] object with the minimum and maximum values of the v

Write a main method that demonstrates how the getMinMax function is used.

Hint: get inspiration from function ordered(ints: IndexedSeq[Int]): Boolean from the textbook/notes. b) Prove formally that the MinMax[A] monoid follows the two monoid laws.

Problem 3. Extra Credit 10 points

A company selling used phones on eBay keeps its inventory information in a CSV file that looks like this:

Product ID Name Unit cost $ Sell price $ Stock Sold
2324 Apple iPhone 6s 189 210 5 2
9842 Motorola G6 146 155 10 7
4471 Samsung Galaxy S7 190 199 3 5

The Canvas homework page includes a link to this file.

Write a program with the IO monad code from the textbook (following the principles from Chapter 13) that reads from the terminal the name of a file with this format and then computes and displays the total cost, revenue, and profit from items sold across the inventory.

Feel free to interpret what these terms mean and to select the output format.

Make sure you comment your code.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] COT5930 Homework5-IndexedSeq
$25