Split Array into 2D based on 2 parameters C# - c#

I have a text file which I have split up into a string array based on new line.
string[] arr = s.Split('\n');
Now, I need to further categorize this into a 2-dimensional array wherein each column is a new "transaction".
So the text file basically contains info about bank transactions, an example being given below:
21......
22....
23.....
31....
32.....
31.....
32.....
21....
21.....
22....
The beginning of the numbers signify a new tx record which begins at a new line. I want to make it into a 2D array wherein each column is grouped as one tx beginning from 21 until it comes across the next 21 (so the record before it).
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].StartsWith("21"))
{
indices[i] = i;
}
}
I tried to write the code above to check for array element beginning with 21 and then storing the index but it ends up storing all the indices.
Any help will be appreciated!

What you'd need to do is
string[] arr = s.Split('\n');
List<List<string>> listOfLists = new List<List<string>>(); //dynamic multi-dimensional list
//list to hold the lines after the line with "21" and that line
List<string> newList = new List<string>();
listOfLists.Add(newList);
for(int i = 0; i < arr.Length; i++)
{
if(arr[i].StartsWith("21"))
{
if(newList.Count > 0)
{
newList = new List<string>(); //make a new list for a column
listOfLists.Add(newList); //add the list of lines (one column) to the main list
}
}
newList.Add(arr[i]); //add the line to a column
}

If I understand you right, you can try regular expressions (i.e. instead of splitting, extract transactions):
using System.Linq;
using System.Text.RegularExpressions;
...
string line = "21 A 22 B 23 C 31 D 32 E 31 F 32 G 21 H 21 I 22 J";
var result = Regex
.Matches(line, "21 .*?((?=21 )|$)")
.OfType<Match>()
.Select(match => match.Value)
.ToArray(); // <- let's materialize as na array
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
21 A 22 B 23 C 31 D 32 E 31 F 32 G
21 H
21 I 22 J

Related

Join keys in key-value pair list with close value c#

I have a list of key-value pairs of <string, int>. I want to merge and construct a new string with the keys that has close values (+3-3) and add each new string to a list.
Here are the keys and values of my list:
Luger: 9
Burger: 9
Le: 21
Pigeon: 21
Burger: 21
Hamburger: 25
Double: 30
Animal: 31
Style: 31
The: 43
Original: 43
Burger: 44
Here's the output that i want to achieve:
Luger Burger
Le Pigeon Burger
Hamburger
Double Animal Style
The Original Burger
To achieve this, firstly i created a list containing this key-value pairs. And iterate through each item and tried to find close values, assign them to new key-value pairs and delete that index. But that doesn't work properly. That's the code so far:
for (int i = 0; i < wordslist.Count; i++)
{
for (int j = 0; j < wordslist.Count; j++)
{
if (wordslist[i].Value <= wordslist[j].Value + 3 && wordslist[i].Value >= wordslist[j].Value - 3)
{
wordslist.Add(
new KeyValuePair<string, int>(wordslist[i].Key + " " + wordslist[j].Key, wordslist[i].Value)
);
wordslist.RemoveAt(j);
}
}
wordslist.RemoveAt(i);
}
this doesn't work and produce repetitive results as below:
Pigeon: 21
Style: 30
Burger: 30
Double Double Animal: 30
Burger Burger: 31
Original Original The The Original Burger Original Burger: 42
Is there any algorithm that can iterate through these items and construct a string by merging the keys that has close values and add each item to a list?
You can simplify this logic:
public IEnumerable<string> GetPlusOrMinus3(Dictionary<string, int> fullList, int checkNumber)
{
return fullList.Where(w => checkNumber <= w.Value + 3
&& checkNumber >= w.Value - 3)
.Select(s => $"{s.Key}: {s.Value}" );
}
The string format isn't perfect for you, but the logic should hold.
And in use you could do something like:
var forOne = GetPlusOrMinus3(values, 1);
var resultString = String.Join(", ", forOne);
Console.WriteLine(resultString);
Which would write out:
one: 1, two: 2, four: 4
And to loop through everything:
foreach(var entryValue in values.Values)
{
Console.WriteLine(String.Join(", ", GetPlusOrMinus3(values, entryValue)));
}
Or to loop through anything without resusing any results:
var matchedNumbers = new List<int>();
foreach(var entryValue in values.Values)
{
var matchResults = values.Where(w => entryValue <= w.Value + 3 && entryValue >= w.Value - 3
&& !matchedNumbers.Contains(w.Value)).ToDictionary(x => x.Key, x => x.Value);
if (matchResults.Any())
{
matchedNumbers.AddRange(matchResults.Select(s => s.Value).ToList());
Console.WriteLine(String.Join(", ",
GetPlusOrMinus3(matchResults, entryValue)));
}
}
Logs:
one: 1, two: 2, four: 4
twelve: 12, 10: 10, eleven: 11
six: 6

How to access the string array elements for search?

I have a text file that include of numbers and I save it in a string array.
one line of my text file is this:
2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21
every line save in one of indexes of string array.
now how can i access array elements as int type and search and calculate in all of array?
this is my array:
string [] lines = File.ReadAllLines(txtPath.Text);
for example I want to return indexes of array that include number'14' in all of array .
This is the easiest and clearest way to solve it. I commented so you can better understand what happens in the entire program.
class Program
{
static void Main(string[] args)
{
// this is your array of strings (lines)
string[] lines = new string[1] {
"2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21"
};
// this dictionary contains the line index and the list of indexes containing number 14
// in that line
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
// iterating over lines array
for (int i = 0; i < lines.Length; i++)
{
// creating the list of indexes and the dictionary key
List<int> indexes = new List<int>();
dict.Add(i, indexes);
// splitting the line by space to get numbers
string[] lineElements = lines[i].Split(' ');
// iterating over line elements
for (int j = 0; j < lineElements.Length; j++)
{
int integerNumber;
// checking if the string lineElements[j] is a number (because there also this case 114:275:5)
if (int.TryParse(lineElements[j], out integerNumber))
{
// if it is we check if the number is 14, in that case we add that index to the indexes list
if (integerNumber == 14)
{
indexes.Add(j);
}
}
}
}
// Printing out lines and indexes:
foreach (int key in dict.Keys)
{
Console.WriteLine(string.Format("LINE KEY: {0}", key));
foreach (int index in dict[key])
{
Console.WriteLine(string.Format("INDEX ELEMENT: {0}", index));
}
Console.WriteLine("------------------");
}
Console.ReadLine();
}
}
UPDATE 1:
As you requested:
special thanks for your clear answering.if i want to do search for all of my array elements what can i do? it means instead of only
number'14' i want to print indexes of all numbers that appear in
indexes
If you want to print all the indexes you should Console.WriteLine(j), that is the index of the inner for cycle, instead of checking the number value if (integerNumber == 14).
So, this is the program:
class Program
{
static void Main(string[] args)
{
// this is your array of strings (lines)
string[] lines = new string[1] {
"2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21"
};
// this dictionary contains the line index and the list of indexes containing number 14
// in that line
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
// iterating over lines array
for (int i = 0; i < lines.Length; i++)
{
// creating the list of indexes and the dictionary key
List<int> indexes = new List<int>();
dict.Add(i, indexes);
// splitting the line by space to get numbers
string[] lineElements = lines[i].Split(' ');
// iterating over line elements
for (int j = 0; j < lineElements.Length; j++)
{
// printing all indexes of the current line
Console.WriteLine(string.Format("Element index: {0}", j));
}
}
Console.ReadLine();
}
}
UPDATE 2:
As you requested:
if i want to search my line till first " : " apper and then search next line, what can i do?
You need to break the for cycle when you are on the element with :
class Program
{
static void Main(string[] args)
{
// this is your array of strings (lines)
string[] lines = new string[1] {
"2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114:275:5 8 1 14 10 6 10 18 12 25 7 40 1 30 18 8 2 1 5 21 10 2 21"
};
// this dictionary contains the line index and the list of indexes containing number 14
// in that line
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
// iterating over lines array
for (int i = 0; i < lines.Length; i++)
{
// creating the list of indexes and the dictionary key
List<int> indexes = new List<int>();
dict.Add(i, indexes);
// splitting the line by space to get numbers
string[] lineElements = lines[i].Split(' ');
// iterating over line elements
for (int j = 0; j < lineElements.Length; j++)
{
// I'm saving the content of lineElements[j] as a string
string element = lineElements[j];
// I'm checking if the element saved as string contains the string ":"
if (element.Contains(":"))
{
// If it does, I'm breaking the cycle, and I'll continue with the next line
break;
}
int integerNumber;
// checking if the string lineElements[j] is a number (because there also this case 114:275:5)
if (int.TryParse(lineElements[j], out integerNumber))
{
// if it is we check if the number is 14, in that case we add that index to the indexes list
if (integerNumber == 14)
{
indexes.Add(j);
}
}
}
}
// Printing out lines and indexes:
foreach (int key in dict.Keys)
{
Console.WriteLine(string.Format("LINE KEY: {0}", key));
foreach (int index in dict[key])
{
Console.WriteLine(string.Format("INDEX ELEMENT: {0}", index));
}
Console.WriteLine("------------------");
}
Console.ReadLine();
}
}
As you can see, if you run this piece of code and compare it with the first version, in output you'll get only the index of the first 14 occurrence, because the second one is after the string with :.
First you must get all conttent of file in the string array format:
public string[] readAllInFile(string filepath){
var lines = File.ReadAllLines(path);
var fileContent = string.Join(' ',lines);//join all lines of file content in one variable
return fileContent.Split(' ');//each word(in your case each number) in one index of array
}
and in usage time you can do like this:
var MyFileContent = readAllInFile(txtPath.Text);
int x= Convert.ToInt32(MyFileContent[2]);
IEnumerable<int> numbers = MyFileContent.Select(m=> int.Parse(m);)
var sumeOf = numbers.sum();
you can use linq to have more tools on collections.
var linesAsInts = lines.Select(x => x.Split(' ').Select(int.Parse));
var filteredLines = linesAsInts.Where(x => x.Contains(14));
// define value delimiters.
var splitChars = new char[] { ' ', ':' };
// read lines and parse into enumerable of enumerable of ints.
var lines = File.ReadAllLines(txtPath.Text)
.Select(x => x.Split(splitChars)
.Select(int.Parse));
// search in array.
var occurences = lines
.Select((line,lineIndex) => line
.Select((integer, integerIndex) => new { integer, integerIndex })
.Where(x => x.integer == 10)
.Select(x => x.integerIndex));
// calculate all of array.
var total = lines.Sum(line => line.Sum());

