HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

“Critter Tracking: When does it cross its own path?” Part 2

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
trackingpathcrosscritterpartitsowndoeswhen

Problem

Basic Info: This question is my second attempt at this question. It is based on a question similar to this Codility question. The input (an int array), the outpout (an integer) and the method signature (public int solution(int[] A)) are given.

Situation:


A critter starts at (0, 0) on a Cartesian graph. We have a non-empty zero-indexed "moves" list that contains numbers. Each number represents the distance moved. (Similar to this question)
The first number is the distance north, the second is the distance east, the third is the distance south, the fourth is the distance west, and repeats like this forever. Therefore the directions cycle every four moves.

Goal:


Find an algorithm that gives the move number that makes the critter cross a point that it has already visited before. The move number is the index of the "moves" list.

Example:


If given this move list: [1, 3, 2, 5, 4, 4, 6, 3, 2]

The answer is then 6. (It's the 7th move).

Draw it on a graph, the turtle will go:

(0,0) -> (0,1) -> (3,1) -> (3,-1) -> (-2,-1) -> (-2,3) -> (2,3) -> (2,-3)

At the 6th index (move number 7th) it will meet (2,1) which is a point that the turtle has already crossed.

Notes:


n = moves

m = avg steps per move

Algorithm should preferably be O(n).

algorithm space Complexity should be __?

n (Number of moves) is an integer between 1 and 100,000

m (distance per move) is a positive integers between 1 and 1,000

"No collision" should return -1

I initially had a Dictionary> to track the path a critter traveled for detecting when he crossed he trail. That answer was O(n) in complexity but O(crap) in space usage when the lines got long - a walk of 1k steps could add as many lists - then as many points on the next step.

Thanks to this answer I was given then idea of tracking line segments - and then comparing the newest line segment with previous for cross overs. (I've also worked on some of the naming and layout inconsistencies)

It see

Solution

Your SequenceDoubler method posted at https://dotnetfiddle.net/8ByC2b could be improved. Here's the original version:

private static IEnumerable SequenceDoubler(int n1, int n2, double step = 0.5)
{
    double d1 = n1;
    double d2 = n2;

    while (d1 <= d2)
    {
        d1 += step;
        yield return (int) d1;
    }
}


The parameter and local variable names aren't very readable, it uses doubles when it doesn't have to, and step isn't very intuitive. What if, for some reason, you wanted to triple the sequence? Here is an improved version using LINQ:

private static IEnumerable SequenceDoubler(int start, int finish, int repeatAmount = 2)
{
    int count = finish - start + 1;
    return
        from x in Enumerable.Range(start, count)
        from _ in Enumerable.Repeat(0, repeatAmount)
        select x;
}


If you prefer to use loops, you can use this version:

private static IEnumerable SequenceDoubler(int start, int finish, int repeatAmount = 2)
{
    for (int i = start; i <= finish; i++)
    {
        for (int j = 0; j < repeatAmount; j++)
        {
            yield return i;
        }
    }
}

Code Snippets

private static IEnumerable<int> SequenceDoubler(int n1, int n2, double step = 0.5)
{
    double d1 = n1;
    double d2 = n2;

    while (d1 <= d2)
    {
        d1 += step;
        yield return (int) d1;
    }
}
private static IEnumerable<int> SequenceDoubler(int start, int finish, int repeatAmount = 2)
{
    int count = finish - start + 1;
    return
        from x in Enumerable.Range(start, count)
        from _ in Enumerable.Repeat(0, repeatAmount)
        select x;
}
private static IEnumerable<int> SequenceDoubler(int start, int finish, int repeatAmount = 2)
{
    for (int i = start; i <= finish; i++)
    {
        for (int j = 0; j < repeatAmount; j++)
        {
            yield return i;
        }
    }
}

Context

StackExchange Code Review Q#95588, answer score: 3

Revisions (0)

No revisions yet.