Check for series of numbers in array

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 7
Reputation: Pachrant is an unknown quantity at this point 
Solved Threads: 0
Pachrant Pachrant is offline Offline
Newbie Poster

Check for series of numbers in array

 
0
  #1
May 30th, 2009
Hello everybody...
Can anyone help me with this?:
I want to check out an array, if there is any number which is 3, or more times in a row.

IE:
int[] tmp= new int[7]{0,2,1,1,1,4,5}; // yes 3 duplicities starts at index 2

int[] tmp= new int[7]{0,2,2,1,1,1,1}; // yes 4 duplicities starts at index 3

any ideas?

Thanks for any answer.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Check for series of numbers in array

 
0
  #2
May 30th, 2009
What have you tried so far?
What code do you already have and what are the problems you have with it?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Check for series of numbers in array

 
0
  #3
May 30th, 2009
Even if you have only a few lines of code, just post them here.
Don't be affraid if it is really bad code, we are here to help you. But please show some effort.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 7
Reputation: Pachrant is an unknown quantity at this point 
Solved Threads: 0
Pachrant Pachrant is offline Offline
Newbie Poster

Re: Check for series of numbers in array

 
0
  #4
May 30th, 2009
Originally Posted by ddanbe View Post
What have you tried so far?
What code do you already have and what are the problems you have with it?
So, I tried to iterate thru the array using for loop and at each iteration remember previous number and count how many numbers are same.

But I gave up before end becouse it not seemd to me as good method.

I hope there is more sophisticated method to do that. Maybe allready written function... Dont Know.

I was thinking about cast the array to string and use regex ... but Im not sure Im able to define a patern and use it.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 7
Reputation: Pachrant is an unknown quantity at this point 
Solved Threads: 0
Pachrant Pachrant is offline Offline
Newbie Poster

Re: Check for series of numbers in array

 
0
  #5
May 30th, 2009
To make it more clear. (or otherwise >) ) :
I have 2 dimensional array (12 rows and 12 cols) filled with numbers {1,2,3,4,5} and what I have to do is remove from it (make it 0) occurrences of 3 and more (max 12) same numbers in a row. I have to do that in horizontal and vertical way.
hope this helps undestand. Any sugestions?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Check for series of numbers in array

 
0
  #6
May 30th, 2009
Originally Posted by Pachrant
So, I tried to iterate thru the array using for loop and at each iteration remember previous number and count how many numbers are same.
Give it a try!!!
If it works you can always improve afterward if at all possible.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 7
Reputation: Pachrant is an unknown quantity at this point 
Solved Threads: 0
Pachrant Pachrant is offline Offline
Newbie Poster

Re: Check for series of numbers in array

 
0
  #7
May 30th, 2009
I will, dont worry, but I hoped that it will be a better (easier) solution, how to do that.
IE cast it to the some collection and use any method or as I mention before cast it to the strig and use regex or any function like contains()...
I´m realy disappointed about it.... Still hope that anyone direct me at the right way. Thx for any advice...
Last edited by Pachrant; May 30th, 2009 at 10:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Check for series of numbers in array

 
0
  #8
May 31st, 2009
A List or an Array has a FindAll method. This is what I should use as a start. Start coding!
Any problems with it, come back here.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 7
Reputation: Pachrant is an unknown quantity at this point 
Solved Threads: 0
Pachrant Pachrant is offline Offline
Newbie Poster

Re: Check for series of numbers in array

 
0
  #9
May 31st, 2009
Originally Posted by ddanbe View Post
A List or an Array has a FindAll method. This is what I should use as a start. Start coding!
Any problems with it, come back here.
Im using this:
Its not best solution, but it works.
Thank for your time.
  1. public Dictionary<int,int> FindSeries(string[] v_Patern , string[] v_Array)
  2. {
  3. Dictionary<int,int> result=new Dictionary<int,int>();
  4.  
  5. string s= String.Join("", v_Array,0,v_Array.Length);
  6.  
  7. string p= String.Join("", v_Patern,0,v_Patern.Length);
  8. string patern="";
  9. for (int l = 0; l < v_Array.Length; l++)
  10. patern += p;
  11. for (int i = 0; i < v_Array.Length; i += v_Patern.Length)
  12. {
  13. patern = patern.Substring(0, patern.Length - p.Length);
  14. if (patern.Length >= 3) // more than 3 occurrances of patern required
  15. {
  16. while (s.IndexOf(patern) != -1)
  17. {
  18. bool b = true;
  19. foreach (KeyValuePair<int, int> d in result)
  20. if ((s.IndexOf(patern) > d.Key && s.IndexOf(patern) < d.Key + d.Value))
  21. b = false;
  22. if (b)
  23. {
  24. result.Add(s.IndexOf(patern), patern.Length);
  25. break;
  26. }
  27. }
  28. }
  29. }
  30. return result; //return value key: index, value number of occurrances
  31. }
but if anyone find, or know a better solution Im alble to rewrite it
Last edited by Pachrant; May 31st, 2009 at 4:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Check for series of numbers in array

 
0
  #10
Jun 1st, 2009
This is my own humble contribution to your problem. If it is better or worser I don't know, but it might give you some ideas.
  1. namespace ConsoleApplication1
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. //this only works with numbers in succession
  8. int[] range = new int[7] { 0, 2, 1, 1, 1, 4, 5 };
  9. //int[] range = new int[7] { 0, 2, 2, 1, 1, 1, 1 };
  10.  
  11. List<int> tmp = new List<int>();
  12. tmp.AddRange( range );
  13.  
  14. foreach (int i in tmp)
  15. {
  16. //can be optimised: if 3 found, jump 3 further in list
  17. Console.WriteLine(i);
  18. int first = tmp.IndexOf(i);
  19. int last = tmp.LastIndexOf(i);
  20. int t = last - first + 1;
  21. if ( t >= 3 )
  22. Console.WriteLine("--> {0} appears {1} times in the list.", i, t);
  23. }
  24. Console.ReadKey();
  25. }
  26. }
  27. }

Another possibility might be you construct a binary tree.
Loop through your input.
If the number is not in the tree insert it.
If the number is already in the tree augment a count variable you keep with the number.
Afterwards, traverse the tree and note which numbers have a count >=3.

I'm not so good at that but look here for info : http://msdn.microsoft.com/en-us/library/ms379572.aspx

Succes!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC