how to write this code by standard meta language(ML)? Write a function numbersTo
ID: 3881766 • Letter: H
Question
how to write this code by standard meta language(ML)?
Write a function numbersToSum that takes anint value (called sum which you can assume is positive), and an int list (called L - which you can assume contains positive numbers) and returns an intlist. The returned list should include the first n elements from the input list L such that the first n elements of the list add to less than sum, but the first (n + 1) elements of the list add to sum or more. Assume the entire list sums to more than the passed in sum value. The type of numbersToSum should be int -> int list ->int list. Examples: [10, 20, 30 [5, 4, 6, 10, 4 [1 numbersToSum 100 [10, 20, 30,40 numbersToSum 30 [5, 4, 6, 10, 4, 2, 1, 5] numbersToSum 1 [2] numbersToSum 1 []Explanation / Answer
function main()
function lst = numbersToSum(sum, L)
lst = []
total = 0
for i = 1:length(L)
total = total + L(i)
if total <= sum
lst(i)=L(i)
end
end
end
L = [5,4,6,10,4,2,1,5]
sum = 30
numbersToSum(sum, L)
end
main()
Output:
L =
5 4 6 10 4 2 1 5
sum = 30
lst = [](0x0)
total = 0
total = 5
lst = 5
total = 9
lst =
5 4
total = 15
lst =
5 4 6
total = 25
lst =
5 4 6 10
total = 29
lst =
5 4 6 10 4
total = 31
total = 32
total = 37
ans =
5 4 6 10 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.