-1

I'm coding a program about "Drunken Squares" where I generate a random list of directions like N S E or W and use code to test if those lists of directions correspond to a perfect square.

Each list of directions is 20 directions (characters) long and in the format of a character array.

One criteria which I am trying to implement is code which would detect how large of clusters of characters there are in the list, here is an example of what I mean:

Grade the list of directions in terms of how well it is clustered
Perfect Square would be (Perfect Input): N N N N N E E E E E S S S S S W W W W W
output: largest cluster = 5
output: smallest cluster = 5

Typical Input: W N N E E E W S N S S W W E S S W W W
output: Largest cluster would be 3
output: Smallest cluster would be 1

My specific question is how do you take input as a character array and return the largest and smallest clusters of characters within the array?

Current code:

for (int i = 0; i < 20; i++) {

      if (i+1 <= 20) {
        if (input2[i].ToString() == input2[i+1].ToString()) {
            //Make some value increase? 
          }
        }
      }
Noah
  • 430
  • 1
  • 4
  • 21
  • What's a cluster? Am I right in thinking that the only valid solution is the perfect input, or some "rotate by 5*N to the right" or the reverse of it? – Caius Jard Feb 23 '22 at 17:25
  • It sure seems like you are asking how to do the entire assignment, not one particular aspect of it. For instance, what is the definition of a "cluster"? Is it just a series of repeated characters? Then it would seem as though finding the largest cluster is the same as [Return the longest subarray of repeating members C#](https://stackoverflow.com/q/11984073/215552) – Heretic Monkey Feb 23 '22 at 17:28
  • See also [Counting the number of times a value appears in an array](https://stackoverflow.com/q/56555652/215552) – Heretic Monkey Feb 23 '22 at 17:29
  • Can a perfect square overwrite its edges and still be perfect? Can I go *N S N S N S N S N S N S* **E N W** *S N S N S* and be perfect? (The italic overwrites the left edge of the square many times, the bold bit draws the other sides) – Caius Jard Feb 23 '22 at 17:30
  • I picked duplicate based on "*a cluster* is a sequence of identical elements" understanding. If your criteria is different there is probably a better duplicate. Note that the code in the "groups of consecutive numbers" question can be easily adapted to any "this element belongs to the group with the previous element(s)" - you may want to [edit] the question to clarify how it did not help along with the explanation what "cluster" means. – Alexei Levenkov Feb 23 '22 at 17:41

1 Answers1

-2

Try code like this :

            string input = "N N N N N E E E E E S S S S S W W W W W";
            string[] directions = input.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
            int length = directions.Length;

            int minLong = 0;
            int maxLong = 0;
            int minLat = 0;
            int maxLat = 0;

            int y = 0;
            int x = 0;

            for (int i = 0; i < length; i++)
            {
                switch (input[i])
                {
                    case 'N':
                        y += 1;
                        if (y > maxLat) maxLat = y;
                        break;
                    case 'S':
                        y -= 1;
                        if (y < minLat) minLat = y;
                        break;
                    case 'E':
                        x += 1;
                        if (x > maxLong) maxLong = x;
                        break;
                    case 'W':
                        x -= 1;
                        if (x < minLong) minLong = x;
                        break;
                }
            }
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 1
    Distinct lack of learning opportunity all round, here – Caius Jard Feb 23 '22 at 17:29
  • 2
    This look like good example of https://meta.stackoverflow.com/questions/256359/flag-try-this-code-answers-as-very-low-quality - code shows an answer to some question (so not NAA) but quite unclear what question it is answering. So @CaiusJard I think it is good learning opportunity for both questioner and answerer. – Alexei Levenkov Feb 23 '22 at 17:34
  • @AlexeiLevenkov lol, yes - quite true that! – Caius Jard Feb 23 '22 at 17:40