How to remove a duplicate number from an array?

Hi I'm working on this simple program that takes 5 numbers from user as long as the numbers are greater than 10 and less than 100. My goal is to remove duplicates numbers an ONLY show the NOT DUPLICATE numbers. Let's say if I enter 23 , 23, 40, 56 , 37 I should only output 40 , 56 , 37. Please help me on this. Thanks in advance. Here's my code:
static void Main(string[] args)
{
int[] arr = new int[5];
for (int i = 0; i < 5; i++)
{
Console.Write("\nPlease enter a number between 10 and 100: ");
int number = Convert.ToInt32(Console.ReadLine());
if (number > 10 && number <= 100)
{
arr[i] = number;
}
else {
i--;
}
}
int[] arr2 = arr.Distinct().ToArray();
Console.WriteLine("\n");
for (int i = 0; i < arr2.Length; i++)
{
Console.WriteLine("you entered {0}", arr2[i]);
}
Console.ReadLine();
}
One way is to group the elements based on input number and filter groups whose count is 1
int[] arr2 = arr.GroupBy(e=>e)
.Where(e=>e.Count() ==1)
.Select(e=>e.Key).ToArray();
Demo
I think you are looking for this:
int[] arr2 = arr.GroupBy(x => x)
.Where(dup=>dup.Count()==1)
.Select(res=>res.Key)
.ToArray();
Input Array : 23 , 23, 40, 56 , 37
Output Array : 40 , 56 , 37
How it Works:
arr.GroupBy(x => x) => give a collection of {System.Linq.GroupedEnumerable<int,int,int>} where x.Key gives you the unique elements.
.Where(dup=>dup.Count()==1)=> Extracts the the KeyValuePairs that contains Values count exactly equal to 1
.Select(res=>res.Key) => will collects the Keys from the above result
In your case, perhaps a combination of LINQ methods would be needed:
int[] arr2;
int[] nodupe = arr2.GroupBy(x => x).Where(y => y.Count() < 2).Select(z => z.Key).ToArray();

