Skip to content

Commit a823656

Browse files
authored
feat: Add Lindenmayer system algorithm (#230) (#230)
1 parent 19216b0 commit a823656

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,44 @@ public class LevenshteinDistanceSnippet {
19151915
}
19161916
```
19171917

1918+
### Lindenmayer System
1919+
1920+
```java
1921+
public class LindenmayerSystemSnippet {
1922+
/**
1923+
* Generates an L-system string based on axiom, production rules, and a number of iterations.
1924+
*
1925+
* @param axiom initial string to begin the L-system
1926+
* @param productionRules map of character rules where each symbol can be replaced with a string
1927+
* @param iterations number of iterations to apply the production rules
1928+
* @return the generated string after all iterations
1929+
*/
1930+
public static String generateLindenmayerSystem(
1931+
String axiom,
1932+
Map<Character, String> productionRules,
1933+
int iterations
1934+
) {
1935+
String current = axiom;
1936+
1937+
for (int i = 0; i < iterations; i++) {
1938+
StringBuilder nextIteration = new StringBuilder(current.length() * 2);
1939+
1940+
// Replace each symbol with the corresponding production rule or the symbol itself
1941+
current.chars()
1942+
.mapToObj(c -> (char) c)
1943+
.forEach(symbol ->
1944+
nextIteration.append(
1945+
productionRules.getOrDefault(symbol, String.valueOf(symbol))
1946+
)
1947+
);
1948+
1949+
current = nextIteration.toString();
1950+
}
1951+
return current;
1952+
}
1953+
}
1954+
```
1955+
19181956
### Max Character Count
19191957

19201958
```java
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2024 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package string;
26+
27+
import java.util.Map;
28+
29+
/**
30+
* LSystemSnippet.
31+
*/
32+
public class LindenmayerSystemSnippet {
33+
/**
34+
* Generates an L-system string based on axiom, production rules, and a number of iterations.
35+
*
36+
* @param axiom initial string to begin the L-system
37+
* @param productionRules map of character rules where each symbol can be replaced with a string
38+
* @param iterations number of iterations to apply the production rules
39+
* @return the generated string after all iterations
40+
*/
41+
public static String generateLindenmayerSystem(
42+
String axiom,
43+
Map<Character, String> productionRules,
44+
int iterations
45+
) {
46+
String current = axiom;
47+
48+
for (int i = 0; i < iterations; i++) {
49+
StringBuilder nextIteration = new StringBuilder(current.length() * 2);
50+
51+
// Replace each symbol with the corresponding production rule or the symbol itself
52+
current.chars()
53+
.mapToObj(c -> (char) c)
54+
.forEach(symbol ->
55+
nextIteration.append(
56+
productionRules.getOrDefault(symbol, String.valueOf(symbol))
57+
)
58+
);
59+
60+
current = nextIteration.toString();
61+
}
62+
return current;
63+
}
64+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2024 Ilkka Seppälä
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package string;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
import org.junit.jupiter.api.Test;
32+
33+
34+
/*
35+
* Tests for 30 Seconds of Java code library
36+
*
37+
*/
38+
class LindenmayerSystemSnippetTest {
39+
40+
/**
41+
* Tests for {@link LindenmayerSystemSnippet#generateLindenmayerSystem(String, Map, int)}.
42+
*/
43+
@Test
44+
public void testGenerateLindenmayerSystems() {
45+
String axiom = "A";
46+
47+
48+
Map<Character, String> productionRules = new HashMap<>();
49+
productionRules.put('A', "AB");
50+
productionRules.put('B', "A");
51+
52+
53+
assertEquals(
54+
"AB",
55+
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 1)
56+
);
57+
assertEquals(
58+
"ABA",
59+
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 2)
60+
);
61+
assertEquals(
62+
"ABAAB",
63+
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 3)
64+
);
65+
assertEquals(
66+
axiom,
67+
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 0)
68+
);
69+
}
70+
}

0 commit comments

Comments
 (0)