-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial commit The source is fully working and needs no fixes. * Revert "Initial commit" This reverts commit 24cd9e86be563964c8af785664c7b08d7df87686. * asdwa * adwa asdwa * Revert "Initial commit" This reverts commit 24cd9e86be563964c8af785664c7b08d7df87686. * Initial commit The source is fully working and needs no fixes.
- Loading branch information
1 parent
1c26147
commit 84f3df5
Showing
4 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
nb-configuration.xml | ||
nbactions-release-profile.xml | ||
nbactions.xml | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# combin: Binary Combiner | ||
|
||
`combin` is a Java-based utility to generate all possible binary combinations of a given size `n`. It offers flexible output options: print results to the console, save them to a file, or store them in memory. | ||
|
||
## Features | ||
|
||
- **Generate binary combinations**: For any size `n` (up to \(2^{16}\)). | ||
- **Customizable characters**: Replace `0` and `1` with any other characters. | ||
- **Output options**: | ||
- Print to the console. | ||
- Save to a file. | ||
- Store in an `ArrayList`. | ||
|
||
--- | ||
|
||
## Requirements | ||
|
||
- Java 8 or higher. | ||
|
||
--- | ||
|
||
## How to Use | ||
|
||
### Compile the Code | ||
```bash | ||
javac Combin.java | ||
``` | ||
|
||
### Run the Program | ||
```bash | ||
java Combin <size> [bin0] [bin1] [filename] [console] | ||
``` | ||
|
||
### Parameters | ||
1. `<size>`: (Required) The size of the binary combinations. Must be a positive integer ≤ 16. | ||
2. `[bin0]`: (Optional) Character to use instead of `0`. | ||
3. `[bin1]`: (Optional) Character to use instead of `1`. | ||
4. `[filename]`: (Optional) File to save the combinations. If provided, the program writes the combinations to this file. | ||
5. `[console]`: (Optional) If provided and set to `console`, results are printed to the console. | ||
|
||
--- | ||
|
||
## Examples | ||
|
||
### Default Binary Output | ||
Generate all binary combinations of size 3 and print them to the console: | ||
```bash | ||
java Combin 3 console | ||
``` | ||
|
||
### Custom Characters | ||
Generate combinations of size 3 using `X` and `Y` instead of `0` and `1`: | ||
```bash | ||
java Combin 3 X Y console | ||
``` | ||
|
||
### Save to a File | ||
Generate combinations of size 4 and save them to `output.txt`: | ||
```bash | ||
java Combin 4 0 1 output.txt | ||
``` | ||
|
||
### Save and Print | ||
Generate combinations of size 4, save them to `output.txt`, and also print them: | ||
```bash | ||
java Combin 4 0 1 output.txt console | ||
``` | ||
|
||
--- | ||
|
||
## Error Handling | ||
- **Invalid size**: If `<size>` is not provided, not a number, or exceeds the limit, an error is displayed. | ||
- **Invalid characters**: Binary characters must be single characters. | ||
- **File write issues**: Errors during file operations are logged to the console. | ||
|
||
--- | ||
|
||
## Example Output | ||
|
||
### Command: | ||
```bash | ||
java Combin 2 console | ||
``` | ||
|
||
### Output: | ||
``` | ||
Generating binary combinations for size: 2 | ||
Generated combinations: | ||
00 | ||
01 | ||
10 | ||
11 | ||
``` | ||
|
||
--- | ||
|
||
## Author | ||
|
||
- **Habis Muhammed** | ||
GitHub: [BrownCoatJustice](https://github.com/BrownCoatJustice) | ||
|
||
--- | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. See the `LICENSE` file for details. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.github.BrownCoatJustice</groupId> | ||
<artifactId>combin</artifactId> | ||
<version>0.1-INDEV</version> | ||
<packaging>jar</packaging> | ||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin --> | ||
<dependency> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.4.2</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.4.2</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<addClasspath>true</addClasspath> | ||
<mainClass>com.github.browncoatjustice.combin.Combin</mainClass> | ||
</manifest> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.release>21</maven.compiler.release> | ||
<exec.mainClass>com.github.browncoatjustice.combin.Combin</exec.mainClass> | ||
</properties> | ||
</project> |
165 changes: 165 additions & 0 deletions
165
src/main/java/com/github/browncoatjustice/combin/Combin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||
*/ | ||
package com.github.browncoatjustice.combin; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* combin - the binary combiner, of sorts. Read below: (and yes, its all small letters, completely intentional. | ||
* Generates all possible combinations of binary values of size n. | ||
* The combinations can be printed to the console, stored in an ArrayList, or written to a file. | ||
* @author habis | ||
*/ | ||
public class Combin { | ||
|
||
// Default binary characters (can be customized by the user) | ||
private static char bin0 = '0'; | ||
private static char bin1 = '1'; | ||
|
||
// List to store results if the user selects "arraylist" option | ||
private static List<String> outcomes = new ArrayList<>(); | ||
|
||
// Maximum size of the binary combinations (set limit for short type) | ||
private static final short MAX_SIZE = 16; // 2^16 combinations max | ||
|
||
public static void main(String[] args) { | ||
// System.out.println("Hello World!"); | ||
if (argsParse(args)) { | ||
if (outcomes.size() > 0) { | ||
System.out.println("Generated combinations:"); | ||
for (String outcome : outcomes) { | ||
System.out.println(outcome); // Print outcomes to console as requested | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static boolean argsParse(String[] args) { | ||
if (args.length > 0) { | ||
try { | ||
// Parse the first argument as the memory size (n), using short | ||
short m = Short.parseShort(args[0]); | ||
|
||
if (m <= 0 || m > MAX_SIZE) { | ||
System.err.println("Error: Memory size (n) must be a positive number and less than or equal to " + MAX_SIZE); | ||
return false; | ||
} | ||
|
||
System.out.println("Generating binary combinations for size: " + m); | ||
|
||
// Optionally, parse the second and third arguments as binary characters | ||
if (args.length > 1) { | ||
bin0 = args[1].charAt(0); | ||
if (args[1].length() != 1) { | ||
throw new IllegalArgumentException("Binary character must be a single character."); | ||
} | ||
} | ||
|
||
if (args.length > 2) { | ||
bin1 = args[2].charAt(0); | ||
if (args[2].length() != 1) { | ||
throw new IllegalArgumentException("Binary character must be a single character."); | ||
} | ||
} | ||
|
||
String filename = null; | ||
Boolean printToConsole = false; | ||
|
||
// Check if the filename is provided | ||
if (args.length > 3) { | ||
filename = args[3]; | ||
printToConsole = false; // User only wants to output to a file | ||
} | ||
|
||
// Check if the user wants to print to console as well | ||
if (args.length > 4 && args[4].equalsIgnoreCase("console")) { | ||
printToConsole = true; | ||
} | ||
|
||
// Call the appropriate overloaded method | ||
if (filename != null && printToConsole) { | ||
generateOutcomes(m, "", filename, printToConsole); // Both console and file output | ||
} else if (filename != null) { | ||
generateOutcomes(m, "", filename, null); // Only file output (null for printToConsole) | ||
} else { | ||
generateOutcomes(m, "", null, printToConsole); // Only console output (null for filename) | ||
} | ||
|
||
return true; | ||
|
||
} catch (NumberFormatException e) { | ||
System.err.println("Error: Memory size (n) must be a valid integer. Please provide a valid positive integer."); | ||
} catch (IllegalArgumentException e) { | ||
System.err.println("Error: " + e.getMessage()); | ||
} catch (Exception e) { | ||
System.err.println("An unexpected error occurred: " + e.getMessage()); | ||
} | ||
} else { | ||
System.err.println("Error: No input provided. Please provide the size of binary combinations (n)."); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Generates binary combinations of size n recursively and prints the | ||
* results to the console. It also saves the results in the ArrayList. | ||
* | ||
* @param n The size of the binary combination. | ||
* @param current The current combination being generated. | ||
*/ | ||
public static void generateOutcomes(short n, String current) { | ||
generateOutcomes(n, current, null, null); // Default to console output and no filename | ||
} | ||
|
||
/** | ||
* Generates binary combinations of size n recursively and stores or prints | ||
* the results to a file. | ||
* | ||
* @param n The size of the binary combination. | ||
* @param current The current combination being generated. | ||
* @param filename The filename to save the results to (if provided). | ||
*/ | ||
public static void generateOutcomes(short n, String current, String filename) { | ||
generateOutcomes(n, current, filename, null); // Only file output | ||
} | ||
|
||
/** | ||
* Generates binary combinations of size n recursively and stores or prints | ||
* the results. | ||
* | ||
* @param n The size of the binary combination. | ||
* @param current The current combination being generated. | ||
* @param filename The filename to save the results to (if provided). | ||
* @param printToConsole Whether to print results to the console (if true). | ||
*/ | ||
public static void generateOutcomes(short n, String current, String filename, Boolean printToConsole) { | ||
// Base case: when n is 0, the combination is complete | ||
if (n == 0) { | ||
try { | ||
// Output depending on the format selected | ||
if (filename != null) { | ||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename, true))) { | ||
writer.write(current + "\n"); | ||
} | ||
} | ||
if (printToConsole != null && printToConsole) { | ||
System.out.println(current); // Print to console if the flag is set | ||
} | ||
outcomes.add(current); // Store the combination in the ArrayList | ||
} catch (IOException e) { | ||
System.err.println("Error: Failed to write to file \"" + filename + "\". " + e.getMessage()); | ||
} | ||
return; | ||
} | ||
|
||
// Recursive calls to generate combinations by adding bin0 and bin1 | ||
generateOutcomes((short) (n - 1), current + bin0, filename, printToConsole); // Add bin0 to the combination | ||
generateOutcomes((short) (n - 1), current + bin1, filename, printToConsole); // Add bin1 to the combination | ||
} | ||
} |