Better way to do this ( using LINQ )? - c#

I am comparing two lists and in one case I am removing the uncommon elements from one list ( removing from lists with more number of elements ) and in the other case (else statement) I am adding the uncommon elements to one list ( adding to list with lesser elements )
I am able to do this using the below given code but I was hoping to achieve this using LINQ in a more concise manner. Please suggest me an equivalent LINQ code
if (receivedList.Count < AuthorFacets.Count)
{
for (int i = AuthorFacets.Count - 1; i >= 0; i--)
{
if (!receivedList.Contains(AuthorFacets[i]))
AuthorFacets.RemoveAt(i);
}
}
else
{
for (int i = 0; i < receivedList.Count; i++)
{
if (!AuthorFacets.Contains(receivedList[i]))
AuthorFacets.Add(receivedList[i]);
}
}

Using linq you can try this
if (receivedList.Count < AuthorFacets.Count)
{
AuthorFacets.RemoveAll(a=>!receivedList.Contains(a))
}
else
{
AuthorFactets.AddRange(receivedList.Where(r=> !AuthorFacets.Contains(r)))
}

How about this?
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var receivedList = new List<string>();
var AuthorFacets = new List<string>();
receivedList.Add("2");
receivedList.Add("4");
receivedList.Add("6");
AuthorFacets.Add("1");
AuthorFacets.Add("2");
AuthorFacets.Add("3");
if (receivedList.Count < AuthorFacets.Count)
{
AuthorFacets = AuthorFacets.Where(i => receivedList.Contains(i)).ToList();
}
else
{
AuthorFacets.AddRange(receivedList.Where(i => !AuthorFacets.Contains(i)));
}
Console.WriteLine(string.Join("\n",AuthorFacets));
}
}
Source Code: https://dotnetfiddle.net/Hz8anK

Related

How can I prevent StackOverflowException in my code

In my code, I cannot figure out why I keep getting 'Process is terminating due to StackOverflowException.' only on the second output.
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace _2018JuniorQ5
{
class Program
{
//Variable Decleration
public static int pages = 0;
public static string[] bookFormat;
public static List<string> alreadyChecked = new List<string>();
public static List<string> nodesToCheck = new List<string>();
public static int level = 0;
public static List<string> childrenNodes = new List<string>();
public static void Main(string[] args)
{
//Get input
pages = Convert.ToInt32(Console.ReadLine());
bookFormat = new string[pages];
for (int x=0; x<pages; x++)
{
bookFormat[x] = Console.ReadLine();
}
//Display if all pages are reachable
Console.WriteLine(getReachablePages(1));
//Find shortest path
List<string> NodeBegin = new List<string>();
NodeBegin.Add("1");
Console.WriteLine(getShortestPath(NodeBegin));
}
public static string getReachablePages(int pageToCheck)
{
string[] options=(bookFormat[pageToCheck - 1]).Split(' ');
alreadyChecked.Add(Convert.ToString(pageToCheck));
for (int a=1; a<=Convert.ToInt32(options[0]); a++)
{
if (!alreadyChecked.Contains(options[a]))
{
getReachablePages(Convert.ToInt32(options[a]));
}
}
if (alreadyChecked.Count == pages)
{
return "Y";
}
else
{
return "N";
}
alreadyChecked.Clear();
}
public static int getShortestPath(List<string> nodesToCheck)
{
level++;
childrenNodes.Clear();
for (int q = 0; q < nodesToCheck.Count; q++)
{
string[] options = bookFormat[Convert.ToInt32(nodesToCheck[q])-1].Split(' ');
if (options[0] == "0")
{
return level;
}
else
{
for (int t = 1; t < options.Length; t++)
{
if (!alreadyChecked.Contains(options[t]))
{
childrenNodes.Add(options[t]);
alreadyChecked.Add(nodesToCheck[q]);
}
}
}
nodesToCheck.Clear();
}
return getShortestPath(childrenNodes);
}
}
}
The first output from the getReachablePages method works, and does not give any errors. However, the second output from the getShortestPath gives the "Process is terminating due to StackOverflowException" error. Can someone explain why the getReachablePages method works, but the getShortestPath method doesn't work?
The problem at the moment is that List<string> is a reference type, so when you pass childrenNodes to getShortestPath (getShortestPath(childrenNodes)), you're actually passing a reference to the same list in memory.
The next thing you do is call childrenNodes.Clear(), which empties that list. Because nodesToCheck and childrenNodes are both pointing at the same list, calling childrenNodes.Clear() means that you have no nodes to check.
Why does this cause a StackOverflowException? It causes one because you have no exit condition for when nodesToCheck is empty. You just keep calling the same method over and over.
I propose the following solution:
public static int getShortestPath(List<string> nodesToCheck)
{
if (!nodesToCheck.Any())
{
return -1;
}
var childrenNodes = new List<string>();
level++;
for (int q = 0; q < nodesToCheck.Count; q++)
{
string[] options = bookFormat[Convert.ToInt32(nodesToCheck[q])-1].Split(' ');
if (options[0] == "0")
{
return level;
}
else
{
for (int t = 1; t < options.Length; t++)
{
if (!alreadyChecked.Contains(options[t]))
{
childrenNodes.Add(options[t]);
alreadyChecked.Add(nodesToCheck[q]);
}
}
}
nodesToCheck.Clear();
}
return getShortestPath(childrenNodes);
}
When nodesToCheck is empty, return -1 (i.e. no path).
Create a new List<string> for childrenNodes within the getShortestPath method.
While this should fix your problem, I would recommend making your entire method self-contained. It's essentially a stateless method, but you're maintaining state outside the method, which led to the problem you have seen, and could lead to more problems if you call this method in a multi-threaded environment.
The other odd thing I noticed, is that you're looping through nodesToCheck, but the way your code is written means that you will only ever consider the first node because you clear nodesToCheck at the end of the first iteration, leaving the list empty. Further more, you're adding nodesToCheck[q] to alreadChecked once per item in options, which I'm sure can't be right.
I recommend learning to use the debugger in Visual Studio. The debugger allows you to step through your code line-by-line, inspect variables, etc. - this will help you locate problems in your code.
P.S. While it is not the correct solution to your problem, if you wish to copy a list you can use LINQ's ToList() method: var listB = listA.ToList();

