Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609200
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:57:11+00:00 2026-05-22T00:57:11+00:00

Take a look at this code: ColorResult contains Index, Color Name, and Probability Colors.Add(new

  • 0

Take a look at this code:

        ColorResult contains Index, Color Name, and Probability

        Colors.Add(new ColorResult(1, "Unknown", 5f));
        Colors.Add(new ColorResult(2, "Blue", 80f));
        Colors.Add(new ColorResult(3, "Blue", 80f));
        Colors.Add(new ColorResult(4, "Green", 40f));
        Colors.Add(new ColorResult(5, "Blue", 80f));
        Colors.Add(new ColorResult(6, "Blue", 80f));
        Colors.Add(new ColorResult(7, "Red", 20f));
        Colors.Add(new ColorResult(8, "Blue", 80f));
        Colors.Add(new ColorResult(9, "Green", 5f));

Using LINQ, how would you accomplish the following:

1) Working sequentially, replace all items at the start of the List<> that have a Probability lower than 60 when the first TWO items that follow with a probability higher than 60 have the same value (“Unknown” becomes “Blue” because #2 and #3 are Blue and have Probability of 60+)

2) Replace any item with a probability lower than 60 that’s surrounded with FOUR neighbors have the same value (“Green” becomes “Blue” because #2, #3, #5 and #6 are Blue and have Probability of 60+)

3) Working sequentially, replace any items at the end of the List<> that are preceded by TWO items with the same value (same as the first part, but in reverse). In the sample data, nothing would happen to #9 since #7 would need to be “Blue” and would need 60+ Probability.

This is pretty easy with loops, but I am absolutely stumped on how to compare sequential “neighbors” in LINQ.

This was my original solution for part 1:

        bool partOneCompleted = false;
        for (int i = 0; i < Colors.Count; i++)
        {
            if (Colors[i].ResultConfidence > 60)
            {
                // This item does not need to be changed
                partOneCompleted = true;
            }
            if (!partOneCompleted)
            {
                int twoItemsAway = i + 2;
                if (twoItemsAway < Colors.Count)
                {
                    if (Colors[twoItemsAway].Name == Colors[twoItemsAway - 1].Name && Colors[twoItemsAway].ResultConfidence > 60 && Colors[twoItemsAway - 1].ResultConfidence > 60)
                    {
                        // The next item, and the one after that both have the same value and 60+ confidence
                        for (int loopBack = i; loopBack >= 0; loopBack--)
                        {
                            Colors[loopBack].Name = Colors[twoItemsAway].Name;
                        }

                        partOneCompleted = true;
                    }
                }
            }
        }

