I am trying to write the code to count arrows in string. I successfully run it and it works.
Arrow is like this >>--> or <--<<
both has to take in account, also we have to take in account the shared part of arrows for example if i have <--<<--<<
will give a count of 2 arrows.
My code to solve problem is this :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringsArrows
{
class Program
{
static void Main(string[] args)
{
using (StreamReader reader = File.OpenText("C:\\Users\\Mohit\\Desktop\\PolmStudio Tasks\\StringsArrows\\StringsArrows\\File.txt"))
while (!reader.EndOfStream)
{
List<string> list = null;
string line = reader.ReadLine();
if (null != line)
{
list = new List<string>();
string[] digits = line.Split(new char[] {'\n' }, StringSplitOptions.RemoveEmptyEntries);
int counter = 0;
foreach (string word in digits)
{
for (int i = 0; i < word.Count();i++)
{
if (i + 4 < word.Length)
{
if (word[i] == '<')
{
if (word[i + 1] == '-')
{
if (word[i + 2] == '-')
{
if (word[i + 3] == '<')
{
if (word[i + 4] == '<')
{
counter++;
}
}
}
}
}
if (word[i] == '>')
{
if (word[i + 1] == '>')
{
if (word[i + 2] == '-')
{
if (word[i + 3] == '-')
{
if (word[i + 4] == '>')
{
counter++;
}
}
}
}
}
}
}
}
Console.WriteLine(" Num. Of arrows are :"+counter);
}
}
Console.ReadKey();
}
}
}
And input from file is this :
<--<<--<<
<<>>--><--<<--<<>>>--><
<-->>
It's output is this :
Num. Of arrows are :2
Num. Of arrows are :4
Num. Of arrows are :0
What i want is: Could some one please know if there is any way possible more optimized then this ?
I do personally like loop-based solutions quite a lot (although you should improve your algorithm to reduce the big indentations; via functions/recursivity). But in this specific case, there are much easier and efficient solutions; for example: relying on Split
, which allows to easily determine the number of times that certain substrings are repeated.
Sample code:
string input = "<<>>--><--<<--<<>>>--><";
string[] temp = input.Split(new string[] { ">>-->", "<--<<" }, StringSplitOptions.None);
int totArrows = temp.Length - 1;
temp = input.Split(new string[] { ">>-->>-->", "<--<<--<<" }, StringSplitOptions.None);
totArrows = totArrows + temp.Length - 1; //4
UPDATE
As proven via comments, the proposed approach might not deliver the right answer under specific conditions. Also as explained via comments, my intention wasn't ever building a ready-to-use code perfectly addressing all the OP's concerns; but showing a different way to face the problem and setting a solid enough starting point (virtually a final solution).
In any case, for the sake of completeness (to reward the interest via comments) and to show the multiple applications of the proposed Split
approach, below these lines I am including a different algorithm perfectly fulfilling (what seems to be) the OP's requirements.
string input2 = "<<>>--><--<<--<<>>>--><";
string[] temp2 = input2.Split(new string[] { "--" }, StringSplitOptions.None);
int totArrows2 = 0;
for(int i = 0; i < temp2.Length - 1; i++)
{
string prevBit = temp2[i].Trim();
string curBit = temp2[i + 1].Trim();
if (prevBit.Length > 0 && curBit.Length > 0)
{
if (prevBit.Substring(prevBit.Length - 1, 1) == "<")
{
if (curBit.Length >= 2 && curBit.Substring(0, 2) == "<<") totArrows2 = totArrows2 + 1;
}
else if (prevBit.Length >= 2 && prevBit.Substring(prevBit.Length - 2, 2) == ">>")
{
if (curBit.Substring(0, 1) == ">") totArrows2 = totArrows2 + 1;
}
}
}
//totArrows2 = 4
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments