From 2c9444579a40fa654278e535a3b8ceedddebc27a Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Wed, 17 Jun 2020 17:27:27 -0400 Subject: [PATCH] Added function to get all permutations of a string --- src/Algorithms.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 19 ++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/Algorithms.rs b/src/Algorithms.rs index 267a217..ef9b5e5 100644 --- a/src/Algorithms.rs +++ b/src/Algorithms.rs @@ -376,3 +376,53 @@ pub fn getDivisorsBig(goalNumber: num::BigInt) -> Vec{ divisors.sort(); return divisors; } + +//This is a function that creates all permutations of a string and returns a vector of those permutations. +pub fn getPermutations(master: String) -> Vec::{ + return getPermutationsFull(master, 0); +} +fn getPermutationsFull(masterOrg: String, num: i32) -> Vec::{ + let mut master = masterOrg; + let mut perms = Vec::::new(); + //Check if the number is out of bounds + if((num >= master.len() as i32) || (num < 0)){ + //Do nothing and return an empty array + } + //If this is the last possible recurse just return the current string + else if(num == (master.len() - 1) as i32){ + perms.push(master); + } + //If there are more possible recurses, recurse with the current permutation + else{ + let mut temp = getPermutationsFull(master.clone(), num + 1); + perms.extend(temp); + //You need to swap the current letter with every possible letter after it + //The ones needed to swap before will happen automatically when the function recurses + let mut cnt = 1; + while((num + cnt) < master.len() as i32){ + master = swapString(master.clone(), num, (num + cnt)); + temp = getPermutationsFull(master.clone(), num + 1); + perms.extend(temp); + master = swapString(master.clone(), num, (num + cnt)); + cnt += 1; + } + + //The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning + perms.sort(); + } + + //Return the array that was built + return perms; +} +pub fn swapString(strng: String, first: i32, second: i32) -> String{ + let mut bytes = Vec::::new(); + bytes.extend_from_slice(strng.as_bytes()); + let temp = bytes[first as usize]; + bytes[first as usize] = bytes[second as usize]; + bytes[second as usize] = temp; + let mut swappedString = "".to_string(); + for loc in 0..bytes.len(){ + swappedString = format!("{}{}", swappedString, (bytes[loc] as char)); + } + return swappedString; +} diff --git a/src/lib.rs b/src/lib.rs index 65a4ff7..2641285 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,6 +103,25 @@ mod AlgorithmsTests{ let answer2 = super::Algorithms::getDivisorsBig(topNum2); assert_eq!(correctAnswer2, answer2); } + #[test] + fn testGetPermutations(){ + //Test + let permString = "012".to_string(); + let mut correctAnswer = Vec::::new(); + correctAnswer.extend_from_slice(&["012".to_string(), "021".to_string(), "102".to_string(), "120".to_string(), "201".to_string(), "210".to_string()]); + let answer = super::Algorithms::getPermutations(permString); + println!("answer: {:?}", answer); + assert_eq!(correctAnswer, answer); + } + #[test] + fn testSwapString(){ + //Test 1 + let string = "012".to_string(); + let correctAnswer = "210".to_string(); + let answer = super::Algorithms::swapString(string, 0, 2); + println!("swaped: {}", answer); + assert_eq!(correctAnswer, answer); + } }