data structure for MovingTotal()

im struggling with this excercise, i tried to do some but i can’t get it

I have to write a code, that can store and check if the total of any three successively added elements is equal to a given total. For example, new MovingTotal() creates an empty container with no existing totals. Append({1,2,3,4}), which means that there are two existing totals (1+2+3=6, 2+3+4=9). Append ({5) appends element 5 and creates an additional total from {3,4,5}. There would now be the totals (same as before and 3+3+5=12). At this point Contains(6), Contains(9), and Contains(12) should return true, Contains(7) should return false.

Could somebody tell me how should i start?

using System; using System.Collections.Generic;  public class MovingTotal {     public List<int> Append(int[] list)     {        throw new InvalidOperationException("Waiting to be implemented.");     }      public bool Contains(int total)     {       throw new InvalidOperationException("Waiting to be implemented.");     }      public static void Main(string[] args)     {         MovingTotal movingTotal = new MovingTotal();          movingTotal.Append(new int[] { 1, 2, 3, 4 });          Console.WriteLine(movingTotal.Contains(6));         Console.WriteLine(movingTotal.Contains(9));         Console.WriteLine(movingTotal.Contains(12));         Console.WriteLine(movingTotal.Contains(7));          movingTotal.Append(new int[] { 5 });          Console.WriteLine(movingTotal.Contains(6));         Console.WriteLine(movingTotal.Contains(9));         Console.WriteLine(movingTotal.Contains(12));         Console.WriteLine(movingTotal.Contains(7));     } } 
Download script demo [LINK]
Download script demo [LINK 2]
Download script demo [LINK 2]
Professor Asked on October 12, 2020 in Android langue.
Add Comment
1 Answer(s)

problem solved
look at the C# code

 

public class MovingTotal
{
List<int[]> storedArray = new List<int[]>();
List<int> items = new List<int>();

public void Append(int[] list)
{
//storedArray.Add(list);
this.items.AddRange(list);
}

public bool Contains(int total)
{
int[] array = this.items.ToArray();
var size = array.Length – 1;
var indexToMove = 0;
var sum = 0;
while (sum <= total)
{
if (sum == total) return true;

if (indexToMove > size || indexToMove + 1 > size || indexToMove + 2 > size) return false;

var number0 = array[indexToMove];
var number1 = array[indexToMove + 1];
var number2 = array[indexToMove + 2];
sum = number0 + number1 + number2;

if (sum == total) return true;
indexToMove += 1;
}
return false;
}

public static void Main(string[] args)
{
MovingTotal movingTotal = new MovingTotal();

movingTotal.Append(new int[] { 1, 2, 3, 4 });

Console.WriteLine(movingTotal.Contains(6));
Console.WriteLine(movingTotal.Contains(9));
Console.WriteLine(movingTotal.Contains(12));
Console.WriteLine(movingTotal.Contains(7));

movingTotal.Append(new int[] { 5 });

Console.WriteLine(movingTotal.Contains(6));
Console.WriteLine(movingTotal.Contains(9));
Console.WriteLine(movingTotal.Contains(12));
Console.WriteLine(movingTotal.Contains(7));
}
}

Download demo
Default Answered on February 18, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.