Can any LINQ experts please share the most efficient implementation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:57:12+00:00Added an answer on May 22, 2026 at 12:57 am

    I started with a test for part 1:

    [Test]
    public void Should_convert_leading_low_probability_colors()
    {
        var colors = new List<ColorResult>
            {
                new ColorResult(1, "Unknown", 5f),
                new ColorResult(2, "Blue", 80f),
                new ColorResult(3, "Blue", 80f),
                new ColorResult(4, "Green", 40f),
                new ColorResult(5, "Blue", 80f),
                new ColorResult(6, "Blue", 80f),
                new ColorResult(7, "Red", 20f),
                new ColorResult(8, "Blue", 80f),
                new ColorResult(9, "Green", 5f)
            };
    
        ConvertLeadingLowProbabilityColors(colors);
    
        foreach (var colorResult in colors)
        {
            Console.WriteLine(colorResult.Index + " " + colorResult.Color);
        }
    
        colors[0].Color.ShouldBeEqualTo("Blue");
        colors[1].Color.ShouldBeEqualTo("Blue");
        colors[2].Color.ShouldBeEqualTo("Blue");
        colors[3].Color.ShouldBeEqualTo("Green");
        colors[4].Color.ShouldBeEqualTo("Blue");
        colors[5].Color.ShouldBeEqualTo("Blue");
        colors[6].Color.ShouldBeEqualTo("Red");
        colors[7].Color.ShouldBeEqualTo("Blue");
        colors[8].Color.ShouldBeEqualTo("Green");
    }
    

    and implementation

    private void ConvertLeadingLowProbabilityColors(IList<ColorResult> colors)
    {
        var leadingBelow60 = Enumerable
            .Range(0, colors.Count)
            .TakeWhile(index => colors[index].Probability < 60)
            .ToList();
        if (leadingBelow60.Count > 0 && leadingBelow60.Count < colors.Count - 2)
        {
            int lastIndex = leadingBelow60.Last();
            var firstNext = colors[lastIndex + 1];
            var secondNext = colors[lastIndex + 2];
            if (firstNext.Probability > 60 &&
                secondNext.Probability > 60 &&
                firstNext.Color == secondNext.Color)
            {
                leadingBelow60.ForEach(index => colors[index].Color = firstNext.Color);
            }
        }
    }
    

    then added a test for part 3, since it is a variation of part 1:

    [Test]
    public void Should_convert_trailing_low_probability_colors()
    {
        var colors = new List<ColorResult>
            {
                new ColorResult(1, "Unknown", 5f),
                new ColorResult(2, "Blue", 80f),
                new ColorResult(3, "Blue", 80f),
                new ColorResult(4, "Green", 40f),
                new ColorResult(5, "Blue", 80f),
                new ColorResult(6, "Blue", 80f),
                new ColorResult(7, "Red", 20f),
                new ColorResult(8, "Blue", 40f),
                new ColorResult(9, "Green", 5f)
            };
    
        ConvertTrailingLowProbabilityColors(colors);
    
        foreach (var colorResult in colors)
        {
            Console.WriteLine(colorResult.Index + " " + colorResult.Color);
        }
    
        colors[0].Color.ShouldBeEqualTo("Unknown");
        colors[1].Color.ShouldBeEqualTo("Blue");
        colors[2].Color.ShouldBeEqualTo("Blue");
        colors[3].Color.ShouldBeEqualTo("Green");
        colors[4].Color.ShouldBeEqualTo("Blue");
        colors[5].Color.ShouldBeEqualTo("Blue");
        colors[6].Color.ShouldBeEqualTo("Blue");
        colors[7].Color.ShouldBeEqualTo("Blue");
        colors[8].Color.ShouldBeEqualTo("Blue");
    }
    

    and implementation:

    private void ConvertTrailingLowProbabilityColors(IList<ColorResult> colors)
    {
        var trailingBelow60 = Enumerable
            .Range(0, colors.Count)
            .Select(i => colors.Count - 1 - i)
            .TakeWhile(index => colors[index].Probability < 60)
            .ToList();
        if (trailingBelow60.Count > 0 && trailingBelow60.Count < colors.Count - 2)
        {
            int lastIndex = trailingBelow60.Last();
            var firstPrevious = colors[lastIndex - 1];
            var secondPrevious = colors[lastIndex - 2];
            if (firstPrevious.Probability > 60 &&
                secondPrevious.Probability > 60 &&
                firstPrevious.Color == secondPrevious.Color)
            {
                trailingBelow60.ForEach(index => colors[index].Color = firstPrevious.Color);
            }
        }
    }
    

    then, I tackled part 2. Again I started with a test:

    [Test]
    public void Should_convert_surrounded_low_probability_colors()
    {
        var colors = new List<ColorResult>
            {
                new ColorResult(1, "Unknown", 5f),
                new ColorResult(2, "Blue", 80f),
                new ColorResult(3, "Blue", 80f),
                new ColorResult(4, "Green", 40f),
                new ColorResult(5, "Blue", 80f),
                new ColorResult(6, "Blue", 80f),
                new ColorResult(7, "Red", 20f),
                new ColorResult(8, "Blue", 80f),
                new ColorResult(9, "Green", 5f)
            };
    
        ConvertSurroundedLowProbabilityColors(colors);
    
        foreach (var colorResult in colors)
        {
            Console.WriteLine(colorResult.Index + " " + colorResult.Color);
        }
    
        colors[0].Color.ShouldBeEqualTo("Unknown");
        colors[1].Color.ShouldBeEqualTo("Blue");
        colors[2].Color.ShouldBeEqualTo("Blue");
        colors[3].Color.ShouldBeEqualTo("Blue");
        colors[4].Color.ShouldBeEqualTo("Blue");
        colors[5].Color.ShouldBeEqualTo("Blue");
        colors[6].Color.ShouldBeEqualTo("Red");
        colors[7].Color.ShouldBeEqualTo("Blue");
        colors[8].Color.ShouldBeEqualTo("Green");
    }
    

    and this implementation:

    private void ConvertSurroundedLowProbabilityColors(IList<ColorResult> colors)
    {
        var surrounding4Modification = new Surrounding4ModificationStrategy();
        foreach (int index in Enumerable
            .Range(0, colors.Count)
            .Where(index => surrounding4Modification.IsMatch(colors, index)))
        {
            surrounding4Modification.Update(colors, index);
        }
    }
    

    This time it seemed cleaner to pull out a helper class:

    public class Surrounding4ModificationStrategy
    {
        public bool IsMatch(IList<ColorResult> input, int index)
        {
            if (index < 2)
            {
                return false;
            }
            if (index >= input.Count - 2)
            {
                return false;
            }
            if (input[index].Probability >= 60)
            {
                return false;
            }
    
            var secondPrevious = input[index - 2];
            if (secondPrevious.Probability < 60)
            {
                return false;
            }
            var firstPrevious = input[index - 1];
            if (firstPrevious.Probability < 60)
            {
                return false;
            }
    
            var firstNext = input[index + 1];
            if (firstNext.Probability < 60)
            {
                return false;
            }
            var secondNext = input[index + 2];
            if (secondNext.Probability < 60)
            {
                return false;
            }
    
            if (new[] { secondPrevious.Color, firstPrevious.Color, firstNext.Color, secondNext.Color }.Distinct().Count() > 1)
            {
                return false;
            }
            return true;
        }
    
        public void Update(IList<ColorResult> input, int index)
        {
            input[index].Color = input[index + 1].Color;
        }
    }
    

    Finally, I created a consolidated test with your data:

    [Test]
    public void Should_convert_all_low_probability_colors()
    {
        var colors = new List<ColorResult>
            {
                new ColorResult(1, "Unknown", 5f),
                new ColorResult(2, "Blue", 80f),
                new ColorResult(3, "Blue", 80f),
                new ColorResult(4, "Green", 40f),
                new ColorResult(5, "Blue", 80f),
                new ColorResult(6, "Blue", 80f),
                new ColorResult(7, "Red", 20f),
                new ColorResult(8, "Blue", 80f),
                new ColorResult(9, "Green", 5f)
            };
    
        ConvertLowProbabilityColors(colors);
    
        foreach (var colorResult in colors)
        {
            Console.WriteLine(colorResult.Index + " " + colorResult.Color);
        }
    
        colors[0].Color.ShouldBeEqualTo("Blue");
        colors[1].Color.ShouldBeEqualTo("Blue");
        colors[2].Color.ShouldBeEqualTo("Blue");
        colors[3].Color.ShouldBeEqualTo("Blue");
        colors[4].Color.ShouldBeEqualTo("Blue");
        colors[5].Color.ShouldBeEqualTo("Blue");
        colors[6].Color.ShouldBeEqualTo("Red");
        colors[7].Color.ShouldBeEqualTo("Blue");
        colors[8].Color.ShouldBeEqualTo("Green");
    }
    

    and an implementation that uses the methods created above:

    public void ConvertLowProbabilityColors(IList<ColorResult> colors)
    {
        ConvertLeadingLowProbabilityColors(colors);
        ConvertSurroundedLowProbabilityColors(colors);
        ConvertTrailingLowProbabilityColors(colors);
    }
    

    Were this my code base I would go on to add tests around edge cases like: all items having probability < 60 for parts 1 and 3; all but last … for part 1; all but first for part 3; etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a look at this code: public class Test { public static void main(String...
take a look at this code: $(document).ready(function() { document.getElementById(sliderId).onmousedown = sliderMouseDown; }); function sliderMouseDown()
Take a look on this code, what I wrote: http://pastebin.com/TZpUAWpA , and this jQuery,
Please take a look at this code: template<class T> class A { class base
Take a look at this code : public class Program { [import]IMain Main {get;
Take a look at this code: #include <iostream> using namespace std; class A {
For example, take a look at this code (from tspl4): (define proc1 (lambda (x
Hey guys, take a look at this, maybe you can help me. This code
Take a look at this code: Foo &Bar::Copy() { return Bar(); } The class
Please take a look at this simple code: http://jsfiddle.net/kerp3/ The box has an inner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.