Problem 4 Write maxrun , a function which returns the length of the longest sublist of negative or non-negative numbers and a list of absolute values of the original list elements. Ensure that this...

1 answer below »

7


Ocaml Question


Problem 4 Write maxrun , a function which returns the length of the longest sublist of negative or<br>non-negative numbers and a list of absolute values of the original list elements. Ensure that this list is<br>in the same order.<br>To report multiple outputs, use a record. We'll define this particular output record as run_output :<br>type run_output =<br>{<br>length : int;<br>entries : int list;<br>}<br>Template:<br>let maxrun (l:int list) : run_output =<br>(** YOUR CODE HERE **)<br>Sample Unit Test<br>let out = moveNeg [8; -5; 3; 0; 10; -4] in<br>if out.length = 3<br>&& out.entries = [8; 5; 3; 0; 10; 4]<br>then print_string

Extracted text: Problem 4 Write maxrun , a function which returns the length of the longest sublist of negative or non-negative numbers and a list of absolute values of the original list elements. Ensure that this list is in the same order. To report multiple outputs, use a record. We'll define this particular output record as run_output : type run_output = { length : int; entries : int list; } Template: let maxrun (l:int list) : run_output = (** YOUR CODE HERE **) Sample Unit Test let out = moveNeg [8; -5; 3; 0; 10; -4] in if out.length = 3 && out.entries = [8; 5; 3; 0; 10; 4] then print_string "YAY" else raise (Failure "00PS");

Answered 122 days AfterJun 04, 2022

Answer To: Problem 4 Write maxrun , a function which returns the length of the longest sublist of negative or...

Ajay answered on Oct 04 2022
70 Votes
Codes
run_output = {
   l = maxrun,
   list = abs_list
}
function maxrun(list)
   max = -1
 
 count = 1
   current = list[1]
   for i = 2,#list do
       element = list[i]
       if element >= 0 and current >= 0 then
           count = count + 1
       elseif element < 0 and current < 0 then
           count = count + 1
       else
           count = 1
       end
       if count > max then
           max = count
       end
       current = element
   end
   return {
       l = max,
       list = list
   }
end
function abs(list)
   for i = 1,#list do
       list[i] = math.abs(list[i])
   end
   return list
end
abs_list = abs([-6, 2, -3, -4, 9, -8,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here