list produces specific set of combinations - c#

I have a C# list:
A
B
A
C
D
A
How can I find all the combinations of letters in which the index of the following letter is always bigger. So in this case, combinations could be: A, AB, ABA, C, CD, ADA... BUT NOT DC, CB... since B index comes before C index for last example. Using indexes, 1, 12, 123, 146, 134 would be accepted, but something like 143 would not be accepted since 4 is greater than 3.

Simply generate all nonempty subsets of set {1,2,3,4,5,6}. For each such subset, just take its numbers (in increasing order) and convert them to the corresponding letters. This way you'll get all possible letter sequences. Then you'll have to remove duplicates, if necessary - for example A will be generated three times by sets {1}, {3} and {6}.

This code generates all the combination ( as the sequence of list ) :
static void Main(string[] args)
{
GetCombination(new List<char> { 'A','B','C' });
Console.ReadKey();
}
static void GetCombination(List<char> list)
{
for (int i = 1; i < Convert.ToInt32(Math.Pow(2, list.Count)); i++)
{
int temp = i;
string str = "";
int j = Convert.ToInt32( Math.Pow(2, list.Count - 1));
int index = 0;
while (j > 0)
{
if (temp - j >= 0)
{
str += list[index];
temp -= j;
}
j /= 2;
index++;
}
Console.WriteLine(str);
}
}
And the output is :
C
B
BC
A
AC
AB
ABC

Related

How do combine a dictionary with a function?

