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

Detecting interesting car mileages

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

Problem

I've had a go at the following challenge (from codewars):


Interesting car mileages are 3-or-more digit numbers that meet one or more of the following criteria:



  • Any digit followed by all zeros: 100, 90000



  • Every digit is the same number: 1111



  • The digits are sequential, incementing†: 1234



  • The digits are sequential, decrementing‡: 4321



  • The digits are a palindrome: 1221 or 73837



  • The digits match one of the values in the awesomePhrases array





† For incrementing sequences, 0 should come after 9, and not before 1, as in 7890.
‡ For decrementing sequences, 0 should come after 1, and not before 9, as in 3210.


Complete the method IsInteresting, so that it returns:



  • 2 if a mileage is interesting



  • 1 if an interesting mileage occurs within the next 2 miles



  • 0 for any other number





Mileages will be in the range 0 to 1,000,000,000. awesomePhrases will always be supplied, but may be empty.

In order to support development, I wrote the following tests (apart from ShouldWorkTest, which was supplied with the challenge).

```
using NUnit.Framework;
using System.Collections.Generic;

[TestFixture, Category("MileageChallenge")]
public class CarMileageTests
{
public const int NotInteresting = 0;
public const int AlmostInteresting = 1;
public const int Interesting = 2;

[TestCase(0)]
[TestCase(97)]
//[TestCase(98)] // Almost Interesting
//[TestCase(99)] // Almost Interesting
public void NumbersBelow100AreNotInteresting(int mileage)
{
Assert.AreEqual(NotInteresting, CarMileage.IsInteresting(mileage, new List { }));
}

[TestCase(100)]
[TestCase(7000)]
[TestCase(900000000)]
public void NumbersFollowedByOnlyZeroAreInteresting(int mileage)
{
Assert.AreEqual(Interesting, CarMileage.IsInteresting(mileage, new List { }));
}

[TestCase(102)]
[TestCase(7700)]
[TestCase(900700000)]
public void NumbersIntermingledWithZ

Solution

The first thing that strikes me is the repetition of the three constants in the code and in the tests:

const int NotInteresting = 0;
    const int AlmostInteresting = 1;
    const int Interesting = 2;


I'd make them internal and set the InternalsVisibleTo to the test.

IsSequence

For this one I'd use the OrderBy[Descending] + SequenceEqual. This would make it virtually a one-liner.

Code Snippets

const int NotInteresting = 0;
    const int AlmostInteresting = 1;
    const int Interesting = 2;

Context

StackExchange Code Review Q#143095, answer score: 2

Revisions (0)

No revisions yet.