Welcome Guest
Log In | Register )
You last visited May 27, 2017, 3:54 am
All times shown are
Eastern Time (GMT-5:00)

Need Help with - C Code to print Combinations in PICK 3 Game

Topic closed. 2 replies. Last post 1 year ago by thamizhpayan.

Page 1 of 1
PrintE-mailLink
Avatar
Thread Starter
Honolulu
United States
Member #173253
February 23, 2016
61 Posts
Offline
Posted: April 24, 2016, 8:54 pm - IP Logged

Below is the C Code to print the Combinations of numbers from an Array for a PICK 3 Game

The Code only does Singles ...

Can some kind soul please modify the code so that it also outputs Doubles ???

Thank you and regards

 

Party

 

// Program to print all combination of size r in an array of size n
#include <stdio.h>

void printCombination(int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, data, 0, n-1, 0, r);
}

void combinationUtil(int arr[], int data[], int start, int end, int index, int r)
{
if (index == r)
{
for (int j=0; j<r; j++)
printf("%d ", data[j]);
printf("\n");
return;
}

for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}

int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}

Output:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

    thamizhpayan's avatar - Lottery-052.jpg

    United States
    Member #173017
    February 13, 2016
    677 Posts
    Online
    Posted: April 25, 2016, 1:01 pm - IP Logged

    Hi,

     

    Instead of recursion,  you can try iterating the array thrice  and filter out the ones you don't need by just checking whether 2 digits are repeating - double and 3 are same -triple). Also you can tweak it further to get boxes and straights based on sorting 3 columns. Then you can print out the values.

     

    I suggest this because it's just pick-3 and pick-4 where the loop goes to a max of 1000 or 10000 which is quick.

     

    If it is like PB/MM, the code has to be tweaked for performance(threaded and so on).

     

    I am more into Java coding nowadays. You can really use some utilities like guava and apache commons code and just generate data with minimal lines of code. Of course language is just what we are comfortable with.

     

    I can send you code when I am at home.

      thamizhpayan's avatar - Lottery-052.jpg

      United States
      Member #173017
      February 13, 2016
      677 Posts
      Online
      Posted: April 25, 2016, 3:21 pm - IP Logged

      Hi,

       

      This might be overkill for what you asked for.

      You can take this code and use the logic and port to C in-case you want in C. Or you can switch to Java.

       

      Save as file PrintPick3Combos.java

      Compile and Run the Java class.

       

       

       

      package org.home.lottery.pick3;

      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.TreeSet;
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;

      public class PrintPick3Combos {
      private static final int MAX_NUMBER_IN_DIGIT = 9;
      ArrayList<String> firstPos = new ArrayList<String>();
      ArrayList<String> secondPos = new ArrayList<String>();
      ArrayList<String> thirdPos = new ArrayList<String>();

      private static boolean includeTriples = true;
      private static boolean includeDoubles = true;
      private static boolean includeSingles = true;

      public PrintPick3Combos() {
      firstPos.addAll(Arrays.asList(new String[] { "1", "2", "3", "4", "5" }));
      secondPos.addAll(Arrays.asList(new String[] { "1", "2", "3", "4", "5" }));
      thirdPos.addAll(Arrays.asList(new String[] { "1", "2", "3", "4", "5" }));
      }

      public static void main(String args[]) throws Exception {

      PrintPick3Combos printPick3Combos = new PrintPick3Combos();
      /*
      * ArrayList<String> allPossiblePick3Straights =
      * printPick3Combos.getAllPossiblePick3Straights(); for (String
      * allPossiblePick3Straight : allPossiblePick3Straights) {
      * System.out.println(allPossiblePick3Straight); }
      */
      System.out.println("-- all - straight --");
      TreeSet<String> allCombinations = printPick3Combos.getAllCombinations();
      for (String allCombination : allCombinations) {
      System.out.println(allCombination);
      }

      System.out.println("-- Sorted - all - box --");
      TreeSet<String> allBoxCombinations = printPick3Combos.getAllBoxes(allCombinations);
      for (String allBoxCombination : allBoxCombinations) {
      System.out.println(allBoxCombination);
      }
      }

      private TreeSet<String> getAllBoxes(TreeSet<String> allCombinations) {
      TreeSet<String> boxes = new TreeSet<String>();
      for (String allCombination : allCombinations) {
      Integer[] sortFirstThreeNumber = sortFirstThreeNumber(getStringToArray(allCombination));
      String box = sortFirstThreeNumber[0].toString() + sortFirstThreeNumber[1].toString()
      + sortFirstThreeNumber[2].toString();
      boxes.add(box);
      }
      return boxes;
      }

      private TreeSet<String> getAllCombinations() {
      TreeSet<String> straights = new TreeSet<String>();
      for (String firstDigit : firstPos) {
      for (String secondDigit : secondPos) {
      for (String thirdDigit : thirdPos) {

      Integer firstDIgitInt = new Integer(firstDigit);
      Integer secondDigitInt = new Integer(secondDigit);
      Integer thirdDigitInt = new Integer(thirdDigit);
      String straight = firstDIgitInt.toString() + secondDigitInt.toString() + thirdDigitInt.toString();

      boolean isSingle = isSingle(firstDIgitInt, secondDigitInt, thirdDigitInt);
      boolean isDouble = isDouble(firstDIgitInt, secondDigitInt, thirdDigitInt);
      boolean isTriple = isTriple(firstDIgitInt, secondDigitInt, thirdDigitInt);

      boolean includeMe = false;
      if (isSingle && includeSingles) {
      includeMe = true;
      } else if (isDouble && includeDoubles) {
      includeMe = true;
      } else if (isTriple && includeTriples) {
      includeMe = true;
      }
      if (includeMe) {
      straights.add(straight);
      }
      }
      }
      }

      return straights;
      }

      private ArrayList<String> getAllPossiblePick3Straights() {
      ArrayList<String> straights = new ArrayList<String>();
      for (int firstDigit = 0; firstDigit <= MAX_NUMBER_IN_DIGIT; ++firstDigit) {
      for (int secondDigit = 0; secondDigit <= MAX_NUMBER_IN_DIGIT; ++secondDigit) {
      for (int thirdDigit = 0; thirdDigit <= MAX_NUMBER_IN_DIGIT; ++thirdDigit) {

      String straight = new Integer(firstDigit).toString() + new Integer(secondDigit).toString()
      + new Integer(thirdDigit).toString();
      straights.add(straight);

      }
      }
      }
      return straights;
      }

      private boolean isTriple(int firstDigit, int secondDigit, int thirdDigit) {
      return firstDigit == secondDigit && secondDigit == thirdDigit ? true : false;
      }

      private boolean isDouble(int firstDigit, int secondDigit, int thirdDigit) {
      return !isTriple(firstDigit, secondDigit, thirdDigit)
      && (firstDigit == secondDigit || firstDigit == thirdDigit || secondDigit == thirdDigit) ? true : false;
      }

      private boolean isSingle(int firstDigit, int secondDigit, int thirdDigit) {
      return !isTriple(firstDigit, secondDigit, thirdDigit) && !isDouble(firstDigit, secondDigit, thirdDigit) ? true
      : false;
      }

      public static Integer[] getStringToArray(String formattedValue) {
      Integer[] balls = new Integer[3];
      String pattern = "([^\\d]*[\\d])([^\\d]*[\\d])([^\\d]*[\\d])";

      Pattern r = Pattern.compile(pattern);
      Matcher m = r.matcher(formattedValue);

      if (m.find()) {
      for (int i = 1; i < 4; i++) {
      String groupValue = m.group(i);
      groupValue = groupValue.replaceAll("[^0-9]", "");
      balls[i - 1] = new Integer(groupValue);
      }
      } else {
      System.out.println("NO MATCH");
      }
      return balls;

      }

      public static Integer[] sortFirstThreeNumber(Integer[] generated) {
      Integer[] subArray = Arrays.copyOfRange(generated, 0, 3);
      Arrays.sort(subArray);
      return subArray;
      }
      }

       

       

      Formatting got messed up. You can get a hint on how to get it working. This can be even more simplified and refactored.

       

       

      Hope it helps