How to sort list of strings of numbers in c# [duplicate]

I have a list of strings of version (see photo), and I'd like to sort them in descending order.
I've seen a few solutions using Version class to compare them, but I can't think of any solution that sort a whole list like this. What is the least complicated way to achieve this?
what is wrong with this simple implementation?
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var ver = new List<Version>();
ver.Add(new Version("3.5"));
ver.Add(new Version("3.15"));
ver.Add(new Version("3.10"));
ver.Add(new Version("3.1"));
ver.Sort();
ver.Reverse();
}
}
}
you can use IComparable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<string> data = new List<string>{
"3.5.0.1", "3.4.1.9", "3.4.1.56", "3.4.1.55", "3.4.1.46",
"3.4.1.45", "3.4.1.44", "3.4.1.30", "3.4.1.3", "3.4.1.22",
"3.4.1.2", "3.4.1.11", "3.4.1.0", "3.4.0.7", "3.4.0.3",
"3.4.0.1", "3.3.0.8", "3.3.0.4", "3.3.0.0", "3.2.0.9",
"3.2.0.6", "3.2.0.3", "3.2.0.27", "3.2.0.20", "3.2.0.15",
"3.2.0.1", "3.2.0.0", "3.1.0.7", "3.1.0.15", "3.1.0.14"
};
List<SortPara> sortPara = data.Select(x => new SortPara(x)).ToList();
data = sortPara.OrderBy(x => x).Select(x => x.strNumbers).ToList();
data = sortPara.OrderByDescending(x => x).Select(x => x.strNumbers).ToList();
}
}
public class SortPara : IComparable<SortPara>
{
public List<int> numbers { get; set; }
public string strNumbers { get; set; }
public SortPara(string strNumbers)
{
this.strNumbers = strNumbers;
numbers = strNumbers.Split(new char[] { '.' }).Select(x => int.Parse(x)).ToList();
}
public int CompareTo(SortPara other)
{
int shortest = this.numbers.Count < other.numbers.Count ? this.numbers.Count : other.numbers.Count;
int results = 0;
for (int i = 0; i < shortest; i++)
{
if (this.numbers[i] != other.numbers[i])
{
results = this.numbers[i].CompareTo(other.numbers[i]);
break;
}
}
return results;
}
}
}
know its an old thread but the easiest way is:
//list with some version numbers as strings
var versionList = new List<string>()
{
"1.1.0",
"1.10.0",
"1.112.0",
}
versionList.OrderByDescending(x => Version.Parse(x));
output:
1.112.0
1.10.0
1.1.0
You should use IComparable as jdweng, just edit a bit to compare versions like "2.1.0.4" and "2.1":
public int CompareTo(SortPara other)
{
int shortest = this.numbers.Count < other.numbers.Count ? this.numbers.Count : other.numbers.Count;
int results = 0;
for (int i = 0; i < shortest; i++)
{
if (this.numbers[i] != other.numbers[i])
{
results = this.numbers[i].CompareTo(other.numbers[i]);
break;
}
}
if (results != 0)
return results;
if (this.numbers.Count > other.numbers.Count)
return 1;
else if (this.numbers.Count < other.numbers.Count)
return -1;
else
return 0;
}
Here's a solution that will sort Semantic Versioning (https://semver.org). Nuget.Core offers a SemanticVersion class that operates very similarly to the standard Version class in .net. It will parse your string into an IComparable and IEquatable object so you can compare multiple versions or sort them within a collection, etc.
Nuget.Core: https://www.nuget.org/packages/nuget.core/ (you can pull this library via nuget)
https://github.com/NuGet/NuGet2/blob/2.13/src/Core/SemanticVersion.cs
var rawVersions = new [] {"v1.4.0", "v1.4.0-patch10", "v1.4.0-patch2"};
var versions = rawVersions.Select(v => new SemanticVersion(v));
var sorted = versions.ToList().Sort();
Sorry for late answer. we can sort version number this way too. here is a small example.
var ver = new List<string>();
ver.Add("3.5");
ver.Add("3.15");
ver.Add("3.10");
ver.Add("3.1");
var OrderData = ver.Select(x => new
{
version = x.ToString(),
vorder = x.ToString().Replace(".", "")
}).OrderByDescending(y => Convert.ToInt32(y.vorder)).ToList();

