Guys I'm having an issue with the print statement not printing to screen, wrote a code to print out every possible combination from a list of characters without having more than 2 sequential repetitions and with not 2 pattern repeating, but cant get it to print. this is the code...
use itertools::Itertools;
fn main() {
let characters = vec!['f', 'a', '4', '7', 'c', '5', 'b', '6', 'd', '1', '8', '3', '0', '9', '2', 'e'];
let start_length = 17;
let end_length = 18;
for length in start_length..=end_length {
println!("Generating combinations of length {}", length);
let keys = characters.iter().copied().permutations(length);
for combination in keys {
let string_combination: String = combination.iter().copied().collect();
println!("Checking combination: {}", string_combination); // Debug: Print each combination
if is_valid_combination(&combination) {
println!("Valid combination: {}", string_combination); // Debug: Print valid combinations
println!("00000000000000000000000000000000000000000000000{}", string_combination);
} else {
println!("Invalid combination: {}", string_combination); // Debug: Print invalid combinations
}
}
}
}
fn is_valid_combination(combination: &Vec<char>) -> bool {
for i in 0..combination.len() - 2 {
if combination[i] == combination[i+1] && combination[i+1] == combination[i+2] {
return false;
}
}
true
}
Thanks I just fixed that
But still having difficulty in making the code repeat elements in the list for the strings and they are to appear double at a position but not 3 same element together...
Please my coding sucks and I've been battling with this for long.
...but note that I added .take(100) so that it would terminate. There are about 21 trillion permutation of length 16, and there's going to be even more of the patterns you describe at the given lengths. And that's just the solutions! By brute-forcing it, you'll be looking at...
295,147,905,179,352,825,856 candidates of length 17 +
4,722,366,482,869,645,213,696 candidates of length 18
...5 sextillion or so candidates.
TL;DR enumeration probably isn't what you want and definitely brute forcing is not what you want.
Ok thanks for the response, but I needed to get the string to be sort of unique, a 16 or 17 character length that has same digits duplicated in any position on same string but not triple.
Do you need to go through all such strings in order, or would it work to generate random strings?
You could generate a random valid string one character at a time. If the last two characters are identical, then don’t include that character in the possibilities for generating the next character.
You could also enumerate combinations in a similar way, pruning any branches that lead to invalid strings, so you don’t need to skip past quintillions of invalid string before finding a valid one.
If you don't care about counting, and just want to generate some distinct IDs with those constraints, I'd suggest something like filtering the results from the uuid crate, and perhaps keeping a HashSet around to make sure they're also distinct.
If that's tenable for your actual goal, anyway; this is seeming like an XY topic.
Yes want to be sort of a sequential outputting in orderly sequence that is, trying every possible combinations except the triple same characters together in a particular string, but should cover every possible combination
Here's a simple recursive program. Printing all valid strings of length 17 and 18 could still take millions of years, so you might want to clarify what you actually need this program to do...
fn main() {
print_strings(&mut String::new(), 17);
print_strings(&mut String::new(), 18);
}
fn print_strings(prefix: &mut String, suffix_len: usize) {
if suffix_len == 0 {
println!("{prefix}");
return;
}
for c in "fa47c5b6d183092e".chars() {
if prefix.ends_with(c)
&& prefix[..prefix.len() - c.len_utf8()].ends_with(c)
{
// Don’t use the same char a third time in a row.
continue;
}
prefix.push(c);
print_strings(prefix, suffix_len - 1);
prefix.pop();
}
}
Thanks @mbrubeck
That's why I want to minimise the repetitions of characters in the final string, so instead of having ffaffaffaff5ddc3a we could skip having more than 2 double 'ff' in a particular string to just 2 and since 2 doubles already exist in the string no need for another. so instead we can have a unique string like ffa01ae4af15ddc3a. so we have a blend of 2 different doubles. All doubles be minimise to just 2 per string for uniqueness...
Thanks
There are 2.8 quadrillion strings (permutations) of length 17 composed of 16 unique characters with 1 used twice. There are 10.7 quadrillion strings of length 17 composed of 16 unique characters with 2 characters each used twice. There are ... I probably don't need to continue. If you can process 1 billion strings a second it will take over 11 days to process 1 quadrillion strings. Hope you have a very large parallel compute cluster for whatever it is you are trying to do.
You can alter the program above to match this new rule, by changing the logic about which characters to skip. But the program will still run take thousands or millions of years to run and will produce more output than you can possibly store or process. Again, if you can tell us what you are actually trying to accomplish, we could suggest a more efficient strategy.
Gracias bro what I am trying to do will reduce the number of valid strings to be printed to just a quarter of what is obtainable in the 17 character range, so I can do a considerable search.
Ok trying to get a unique array of printed hexes to match something like this:
93051F27B09112D400
9CCB5EFDACCF6808
9F838B93505B268671
As distinct and unique characters that don't look pedestrian but in a sequential order. So minimising the doubles to just 2 per string makes them unique.
I understand what sort of strings you want. What I don't understand is why you want to spend thousands of years printing N quadrillion of them in order. What are you planning to do with these strings after you generate them?
Anyway, this version filters out more than two “doubles” of the same character:
fn main() {
print_strings(&mut String::new(), 17);
print_strings(&mut String::new(), 18);
}
fn print_strings(prefix: &mut String, suffix_len: usize) {
if suffix_len == 0 {
println!("{prefix}");
return;
}
for c in "fa47c5b6d183092e".chars() {
if !skip_char(prefix, c) {
prefix.push(c);
print_strings(prefix, suffix_len - 1);
prefix.pop();
}
}
}
fn skip_char(prefix: &str, c: char) -> bool {
let double = format!("{c}{c}");
prefix.ends_with(&double) ||
(prefix.ends_with(c) && prefix.matches(&double).count() >= 2)
}