Philippe Renaud
asked on
String manipulation
Hi EE,
I have to create a list(Of String) with a loop
what I want is to start with : aaaa
then :
aaab
aaac
aaad
all up to aaaz
after I would like aaa0 up to aaa9
then aaba all up to aaza then numbers...
I think you know waht all mean, in other words all possibility Characters (4 of length) numeric & alpha
how can i do that ?
I have to create a list(Of String) with a loop
what I want is to start with : aaaa
then :
aaab
aaac
aaad
all up to aaaz
after I would like aaa0 up to aaa9
then aaba all up to aaza then numbers...
I think you know waht all mean, in other words all possibility Characters (4 of length) numeric & alpha
how can i do that ?
Just in case, I'd like to clarify the task.
First, I believe Kyle Abrahams is right and this is academic exercise. So I don't want to provide any code.
About the task. As a result of the algorithm you have described you will get a list of (26 + 10)*4 strings. There won't be strings in the list like: aazz or zzza ot zzzz? Am I correct? Or do you need a big list of all possible combinations (quite a big number :))?
First, I believe Kyle Abrahams is right and this is academic exercise. So I don't want to provide any code.
About the task. As a result of the algorithm you have described you will get a list of (26 + 10)*4 strings. There won't be strings in the list like: aazz or zzza ot zzzz? Am I correct? Or do you need a big list of all possible combinations (quite a big number :))?
(quite a big number
actually it is 1679616 4-char-words what is about 6.7 MB without spaces or 8.4 MB with one space after each word.
you could make a class AlphaNum4 which has a 4-byte array as member and a static byte array of length 36 which contains your 'alphabet'
{ 'a', 'b', ..., 'z', '0', .... '9' }
then a member function which increments the 'this' like kyle has described and returns the incremented 'number'.
then an output function which returns the AlphaNum4 as string.
with that you could have a loop from 0 to 1679616 (excluded) (or 36*36*36*36) where you increment from 'aaaa' to '9999' and write each combination to a file.
Sara
ASKER
I knew the "hacking" comment would come up it's always the same thing on expert exchange
It's not related to this at all, neither Academic, it's for work and we need to anotate a document and every new document they want to increase a code (they have their meaning) so it would start at aaaa and like I said increase
I can ask maybe to do this only at 3 combinaison and not 4 ... that I can do
but I need help with the code thats why I came here....
anyways.
It's not related to this at all, neither Academic, it's for work and we need to anotate a document and every new document they want to increase a code (they have their meaning) so it would start at aaaa and like I said increase
I can ask maybe to do this only at 3 combinaison and not 4 ... that I can do
but I need help with the code thats why I came here....
anyways.
ASKER
Also I am a member since 2005 go in my profile
I dont think I would go to school from 2005 to 2016
neither at my age and neither at the income I do, I dont need school anymore
It's a bit annoying to always defend this fact every time we ask a question (I dont want to offend anybody) I say this with respect.
I dont think I would go to school from 2005 to 2016
neither at my age and neither at the income I do, I dont need school anymore
It's a bit annoying to always defend this fact every time we ask a question (I dont want to offend anybody) I say this with respect.
Hi PhilippeRenaud;
See if this code meets your needs.
See if this code meets your needs.
// Starting value of the list of strings to be generated
string startSeq = "aaaa";
// Used to build each element of the list
StringBuilder sb = new StringBuilder();
// The values to be replaced in the startSeq
string replaceWith = "bcdefghijklmnopqrstuvwxyz0123456789";
// The resulting list of values
List<string> theSeq = new List<string>();
// Add the first element to the list
theSeq.Add(startSeq);
// Loop through the four elements in the sequence to be replaced
for (int i = 3; i > -1; i--)
{
// Start the next sequence
sb.Clear().Append(startSeq);
// Go through the characters that will replace the character in startSeq
foreach (char nextChar in replaceWith)
{
// Remove the original character and replace it
sb.Remove(i, 1).Insert(i, nextChar.ToString());
// Add it to the resulting sequence
theSeq.Add(sb.ToString());
}
}
// The list is now in theSeq
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Using a Code Project by Adrian Akison, I produced this solution:
Program.cs -
SupportingObjects.cs -
Sample output -
The original project is here: http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
-saige-
Program.cs -
using System;
using System.Collections.Generic;
using System.Linq;
namespace EE_Q28983886
{
class Program
{
static void Main(string[] args)
{
var set = new List<char>((from i in Enumerable.Range(0, 26) select (char)(i + 97)).Concat(from i in Enumerable.Range(0, 10) select (char)(i + 48)));
var variations = new Variations<char>(set, 4, GenerateOption.WithRepetition).Select(x => string.Join("", x)).ToList();
foreach (var variant in variations)
Console.WriteLine(String.Join("", variant.ToArray()));
Console.ReadLine();
}
}
}
SupportingObjects.cs -
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
// Using Combinations, Permutations and Variations implementation provided by Adrian Akison
// http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
namespace EE_Q28983886
{
public enum GenerateOption
{
WithoutRepetition,
WithRepetition
}
interface IMetaCollection<T> : IEnumerable<IList<T>>
{
long Count { get; }
GenerateOption Type { get; }
int UpperIndex { get; }
int LowerIndex { get; }
}
class SmallPrimeUtility
{
private SmallPrimeUtility() { ;}
public static List<int> Factor(int i)
{
int primeIndex = 0;
int prime = PrimeTable[primeIndex];
List<int> factors = new List<int>();
while (i > 1)
{
if (i % prime == 0)
{
factors.Add(prime);
i /= prime;
}
else
{
++primeIndex;
prime = PrimeTable[primeIndex];
}
}
return factors;
}
public static List<int> MultiplyPrimeFactors(IList<int> lhs, IList<int> rhs)
{
List<int> product = new List<int>(lhs.Concat(rhs));
product.Sort();
return product;
}
public static List<int> DividePrimeFactors(IList<int> numerator, IList<int> denominator)
{
return numerator.Except(denominator).ToList();
}
public static long EvaluatePrimeFactors(IList<int> value)
{
long accumulator = 1;
foreach (int prime in value)
accumulator *= prime;
return accumulator;
}
static SmallPrimeUtility()
{
CalculatePrimes();
}
private static void CalculatePrimes()
{
BitArray sieve = new BitArray(65536, true);
for (int possiblePrime = 2; possiblePrime <= 256; ++possiblePrime)
{
if (sieve[possiblePrime] == true)
{
for (int nonPrime = 2 * possiblePrime; nonPrime < 65536; nonPrime += possiblePrime)
sieve[nonPrime] = false;
}
}
// Scan sieve for primes...
if (PrimeTable == default(IList<int>))
PrimeTable = new List<int>();
for (int i = 2; i < 65536; ++i)
{
if (sieve[i])
PrimeTable.Add(i);
}
}
public static IList<int> PrimeTable { get; private set; }
}
class Combinations<T> : IMetaCollection<T>
{
#region Constructors
protected Combinations() { ;}
public Combinations(IList<T> set, int lowerIndex)
{
Initialize(set, lowerIndex, GenerateOption.WithoutRepetition);
}
public Combinations(IList<T> set, int lowerIndex, GenerateOption type)
{
Initialize(set, lowerIndex, type);
}
#endregion
#region IEnumerable Interface
public IEnumerator<IList<T>> GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(this);
}
#endregion
#region Enumerator Inner Class
public class Enumerator : IEnumerator<IList<T>>
{
#region Constructors
public Enumerator(Combinations<T> source)
{
parent = source;
enumerator = (Permutations<bool>.Enumerator)parent.permutations.GetEnumerator();
}
#endregion
#region IEnumerator interface
public void Reset()
{
enumerator.Reset();
}
public bool MoveNext()
{
bool ret = enumerator.MoveNext();
values = null;
return ret;
}
public IList<T> Current
{
get
{
ComputeCurrent();
return values;
}
}
object IEnumerator.Current
{
get
{
ComputeCurrent();
return values;
}
}
public void Dispose() { ;}
#endregion
#region Heavy Lifting Members
private void ComputeCurrent()
{
if (values == null)
{
values = new List<T>();
int index = 0;
IList<bool> currentPermutation = (IList<bool>)enumerator.Current;
for (int i = 0; i < currentPermutation.Count; ++i)
{
if (currentPermutation[i] == false)
{
values.Add(parent.values[index]);
if (parent.Type == GenerateOption.WithoutRepetition)
++index;
}
else
++index;
}
}
}
#endregion
#region Data
private Combinations<T> parent;
private List<T> values;
private Permutations<bool>.Enumerator enumerator;
#endregion
}
#endregion
#region IMetaList Interface
public long Count { get { return permutations.Count; } }
public GenerateOption Type { get; private set; }
public int UpperIndex { get { return values.Count; } }
public int LowerIndex { get; private set; }
#endregion
#region Heavy Lifting Members
private void Initialize(IList<T> set, int lowerIndex, GenerateOption type)
{
Type = type;
LowerIndex = lowerIndex;
values = new List<T>();
values.AddRange(set);
List<bool> myMap = new List<bool>();
if (type == GenerateOption.WithoutRepetition)
{
for (int i = 0; i < values.Count; ++i)
myMap.Add(i >= values.Count - LowerIndex ? false : true);
}
else
{
for (int i = 0; i < set.Count - 1; ++i)
myMap.Add(true);
for (int i = 0; i < LowerIndex; ++i)
myMap.Add(false);
}
permutations = new Permutations<bool>(myMap);
}
#endregion
#region Data
private List<T> values;
private Permutations<bool> permutations;
#endregion
}
class Permutations<T> : IMetaCollection<T>
{
#region Constructors
protected Permutations() { ;}
public Permutations(IList<T> set)
{
Initialize(set, GenerateOption.WithoutRepetition, null);
}
public Permutations(IList<T> set, GenerateOption type)
{
Initialize(set, type, null);
}
public Permutations(IList<T> set, IComparer<T> comparer)
{
Initialize(set, GenerateOption.WithoutRepetition, comparer);
}
#endregion
#region IEnumerable Interface
public virtual IEnumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<IList<T>> IEnumerable<IList<T>>.GetEnumerator()
{
return new Enumerator(this);
}
#endregion
#region Enumerator Inner-Class
public class Enumerator : IEnumerator<IList<T>>
{
#region Constructors
public Enumerator(Permutations<T> source)
{
parent = source;
orders = new int[source.orders.Length];
source.orders.CopyTo(orders, 0);
Reset();
}
#endregion
#region IEnumerator Interface
public void Reset()
{
position = Position.BeforeFirst;
}
public bool MoveNext()
{
if (position == Position.BeforeFirst)
{
values = new List<T>(parent.values.Count);
values.AddRange(parent.values);
Array.Sort(orders);
position = Position.InSet;
}
else if (position == Position.InSet)
{
if (values.Count < 2)
position = Position.AfterLast;
else if (NextPermutation() == false)
position = Position.AfterLast;
}
return position != Position.AfterLast;
}
public object Current
{
get
{
if (position == Position.InSet)
return new List<T>(values);
else
throw new InvalidOperationException();
}
}
IList<T> IEnumerator<IList<T>>.Current
{
get
{
if (position == Position.InSet)
return new List<T>(values);
else
throw new InvalidOperationException();
}
}
public virtual void Dispose() { ;}
#endregion
#region Heavy Lifting Methods
private bool NextPermutation()
{
int i = orders.Length - 1;
while (orders[i - 1] >= orders[i])
{
--i;
if (i == 0)
return false;
}
int j = orders.Length;
while (orders[j - 1] <= orders[i - 1])
--j;
Swap(i - 1, j - 1);
++i;
j = orders.Length;
while (i < j)
{
Swap(i - 1, j - 1);
++i;
--j;
}
return true;
}
private void Swap(int i, int j)
{
current = values[i];
values[i] = values[j];
values[j] = current;
kvi = orders[i];
orders[i] = orders[j];
orders[j] = kvi;
}
#endregion
#region Data and Internal Members
private T current;
private int kvi;
private Position position = Position.BeforeFirst;
private int[] orders;
private List<T> values;
private Permutations<T> parent;
private enum Position
{
BeforeFirst,
InSet,
AfterLast
}
#endregion
}
#endregion
#region IMetaList Interface
public long Count { get; private set; }
public GenerateOption Type { get; private set; }
public int UpperIndex { get { return values.Count; } }
public int LowerIndex { get { return values.Count; } }
#endregion
#region Heavy Lifting Members
private void Initialize(IList<T> set, GenerateOption type, IComparer<T> comparer)
{
Type = type;
values = new List<T>(set.Count);
values.AddRange(set);
orders = new int[set.Count];
if (type == GenerateOption.WithRepetition)
{
for (int i = 0; i < orders.Length; ++i)
orders[i] = i;
}
else
{
if (comparer == null)
comparer = new SelfComparer<T>();
values.Sort(comparer);
int j = 1;
if (orders.Length > 0)
orders[0] = j;
for (int i = 1; i < orders.Length; ++i)
{
if (comparer.Compare(set[i - 1], set[i]) != 0)
++j;
orders[i] = j;
}
}
Count = GetCount();
}
private long GetCount()
{
int runCount = 1;
List<int> divisors = new List<int>();
List<int> numerators = new List<int>();
for (int i = 1; i < orders.Length; ++i)
{
numerators.AddRange(SmallPrimeUtility.Factor(i + 1));
if (orders[i] == orders[i - 1])
++runCount;
else
{
for (int f = 2; f <= runCount; ++f)
divisors.AddRange(SmallPrimeUtility.Factor(f));
runCount = 1;
}
}
for (int f = 2; f <= runCount; ++f)
divisors.AddRange(SmallPrimeUtility.Factor(f));
return SmallPrimeUtility.EvaluatePrimeFactors(SmallPrimeUtility.DividePrimeFactors(numerators, divisors));
}
#endregion
#region Data and Internal Members
private List<T> values;
private int[] orders;
private class SelfComparer<U> : IComparer<U>
{
public int Compare(U x, U y)
{
return ((IComparable<U>)x).CompareTo(y);
}
}
#endregion
}
class Variations<T> : IMetaCollection<T>
{
#region Constructors
protected Variations() { ;}
public Variations(IList<T> set, int lowerIndex)
{
Initialize(set, lowerIndex, GenerateOption.WithoutRepetition);
}
public Variations(IList<T> set, int lowerIndex, GenerateOption type)
{
Initialize(set, lowerIndex, type);
}
#endregion
#region IEnumerable Interface
public IEnumerator<IList<T>> GetEnumerator()
{
if (Type == GenerateOption.WithRepetition)
return new EnumeratorWithRepetition(this);
else
return new EnumeratorWithoutRepetition(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
if (Type == GenerateOption.WithRepetition)
return new EnumeratorWithRepetition(this);
else
return new EnumeratorWithoutRepetition(this);
}
#endregion
#region Enumerator Inner Class
public class EnumeratorWithRepetition : IEnumerator<IList<T>>
{
#region Constructors
public EnumeratorWithRepetition(Variations<T> source)
{
parent = source;
Reset();
}
#endregion
#region IEnumerator interface
public void Reset()
{
values = null;
indices = null;
}
public bool MoveNext()
{
int carry = 1;
if (indices == null)
{
indices = new List<int>();
for (int i = 0; i < parent.LowerIndex; ++i)
indices.Add(0);
carry = 0;
}
else
{
for (int i = indices.Count - 1; i >= 0 && carry > 0; --i)
{
indices[i] += carry;
carry = 0;
if (indices[i] >= parent.UpperIndex)
{
indices[i] = 0;
carry = 1;
}
}
}
values = null;
return carry != 1;
}
public IList<T> Current
{
get
{
ComputeCurrent();
return values;
}
}
object IEnumerator.Current
{
get
{
ComputeCurrent();
return values;
}
}
public void Dispose() { ;}
#endregion
#region Heavy Lifting Members
private void ComputeCurrent()
{
if (values == null)
{
values = new List<T>();
foreach (int index in indices)
values.Add(parent.values[index]);
}
}
#endregion
#region Data
private Variations<T> parent;
private List<T> values;
private List<int> indices;
#endregion
}
public class EnumeratorWithoutRepetition : IEnumerator<IList<T>>
{
#region Constructors
public EnumeratorWithoutRepetition(Variations<T> source)
{
parent = source;
enumerator = (Permutations<int>.Enumerator)parent.permutations.GetEnumerator();
}
#endregion
#region IEnumerator interface
public void Reset()
{
enumerator.Reset();
}
public bool MoveNext()
{
bool ret = enumerator.MoveNext();
values = null;
return ret;
}
public IList<T> Current
{
get
{
ComputeCurrent();
return values;
}
}
object IEnumerator.Current
{
get
{
ComputeCurrent();
return values;
}
}
public void Dispose() { ;}
#endregion
#region Heavy Lifting Members
private void ComputeCurrent()
{
if (values == null)
{
values = new List<T>();
int index = 0;
IList<int> currentPermutation = (IList<int>)enumerator.Current;
for (int i = 0; i < parent.LowerIndex; ++i)
values.Add(parent.values[0]);
for (int i = 0; i < currentPermutation.Count; ++i)
{
int position = currentPermutation[i];
if (position != Int32.MaxValue)
{
values[position] = parent.values[index];
if (parent.Type == GenerateOption.WithoutRepetition)
++index;
}
else
++index;
}
}
}
#endregion
#region Data
private Variations<T> parent;
private List<T> values;
private Permutations<int>.Enumerator enumerator;
#endregion
}
#endregion
#region IMetaList Interface
public long Count
{
get
{
if (Type == GenerateOption.WithoutRepetition)
return permutations.Count;
else
return (long)Math.Pow(UpperIndex, LowerIndex);
}
}
public GenerateOption Type { get; private set; }
public int UpperIndex { get { return values.Count; } }
public int LowerIndex { get; private set; }
#endregion
#region Heavy Lifting Members
private void Initialize(IList<T> set, int lowerIndex, GenerateOption type)
{
Type = type;
LowerIndex = lowerIndex;
values = new List<T>();
values.AddRange(set);
if (type == GenerateOption.WithoutRepetition)
{
List<int> myMap = new List<int>();
int index = 0;
for (int i = 0; i < values.Count; ++i)
{
if (i >= values.Count - LowerIndex)
myMap.Add(index++);
else
myMap.Add(Int32.MaxValue);
}
permutations = new Permutations<int>(myMap);
}
else { /* List is not used */;}
}
#endregion
#region Data
private List<T> values;
private Permutations<int> permutations;
#endregion
}
}
Which produces all of the variations of aaaa through 9999.Sample output -
The original project is here: http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
-saige-
ASKER
Fernando, its almost it but you miss tons of combinaitions...
Saige code seems to be perfect, but, im just wondering if the number of code lines or a bit overkill to do what Iwanted....
can we just enhance Fernando script or I need 1 milions lines to do this? lol
Saige code seems to be perfect, but, im just wondering if the number of code lines or a bit overkill to do what Iwanted....
can we just enhance Fernando script or I need 1 milions lines to do this? lol
Did you check out my solution?
https://www.experts-exchange.com/questions/28983886/String-manipulation.html?anchorAnswerId=41893285#a41893285
https://www.experts-exchange.com/questions/28983886/String-manipulation.html?anchorAnswerId=41893285#a41893285
ASKER
im looking now ;)
ASKER
could we just Translate in Vb.net ?? or C# ?? :/
That is C# . . . it's just a console application.
ASKER
oups you're right.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
just like a number system you have a starting point and an ending point.
eg
00
01
...
09 <- end point
10
So start at the right most char, increment by one. When you hit the endpoint, increment 1 to the left, and then reset the current slot.
eg
099 + 1
090 <-- add 1 to middle column, but need to reset again.
100
I'm assuming this is academic which is why I posted at all, but know there's a hacking technique called brute forcing which is essentially what you're programming here.