I want to set each letter of the alphabet to a numeric value: a = 1, b = 2, c = 3, etc.
I created a dictionary to set each letter to a value. I created a function that takes in a word and finds the total value of its letters. How do I access my created dictionary within my function?
Console.WriteLine("YOUR NAME VALUE IS: " + hashFunction("b"));//I want the output to be 2 (b = 2) in this case.
int hashFunction(string name)
{
int totalSum = 0;
Char [] characters = name.ToCharArray();
for (int letterIndex = 0; letterIndex < characters.Length; letterIndex++)
{
totalSum += Convert.ToInt32(characters[letterIndex]);
}
return totalSum;
}
Dictionary<char, int> charValues = new Dictionary<char, int>();
charValues.Add('a', 1);
charValues.Add('b', 2);
charValues.Add('c', 3);
charValues.Add('d', 4);
charValues.Add('e', 5);
//etc etc etc
}
Since letters a..z within ASCII table are consequent we can compute letter index as
letter - 'a' + 1
without elaborated mappig based on dictionary. Then we can use Linq to find the Sum:
using System.Linq;
...
// static : we don't want "this" within the method
static int hashFunction(string name) => name
.Where(c => c >= 'a' && c <= 'z')
.Sum(c => c - 'a' + 1);
Edit: if you insist on dictionary based solution, you can put it like this:
using System.Linq;
...
static IReadOnlyDictionary<char, int> charValues = Enumerable
.Range('a', 'z' - 'a' + 1)
.ToDictionary(letter => (char)letter, letter - 'a' + 1);
int hashFunction(string name)
{
int totalSum = 0;
foreach (char c in name)
if (charValues.TryGetValue(c, out int v))
totalSum += v;
return totalSum;
}
Why do you need a dictionary?
IEnumerable<int> MapCharsToInts( IEnumerable<char> chars )
{
foreach ( char ch in chars.Map( c => Char.ToLower(c) )
{
int v = 1 + ch - 'a' ;
yield v >= 1 && v <= 26 ? v : 0 ;
}
}
How do I access my created dictionary within my function?
by the name of the variable. You use the [ ] brakets like in an array, but you pass a key values as parameter into it. In your case one of the chars that you retrieve from your char array. You can actually use the string directly to access the chars, since it is a char array under the hood:
int hashFunction(string name)
{
int totalSum = 0;
for (int letterIndex = 0; letterIndex < name.Length; letterIndex++)
{
char theKey = name[letterIndex];
totalSum += charValues[theKey];
}
return totalSum;
}
the dictionary will then return the appropriate integer, or it will throw an exception if it does not find any match.
The most important part is that the dictionary has to be declared as field or property on class level! not a local variable in some other method.

Algorithm converting lotto ticket number to integer value and back again

I'm looking for the algorithm to convert a lotto ticket number to an integer value an back again.
Let's say the lotto number can be between 1 and 45 and a tickets contains 6 unique numbers. This means there are a maximum of 8145060 unique lotto tickets.
eg:
01-02-03-04-05-06 = 1
01-02-03-04-05-07 = 2
.
.
.
39-41-42-43-44-45 = 8145059
40-41-42-43-44-45 = 8145060
I'd like to have a function (C# preferable but any language will do) which converts between a lotto ticket and an integer and back again. At the moment I use the quick and dirty method of pre-calculating everything, which needs a lot of memory.
For enumerating integer combinations, you need to use the combinatorial number system. Here's a basic implementation in C#:
using System;
using System.Numerics;
using System.Collections.Generic;
public class CombinatorialNumberSystem
{
// Helper functions for calculating values of (n choose k).
// These are not optimally coded!
// ----------------------------------------------------------------------
protected static BigInteger factorial(int n) {
BigInteger f = 1;
while (n > 1) f *= n--;
return f;
}
protected static int binomial(int n, int k) {
if (k > n) return 0;
return (int)(factorial(n) / (factorial(k) * factorial(n-k)));
}
// In the combinatorial number system, a combination {c_1, c_2, ..., c_k}
// corresponds to the integer value obtained by adding (c_1 choose 1) +
// (c_2 choose 2) + ... + (c_k choose k)
// NOTE: combination values are assumed to start from zero, so
// a combination like {1, 2, 3, 4, 5} will give a non-zero result
// ----------------------------------------------------------------------
public static int combination_2_index(int[] combo) {
int ix = 0, i = 1;
Array.Sort(combo);
foreach (int c in combo) {
if (c > 0) ix += binomial(c, i);
i++;
}
return ix;
}
// The reverse of this process is a bit fiddly. See Wikipedia for an
// explanation: https://en.wikipedia.org/wiki/Combinatorial_number_system
// ----------------------------------------------------------------------
public static int[] index_2_combination(int ix, int k) {
List<int> combo_list = new List<int>();
while (k >= 1) {
int n = k - 1;
if (ix == 0) {
combo_list.Add(n);
k--;
continue;
}
int b = 0;
while (true) {
// (Using a linear search here, but a binary search with
// precomputed binomial values would be faster)
int b0 = b;
b = binomial(n, k);
if (b > ix || ix == 0) {
ix -= b0;
combo_list.Add(n-1);
break;
}
n++;
}
k--;
}
int[] combo = combo_list.ToArray();
Array.Sort(combo);
return combo;
}
}
The calculations are simpler if you work with combinations of integers that start from zero, so for example:
00-01-02-03-04-05 = 0
00-01-02-03-04-06 = 1
.
.
.
38-40-41-42-43-44 = 8145058
39-40-41-42-43-44 = 8145059
You can play around with this code at ideone if you like.
there seem to be actually 45^6 distinct numbers, a simple way is to treat the ticket number as a base-45 number and convert it to base 10:
static ulong toDec(string input){
ulong output = 0;
var lst = input.Split('-').ToList();
for (int ix =0; ix< lst.Count; ix++)
{
output = output + ( (ulong.Parse(lst[ix])-1) *(ulong) Math.Pow(45 , 5-ix));
}
return output;
}
examples:
01-01-01-01-01-01 => 0
01-01-01-01-01-02 => 1
01-01-01-01-02-01 => 45
45-45-45-45-45-45 => 8303765624

Why does my C# code does not work as intended (arrays transformation, newbie)

I'm trying to take array of numbers (0 or 1 only) and repeatedly transform it as follows:
first and last numbers are always 0;
all the other numbers are
derived from previous state of array: if array[n] and its two neighbours
(array[n-1], array[n] and array[n+1]) have the same value (three 0s or three 1s) then newarray[n] should be be 1, otherwise it should be 0 (that produces nice patterns).
For example, if the array size is 10 and it starts with all zeroes, then program should output this:
0000000000
0111111110
0011111100
0001111000
0100110010
0000000000
0111111110
...and so on
I wrote a code that should do this and it doesn't work as intended. It always does first transformation perfectly but then begins some wrong, asymmetric, crazy stuff:
0000000000
0111111110
0000000000
0101010100
0000000010
0101010000
What makes my code behave the way it does?
Here is the code:
namespace test
{
class Program
{
public static void Main(string[] args)
{
const int leng = 10; //length of array
int[] arrayone = new int[leng];
int[] arraytwo = new int[leng];
for (int i = 0; i<=leng-1; i++)
{
arrayone[i] = 0;
arraytwo[i] = 0;
} //making two arrays and filling them with zeroes
for (int i = 0; i<=leng-1; i++)
{
Console.Write(arrayone[i]);
}
Console.WriteLine(' '); //printing the first state of array
for (int st=1; st<=16; st++) //my main loop
{
arraytwo[0]=0;
arraytwo[leng - 1]=0; //making sure that first and last elements are zero. I'm not sure I need this since I fill both arrays with zeroes in the beginning. But it probably doesn't hurt?
for (int i = 1; i<=leng-2; i++) //calculating new states for elements from second to second-to-last
{
if (((arrayone[i-1]) + (arrayone[i]) + (arrayone[i+1]) == 0) | ((arrayone[i-1]) + (arrayone[i]) + (arrayone[i+1]) == 3) == true)
arraytwo[i] = 1;
else
arraytwo[i] = 0;
}
//by now arraytwo contains transformed version of arrayone
for (int i = 0; i<=leng-1; i++)
{
Console.Write(arraytwo[i]);
} //printing result
arrayone = arraytwo; //copying content of arraytwo to arrayone for the next round of transformation;
Console.WriteLine(' ');
}
Console.Write(" ");
Console.ReadKey(true);
}
}
}
Tweakable version: https://dotnetfiddle.net/8htp9N
As pointed out you're talking an object and working on it, at the end of that you're assigning the reference not the values. one way to combat that would be
for your line: arrayone = arraytwo;
change it to : arrayone = (int[])arraytwo.Clone();
this will copy the values - for integers this will be sufficient.
Please, notice how your current code is complex and thus diffcult to debug. Make it simpler, extract a method:
using System.Linq;
...
private static IEnumerable<int[]> Generate(int width) {
int[] item = new int[width];
while (true) {
yield return item.ToArray(); // .ToArray() - return a copy of the item
int[] next = new int[width];
for (int i = 1; i < item.Length - 1; ++i)
if (item[i - 1] == item[i] && item[i] == item[i + 1])
next[i] = 1;
item = next;
}
}
Then you can put
public static void Main(string[] args) {
var result = Generate(10) // each line of 10 items
.Take(7) // 7 lines
.Select(item => string.Concat(item));
Console.Write(string.Join(Environment.NewLine, result));
}
Outcome:
0000000000
0111111110
0011111100
0001111000
0100110010
0000000000
0111111110

Finding all permutations in a list of numbers

Currently I have an array of 13 values and I want to find all the permutations of it and exclude the ones that summations that exceed a given threshold. The problem is that there are so many values it never finishes. Is there a way to optimize what I have?
public class formPermut
{
public void swapTwoNumber(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
public void prnPermut(int[] list, int k, int m)
{
int i;
if (k == m)
{
for (i = 0; i <= m; i++)
Console.Write("{0}", list[i]);
Console.Write(" ");
}
else
for (i = k; i <= m; i++)
{
swapTwoNumber(ref list[k], ref list[i]);
prnPermut(list, k + 1, m);
swapTwoNumber(ref list[k], ref list[i]);
}
}
}
public static void RecExercise11()
{
int n, i;
formPermut test = new formPermut();
int[] arr1 = new int[13];
Console.WriteLine("\n\n Recursion : Generate all possible permutations of an array :");
Console.WriteLine("------------------------------------------------------------------");
Console.Write(" Input the number of elements to store in the array [maximum 13 digits ] :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write(" Input {0} number of elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write(" element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\n The Permutations with a combination of {0} digits are : \n", n);
test.prnPermut(arr1, 0, n - 1);
Console.Write("\n\n");
}
}
}
So the problem is that you need to exclude the permutations whose sum is greater than a threshold is a perfect candidate for backtracking (don't search this term yet), think about a way to speed this up (there's n! permutations so it won't finish in a while).
Imagine this like a tree were the leaves are the permutations themselves and answer:
Do you need to know the full permutation before being able to exclude it?
What calculations can you make in a non-leaf node?
I would suggest you look at using the Permutation library from here
https://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
and then write your filtering on top of that.

String Combinations With Character Replacement

I am trying to work through a scenario I haven't seen before and am struggling to come up with an algorithm to implement this properly. Part of my problem is a hazy recollection of the proper terminology. I believe what I am needing is a variation of the standard "combination" problem, but I could well be off there.
The Scenario
Given an example string "100" (let's call it x), produce all combinations of x that swap out one of those 0 (zero) characters for a o (lower-case o). So, for the simple example of "100", I would expect this output:
"100"
"10o"
"1o0"
"1oo"
This would need to support varying length strings with varying numbers of 0 characters, but assume there would never be more than 5 instances of 0.
I have this very simple algorithm that works for my sample of "100" but falls apart for anything longer/more complicated:
public IEnumerable<string> Combinations(string input)
{
char[] buffer = new char[input.Length];
for(int i = 0; i != buffer.Length; ++i)
{
buffer[i] = input[i];
}
//return the original input
yield return new string(buffer);
//look for 0's and replace them
for(int i = 0; i != buffer.Length; ++i)
{
if (input[i] == '0')
{
buffer[i] = 'o';
yield return new string(buffer);
buffer[i] = '0';
}
}
//handle the replace-all scenario
yield return input.Replace("0", "o");
}
I have a nagging feeling that recursion could be my friend here, but I am struggling to figure out how to incorporate the conditional logic I need here.
Your guess was correct; recursion is your friend for this challenge. Here is a simple solution:
public static IEnumerable<string> Combinations(string input)
{
int firstZero = input.IndexOf('0'); // Get index of first '0'
if (firstZero == -1) // Base case: no further combinations
return new string[] { input };
string prefix = input.Substring(0, firstZero); // Substring preceding '0'
string suffix = input.Substring(firstZero + 1); // Substring succeeding '0'
// e.g. Suppose input was "fr0d00"
// Prefix is "fr"; suffix is "d00"
// Recursion: Generate all combinations of suffix
// e.g. "d00", "d0o", "do0", "doo"
var recursiveCombinations = Combinations(suffix);
// Return sequence in which each string is a concatenation of the
// prefix, either '0' or 'o', and one of the recursively-found suffixes
return
from chr in "0o" // char sequence equivalent to: new [] { '0', 'o' }
from recSuffix in recursiveCombinations
select prefix + chr + recSuffix;
}
This works for me:
public IEnumerable<string> Combinations(string input)
{
var head = input[0] == '0' //Do I have a `0`?
? new [] { "0", "o" } //If so output both `"0"` & `"o"`
: new [] { input[0].ToString() }; //Otherwise output the current character
var tails = input.Length > 1 //Is there any more string?
? Combinations(input.Substring(1)) //Yes, recursively compute
: new[] { "" }; //Otherwise, output empty string
//Now, join it up and return
return
from h in head
from t in tails
select h + t;
}
You don't need recursion here, you can enumerate your patterns and treat them as binary numbers. For example, if you have three zeros in your string, you get:
0 000 ....0..0....0...
1 001 ....0..0....o...
2 010 ....0..o....0...
3 011 ....0..o....o...
4 100 ....o..0....0...
5 101 ....o..0....o...
6 110 ....o..o....0...
7 111 ....o..o....o...
You can implement that with bitwise operators or by treating the chars that you want to replace like an odometer.
Below is an implementation in C. I'm not familiar with C# and from the other answers I see that C# already has suitable standard classes to implement what you want. (Although I'm surprised that so many peolpe propose recursion here.)
So this is more an explanation or illustration of my comment to the question than an implementation advice for your problem.
int binrep(char str[])
{
int zero[40]; // indices of zeros
int nzero = 0; // number of zeros in string
int ncombo = 1; // number of result strings
int i, j;
for (i = 0; str[i]; i++) {
if (str[i] == '0') {
zero[nzero++] = i;
ncombo <<= 1;
}
}
for (i = 0; i < ncombo; i++) {
for (j = 0; j < nzero; j++) {
str[zero[j]] = ((i >> j) & 1) ? 'o' : '0';
}
printf("%s\n", str); // should yield here
}
return ncombo;
}
Here's a solution using recursion, and your buffer array:
private static void Main(string[] args)
{
var a = Combinations("100");
var b = Combinations("10000");
}
public static IEnumerable<string> Combinations(string input)
{
var combinations = new List<string>();
combinations.Add(input);
for (int i = 0; i < input.Length; i++)
{
char[] buffer= input.ToArray();
if (buffer[i] == '0')
{
buffer[i] = 'o';
combinations.Add(new string(buffer));
combinations = combinations.Concat(Combinations(new string(buffer))).ToList();
}
}
return combinations.Distinct();
}
The method adds the raw input as the first result. After that, we replace in a loop the 0s we see as a o and call our method back with that new input, which will cover the case of multiple 0s.
Finally, we end up with a couple duplicates, so we use Distinct.
I know that the earlier answers are better. But I don't want my code to go to waste. :)
My approach for this combinatorics problem would be to take advantage of how binary numbers work. My algorithm would be as follows:
List<string> ZeroCombiner(string str)
{
// Get number of zeros.
var n = str.Count(c => c == '0');
var limit = (int)Math.Pow(2, n);
// Create strings of '0' and 'o' based on binary numbers from 0 to 2^n.
var binaryStrings = new List<string>();
for (int i = 0; i < limit; ++i )
{
binaryStrings.Add(Binary(i, n + 1));
}
// Replace each zero with respect to each binary string.
var result = new List<string>();
foreach (var binaryString in binaryStrings)
{
var zeroCounter = 0;
var combinedString = string.Empty;
for (int i = 0; i < str.Length; ++i )
{
if (str[i] == '0')
{
combinedString += binaryString[zeroCounter];
++zeroCounter;
}
else
combinedString += str[i];
}
result.Add(combinedString);
}
return result;
}
string Binary(int i, int n)
{
string result = string.Empty;
while (n != 0)
{
result = result + (i % 2 == 0 ? '0' : 'o');
i = i / 2;
--n;
}
return result;
}

Categories