How to set space between TextBox values into array

How to set space between TextBox values into array?
TextBox values:
1 32 54 76 89 89 454 23
Expected output array:
value[1]=1;
value[2]=32;
value[3]=76
...
Try this:
var numbers = textBox1.Text.Split(' ');
List<int> lst = numbers.Select(item => int.Parse(item)).ToList();
It will be even better if you use method group like this:
List<int> lst = numbers.Select(int.Parse).ToList();
Then you can get it's values like this:
lst[0] --> 1
lst[1] --> 32
and ...
You first have to split the users input:
var values = myTextBox.Text.Split(' ');
Now you can easily set your array of integers:
var myArr = values.Select(x => Convert.ToInt32(x)).ToArray();
Alternativly you can also use Int.Parse instead of Convert.ToInt32.
String[] values = tv.Text.Split(' '); // the values as string
int[] arr = new int[values.Length]; // the values as integer
for (int i = 0; i < arr.Length; i++)
arr[i] = int.Parse(values[i]);
If I understand you correct you need to create int array from string you can do it like this:
string TextBoxText = "1 32 54 76 89 89 454 23";
var value = TextBoxText.Split(' ').Select(Int32.Parse).ToList();
Then you can use your array like this:
Console.WriteLine(value[1]);
Console.WriteLine(value[2]);
TextBoxText should be Text property of your TextBox component.

Check if string contains a successive pair of numbers

I have one list. I want check if list[i] contains string "6 1". But this code thinks 6 13 24 31 35 contains "6 1". Its false.
6 13 24 31 35
1 2 3 6 1
stringCheck = "6 1";
List<string> list = new List<string>();
list.Add("6 13 24 31 35");
list.Add("1 2 3 6 1");
for (int i=0; i<list.Count; i++)
{
if (list[i].Contains(stringCheck)
{
// its return me two contains, but in list i have one
}
}
But this code thinks 6 13 24 31 35 contains "6 1". Its false. […]
List<string> list = new List<string>();
list.Add("6 13 24 31 35");
list.Add("1 2 3 6 1");
No, it's true because you are dealing with sequences of characters, not sequences of numbers here, so your numbers get treated as characters.
If you really are working with numbers, why not reflect that in the choice of data type chosen for your list?:
// using System.Linq;
var xss = new int[][]
{
new int[] { 6, 13, 24, 31, 35 },
new int[] { 1, 2, 3, 6, 1 }
};
foreach (int[] xs in xss)
{
if (xs.Where((_, i) => i < xs.Length - 1 && xs[i] == 6 && xs[i + 1] == 1).Any())
{
// list contains a 6, followed by a 1
}
}
or if you prefer a more procedural approach:
foreach (int[] xs in xss)
{
int i = Array.IndexOf(xs, 6);
if (i >= 0)
{
int j = Array.IndexOf(xs, 1, i);
if (i + 1 == j)
{
// list contains a 6, followed by a 1
}
}
}
See also:
Finding a subsequence in longer sequence
Find sequence in IEnumerable<T> using Linq

Categories

Resources