Skip to content

Commit 64b7b70

Browse files
authored
Add an algorithm for determining the number of possible rearrangements of a string (#129)
1 parent 7f00733 commit 64b7b70

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

text_manipulation/rearrangeWays.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
rearrangeWays <- function(string, as_report = TRUE){
2+
3+
if(as_report){ # conditional statement
4+
5+
# split the string into letters
6+
string_split <- c(unlist(strsplit(string,split = "")))
7+
8+
# get the factorial of the letters vector
9+
possible_ways <- factorial(length(string_split))
10+
11+
# create the answer text
12+
answer <- paste(string, "can be rearranged in", possible_ways, "possible ways")
13+
14+
15+
return(noquote(answer))
16+
17+
18+
}else{
19+
20+
# split the string into letters
21+
string_split <- c(unlist(strsplit(string,split = "")))
22+
23+
# get the factorial of the letters vector
24+
possible_ways <- factorial(length(string_split))
25+
26+
return(possible_ways)
27+
28+
}
29+
30+
31+
}
32+
33+
rearrangeWays(string = "straight")

0 commit comments

Comments
 (0)