How to sort a list<string> / array of string version number?

I have a list of strings of version (see photo), and I'd like to sort them in descending order.
I've seen a few solutions using Version class to compare them, but I can't think of any solution that sort a whole list like this. What is the least complicated way to achieve this?
what is wrong with this simple implementation?
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var ver = new List<Version>();
ver.Add(new Version("3.5"));
ver.Add(new Version("3.15"));
ver.Add(new Version("3.10"));
ver.Add(new Version("3.1"));
ver.Sort();
ver.Reverse();
}
}
}
you can use IComparable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<string> data = new List<string>{
"3.5.0.1", "3.4.1.9", "3.4.1.56", "3.4.1.55", "3.4.1.46",
"3.4.1.45", "3.4.1.44", "3.4.1.30", "3.4.1.3", "3.4.1.22",
"3.4.1.2", "3.4.1.11", "3.4.1.0", "3.4.0.7", "3.4.0.3",
"3.4.0.1", "3.3.0.8", "3.3.0.4", "3.3.0.0", "3.2.0.9",
"3.2.0.6", "3.2.0.3", "3.2.0.27", "3.2.0.20", "3.2.0.15",
"3.2.0.1", "3.2.0.0", "3.1.0.7", "3.1.0.15", "3.1.0.14"
};
List<SortPara> sortPara = data.Select(x => new SortPara(x)).ToList();
data = sortPara.OrderBy(x => x).Select(x => x.strNumbers).ToList();
data = sortPara.OrderByDescending(x => x).Select(x => x.strNumbers).ToList();
}
}
public class SortPara : IComparable<SortPara>
{
public List<int> numbers { get; set; }
public string strNumbers { get; set; }
public SortPara(string strNumbers)
{
this.strNumbers = strNumbers;
numbers = strNumbers.Split(new char[] { '.' }).Select(x => int.Parse(x)).ToList();
}
public int CompareTo(SortPara other)
{
int shortest = this.numbers.Count < other.numbers.Count ? this.numbers.Count : other.numbers.Count;
int results = 0;
for (int i = 0; i < shortest; i++)
{
if (this.numbers[i] != other.numbers[i])
{
results = this.numbers[i].CompareTo(other.numbers[i]);
break;
}
}
return results;
}
}
}
know its an old thread but the easiest way is:
//list with some version numbers as strings
var versionList = new List<string>()
{
"1.1.0",
"1.10.0",
"1.112.0",
}
versionList.OrderByDescending(x => Version.Parse(x));
output:
1.112.0
1.10.0
1.1.0
You should use IComparable as jdweng, just edit a bit to compare versions like "2.1.0.4" and "2.1":
public int CompareTo(SortPara other)
{
int shortest = this.numbers.Count < other.numbers.Count ? this.numbers.Count : other.numbers.Count;
int results = 0;
for (int i = 0; i < shortest; i++)
{
if (this.numbers[i] != other.numbers[i])
{
results = this.numbers[i].CompareTo(other.numbers[i]);
break;
}
}
if (results != 0)
return results;
if (this.numbers.Count > other.numbers.Count)
return 1;
else if (this.numbers.Count < other.numbers.Count)
return -1;
else
return 0;
}
Here's a solution that will sort Semantic Versioning (https://semver.org). Nuget.Core offers a SemanticVersion class that operates very similarly to the standard Version class in .net. It will parse your string into an IComparable and IEquatable object so you can compare multiple versions or sort them within a collection, etc.
Nuget.Core: https://www.nuget.org/packages/nuget.core/ (you can pull this library via nuget)
https://github.com/NuGet/NuGet2/blob/2.13/src/Core/SemanticVersion.cs
var rawVersions = new [] {"v1.4.0", "v1.4.0-patch10", "v1.4.0-patch2"};
var versions = rawVersions.Select(v => new SemanticVersion(v));
var sorted = versions.ToList().Sort();
Sorry for late answer. we can sort version number this way too. here is a small example.
var ver = new List<string>();
ver.Add("3.5");
ver.Add("3.15");
ver.Add("3.10");
ver.Add("3.1");
var OrderData = ver.Select(x => new
{
version = x.ToString(),
vorder = x.ToString().Replace(".", "")
}).OrderByDescending(y => Convert.ToInt32(y.vorder)).ToList();

