Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
uniqueness 2.8
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { newDivQ(); } public static void newDivQ() { Random rand = new Random(); List<string> divQ = new List<string>(); //List<int> divisor = new List<int>(); List<string> quotient = new List<string>(); int num = 90; int count =0; while (divQ.Count()<num) { int a = rand.Next(2,101); int b = rand.Next(2,11); int c = a/b; if (a%b==0 && a/b<=10) { if (divQ.Count()==0) { // 1st quotient string q = Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c); divQ.Add(q); string substring = divQ[divQ.Count-1].Substring(divQ[divQ.Count-1].IndexOf("=")+1); quotient.Add(substring); Console.WriteLine("divQ count{0}",divQ.Count); Console.WriteLine("quotient count{0}",quotient.Count+"\n"); Console.WriteLine("current quotient "+quotient[divQ.Count-1]); Console.WriteLine("substring "+substring); foreach (var obj in divQ) Console.WriteLine("\n"+obj); foreach (var obj in quotient) Console.WriteLine(obj); Console.WriteLine(); } else if (divQ.Count()==1 && !divQ.Contains(Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c))) { // 1st quotient string q = Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c); //divQ.Add(q); //string substring = divQ[divQ.Count-1].Substring(divQ[divQ.Count-1].IndexOf("=")+1); string substring = q.Substring(q.IndexOf("=")+1); if (!substring.Equals(quotient[divQ.Count-1])) { //if current quotient is unique to previous quotient divQ.Add(q); quotient.Add(substring); } Console.WriteLine("----\ndivQ count{0}",divQ.Count); Console.WriteLine("quotient count{0}",quotient.Count+"\n"); if(!substring.Equals(quotient[divQ.Count-1])) { Console.WriteLine("last quotient "+quotient[divQ.Count-2]); } Console.WriteLine("current quotient "+quotient[divQ.Count-1]); Console.WriteLine("substring "+substring); Console.WriteLine(); foreach (var obj in divQ) Console.WriteLine(obj); foreach (var obj in quotient) Console.WriteLine(obj); Console.WriteLine(); } else if (divQ.Count()==2 && !divQ.Contains(Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c))) { // 1st quotient string q = Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c); //divQ.Add(q); //string substring = divQ[divQ.Count-1].Substring(divQ[divQ.Count-1].IndexOf("=")+1); string substring = q.Substring(q.IndexOf("=")+1); List<string> subQuotient = quotient.GetRange(quotient.Count()-2,2); if (UniqueInList(substring,subQuotient)) { //if quotient is equal to one of previous 2 quotients (NOT unique) divQ.Add(q); quotient.Add(substring); } Console.WriteLine("----\ndivQ count{0}",divQ.Count); Console.WriteLine("quotient count{0}",quotient.Count+"\n"); if (divQ.Count>2) { Console.Write("(last 2 of quotients before adding the incoming one) "); foreach (var obj in subQuotient) Console.Write(obj+" "); } Console.WriteLine("\n2nd last quotient "+quotient[divQ.Count-3]); Console.WriteLine("last quotient "+quotient[divQ.Count-2]); Console.WriteLine("current quotient "+quotient[divQ.Count-1]); Console.WriteLine("incoming quotient "+substring); Console.WriteLine("unique? "+UniqueInList(substring,subQuotient)); Console.WriteLine(); foreach (var obj in divQ) Console.WriteLine(obj); foreach (var obj in quotient) Console.WriteLine(obj); Console.WriteLine(); } else if (divQ.Count()>=3 && !divQ.Contains(Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c))) { // 1st quotient string q = Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c); //divQ.Add(q); //string substring = divQ[divQ.Count-1].Substring(divQ[divQ.Count-1].IndexOf("=")+1); string substring = q.Substring(q.IndexOf("=")+1); List<string> subQuotient = quotient.GetRange(quotient.Count()-3,3); if (UniqueInList(substring,subQuotient)) { //if quotient is equal to one of previous 3 quotients (NOT unique) divQ.Add(q); quotient.Add(substring); } Console.WriteLine("----\ndivQ count{0}",divQ.Count); Console.WriteLine("quotient count{0}",quotient.Count+"\n"); if (divQ.Count>3) { Console.Write("(last 3 of quotients before adding the incoming one) "); foreach (var obj in subQuotient) Console.Write(obj+" "); Console.WriteLine("\n3rd last quotient "+quotient[divQ.Count-4]); } Console.WriteLine("2nd last quotient "+quotient[divQ.Count-3]); Console.WriteLine("last quotient "+quotient[divQ.Count-2]); Console.WriteLine("current quotient "+quotient[divQ.Count-1]); Console.WriteLine("incoming quotient "+substring); Console.WriteLine("unique? "+UniqueInList(substring,subQuotient)); Console.WriteLine(); foreach (var obj in divQ) Console.WriteLine(obj); foreach (var obj in quotient) Console.WriteLine(obj); Console.WriteLine(); } /*else if (divQ.Count()>=3 && !divQ.Contains(Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c))) { // 1st quotient string q = Convert.ToString(a)+"\\div"+Convert.ToString(b)+"="+Convert.ToString(c); //divQ.Add(q); //string substring = divQ[divQ.Count-1].Substring(divQ[divQ.Count-1].IndexOf("=")+1); string substring = q.Substring(q.IndexOf("=")+1); List<string> subQuotient = quotient.GetRange(quotient.Count()-3,3);// if (!UniqueInList(substring,subQuotient)) { //if quotient is equal to one of previous 3 quotients (NOT unique) divQ.Add(q); quotient.Add(substring); } Console.WriteLine("----\ndivQ count{0}",divQ.Count); Console.WriteLine("quotient count{0}",quotient.Count+"\n"); foreach (var obj in subQuotient) Console.Write(obj+" "); Console.WriteLine("\n3rd last quotient "+quotient[divQ.Count-4]); Console.WriteLine("2nd last quotient "+quotient[divQ.Count-3]); Console.WriteLine("last quotient "+quotient[divQ.Count-2]); Console.WriteLine("current quotient "+quotient[divQ.Count-1]); Console.WriteLine("substring "+substring); Console.WriteLine(); foreach (var obj in divQ) Console.WriteLine(obj); foreach (var obj in quotient) Console.WriteLine(obj); Console.WriteLine(); }*/ } } /* for (int i =0; i<100; i++) { list.Add(rand.Next(1,40)); }*/ } public static bool UniqueInList(string n, List<string> t) { int count = 0; for (int i=0; i<t.Count(); i++) { if (!n.Equals(t[i])) count++; } if (count.Equals(t.Count())) return true; else return false; } public static bool Unique(List<string> t){ int count = 0; for (int i=0; i<t.Count()-1; i++){ for (int j=i+1; j<t.Count(); j++) { //Console.WriteLine(i + " " + j); if (!t[i].Equals(t[j])){ count++; } } } if (count.Equals(nCr(t.Count(),2))) { return true; } else return false; } public static int nCr(int n, int r) { return (Factorial(n)/Factorial(r))/Factorial(n-r); } public static int Factorial(int n) { if (n>0) { for (int i=n-1; i>1; i--) { n = n*i; } return n; } else if (n<0) throw new ArgumentOutOfRangeException("n","n should be non-zero"); else return 1; } } }
run
|
edit
|
history
|
help
0
c++ MICHELL MARTINEZ
PolyMorphism
Event test
IEnumerable<T> generic interface implementation
SSTR PARS
bitcount algo
Update 2D Array
count occurances
c++online
Programmer DNA