Last active
January 10, 2023 06:41
-
-
Save sholfen/20c6be9016e925e1cbc217b3c09a3196 to your computer and use it in GitHub Desktop.
answer1.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace OurSkyTestConsoleApp | |
{ | |
public class answer1 | |
{ | |
public void Run() | |
{ | |
List<string> list = new List<string> { "Hi", "Hello", "HelloWorld", "HiWorld", "HelloWorldWideWeb", "HelloWWW" }; | |
//Output = "HelloWorldWideWeb" | |
//List<string> list = new List<string> { "Oursky", "OurSky", "OurskyLimited", "OurskyHK", "SkymakersDigitalLTD", "SkymakersDigitalLtd" }; | |
//Output = "SkymakersDigitalLTD" | |
ConcurrentBag<(string word, int count, int index)> finalResult = new ConcurrentBag<(string word, int count, int index)>(); | |
Parallel.ForEach(list, (x, state, index) => | |
{ | |
var result = Check(list, (int)index, x); | |
finalResult.Add(result); | |
}); | |
var items = from i in finalResult.OrderByDescending(x => x.count).ThenByDescending(x => x.word.Length).ThenBy(x => x.index) | |
select i; | |
foreach (var item in items) | |
{ | |
Console.WriteLine($"{item.index},{item.word}: {item.count}"); | |
} | |
} | |
public (string word, int count, int index) Check(List<string> input, int index, string word) | |
{ | |
(string word, int count, int index) result = (word, 0, index); | |
foreach (string item in input) | |
{ | |
if (item == word) | |
{ | |
continue; | |
} | |
if (word.Contains(item)) | |
{ | |
result.count += 1; | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment