7orlum
BAN USERusing System;
using System.Collections.Generic;
using System.Linq;
namespace Ed1
{
public class Program
{
public static void Main(string[] args)
{
List<int> values = new List<int>(args.Length);
foreach (string arg in args)
values.Add(int.Parse(arg));
int resultPosition = 0;
int resultLength = 0;
FindSequence(values, ref resultPosition, ref resultLength);
string result = "";
for (int i = resultPosition; i < resultPosition + resultLength; i++)
result = result + values[i] + " ";
Console.WriteLine(result);
}
public static void FindSequence(IList<int> values, ref int resultPosition, ref int resultLength)
{
resultLength = 0;
resultPosition = 0;
int position = 0;
while (position < values.Count())
{
int length = SequenceLength(values, position);
if (length > resultLength)
{
resultLength = length;
resultPosition = position;
}
position = position + length;
}
}
static int SequenceLength(IList<int> values, int start)
{
if (start < 0 || start >= values.Count())
throw new IndexOutOfRangeException("start");
if (start == values.Count() - 1)
return 1;
if (values[start] == values[start + 1])
return 1;
int i = start + 2;
while
(
i < values.Count() &&
(
values[i - 2] < values[i - 1] &&
values[i] < values[i - 1]
||
values[i - 2] > values[i - 1] &&
values[i] > values[i - 1]
)
)
i = i + 1;
return i - start;
}
}
}
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ed1;
namespace Ed1UnitTest
{
[TestClass]
public class UnitTest
{
[TestMethod]
public void TestMethod1()
{
int resultPosition = 0;
int resultLength = 0;
int[] values = {1};
Ed1.Program.FindSequence(values, ref resultPosition, ref resultLength);
Assert.AreEqual(resultPosition, 0);
Assert.AreEqual(resultLength, 1);
}
[TestMethod]
public void TestMethod2()
{
int resultPosition = 0;
int resultLength = 0;
int[] values = { };
Ed1.Program.FindSequence(values, ref resultPosition, ref resultLength);
Assert.AreEqual(resultPosition, 0);
Assert.AreEqual(resultLength, 0);
}
[TestMethod]
public void TestMethod3()
{
int resultPosition = 0;
int resultLength = 0;
int[] values = {0, 2 };
Ed1.Program.FindSequence(values, ref resultPosition, ref resultLength);
Assert.AreEqual(resultPosition, 0);
Assert.AreEqual(resultLength, 2);
}
[TestMethod]
public void TestMethod4()
{
int resultPosition = 0;
int resultLength = 0;
int[] values = {-1, -1, 4, 2, 5, 3, 2, 5, 0};
Ed1.Program.FindSequence(values, ref resultPosition, ref resultLength);
Assert.AreEqual(resultPosition, 1);
Assert.AreEqual(resultLength, 5);
}
[TestMethod]
public void TestMethod5()
{
int resultPosition = 0;
int resultLength = 0;
int[] values = { -1, -1, 4, 2, 5, 3 };
Ed1.Program.FindSequence(values, ref resultPosition, ref resultLength);
Assert.AreEqual(resultPosition, 1);
Assert.AreEqual(resultLength, 5);
}
}
}
We note that the distance between the three words will always be equal to twice the distance between the two extreme words.
- 7orlum April 01, 2016