Trying to find specific word inside string without contain method. code isn't working the way it should

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sisesta suvaline tekst-->");
string tekst1 = Console.ReadLine();
// string tekst2 = ("ja");
char jtaht = ('j');
char ataht = ('a');
int jsidOntekstis = 0;
int asidOnTekstis = 0;
int tekstipikkus = tekst1.Length;
int jasidonTekstis = jsidOntekstis + asidOnTekstis;
int jasidEiOleTekstis=1;
for (int i = 0; i < tekstipikkus; i++)
{
if (tekst1[i] == jtaht)
{
jsidOntekstis++;
}
if (tekst1[i] == ataht)
{
asidOnTekstis++;
}
}
// for (int k = 0; i < tekstipikkus; i++)
{
{
if (jasidonTekstis > jasidEiOleTekstis)
{
Console.Write("Ja on tekstis olemas");
}
else
{
Console.Write("Ja-sid ei ole tekstis");
}
}
}
Console.ReadKey();
}
}
}
So This is my code and it isn't working the way it should. My teacher asked me to search for "ja" in text without contain method so we would think more logically. I completed all other exercises but this one. Thank you!
StackOverflow is actually not a place where people DO something for you.
They help you and tell you HOW to do this. This issue contains only the wrong piece of code and question "what's wrong".
First of all, I need to tell you that the first problem is, obviously, the algorithm.
I can't understand what is your code supposed to do because even you don't understand it.
using System;
using System.Text;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Where to search -->");
string text = Console.ReadLine();
string pattern = "ja"; // Probably, it is better to get it from Console as well
for (int i = 0; i < text.Length; i++)
{
for (int j = 0; j < pattern.Length; j++)
{
if (text[i+j] == pattern[j] && j == pattern.Length - 1)
Console.WriteLine(i);
if (text[i+j] != pattern[j]) break;
}
}
}
}
}
Here is the (not a best) code which searches for the pattern in the text without Contains method. It iterates through the text string - and if it meets the first character of pattern string - it goes further comparing characters one by one in a row. If the inner loop iterated till the end then it means that text string contains pattern string and outputs it's position. If in any moment characters are not equal then the inner loop breaks and continues the main loop.
Research it and understand it. Then you can solve the problem.

Concat error in C#

I have a quick sort program using lists.
The error is in the quick sort function return statement.
System.Collections.List does not contain definition for Concat and the best extension method System.Collections.Generic.IEnumerableTsource has some invalid arguments.
The code is as follows.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the n9o. of elements: ");
int n = Convert.ToInt32(Console.ReadLine());
List<int> unsorted = new List<int>();
Console.WriteLine("Enter the elements: ");
for (int i = 0; i < n; i++)
{
unsorted.Add(Convert.ToInt32(Console.ReadLine()));
}
List<int> sorted = quicksort(unsorted);
foreach (int entry in sorted)
{
Console.Write(entry + "\t");
}
return;
} //end of main.
public static List<int> quicksort(List<int> given)
{
if (given.Count == 1)
return given;
int mid = given.Count / 2;
List<int> less = new List<int>();
List<int> big = new List<int>();
for (int a = 0; a < given.Count; a++)
{
if (given[a] < mid)
{
less.Add(given[a]);
}
else
big.Add(given[a]);
}
return (quicksort(less).Concat(given[mid]).Concat(quicksort(big)));
}
}//end of class.
}//end of namespace.
You can't Concat an int into an IEnumerable<int>. You could instead wrap it in an array and Concat that to your other lists:
return quicksort(less)
.Concat(new[] { given[mid] })
.Concat(quicksort(big))
.ToList();
As the error is trying to tell you, the Concat() method takes a collection of items to concatenate, not a single int.
I think adding given[mid] to the resulting list is a mistake, since it will add the item to your resulting list twice...
also, you need to test against given[mid] not mid
So you should change your if statement to:
if (given[a] < given[mid])
less.Add(given[a]);
else if (given[a] > given[mid])
big.Add(given[a]);
This is assuming that all numbers are unique as well, because if given[mid] is not unique, then you have a problem

Categories

Resources