Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add LindenmayerSystemSnippet & tests and Update README.md #230

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,44 @@ public class LevenshteinDistanceSnippet {
}
```

### Lindenmayer System

```java
public class LindenmayerSystemSnippet {
/**
* Generates an L-system string based on axiom, production rules, and a number of iterations.
*
* @param axiom initial string to begin the L-system
* @param productionRules map of character rules where each symbol can be replaced with a string
* @param iterations number of iterations to apply the production rules
* @return the generated string after all iterations
*/
public static String generateLindenmayerSystem(
String axiom,
Map<Character, String> productionRules,
int iterations
) {
String current = axiom;

for (int i = 0; i < iterations; i++) {
StringBuilder nextIteration = new StringBuilder(current.length() * 2);

// Replace each symbol with the corresponding production rule or the symbol itself
current.chars()
.mapToObj(c -> (char) c)
.forEach(symbol ->
nextIteration.append(
productionRules.getOrDefault(symbol, String.valueOf(symbol))
)
);

current = nextIteration.toString();
}
return current;
}
}
```

### Max Character Count

```java
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/string/LindenmayerSystemSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2017-2024 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package string;

import java.util.Map;

/**
* LSystemSnippet.
*/
public class LindenmayerSystemSnippet {
/**
* Generates an L-system string based on axiom, production rules, and a number of iterations.
*
* @param axiom initial string to begin the L-system
* @param productionRules map of character rules where each symbol can be replaced with a string
* @param iterations number of iterations to apply the production rules
* @return the generated string after all iterations
*/
public static String generateLindenmayerSystem(
String axiom,
Map<Character, String> productionRules,
int iterations
) {
String current = axiom;

for (int i = 0; i < iterations; i++) {
StringBuilder nextIteration = new StringBuilder(current.length() * 2);

// Replace each symbol with the corresponding production rule or the symbol itself
current.chars()
.mapToObj(c -> (char) c)
.forEach(symbol ->
nextIteration.append(
productionRules.getOrDefault(symbol, String.valueOf(symbol))
)
);

current = nextIteration.toString();
}
return current;
}
}
70 changes: 70 additions & 0 deletions src/test/java/string/LindenmayerSystemSnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* MIT License
*
* Copyright (c) 2017-2024 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package string;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;


/*
* Tests for 30 Seconds of Java code library
*
*/
class LindenmayerSystemSnippetTest {

/**
* Tests for {@link LindenmayerSystemSnippet#generateLindenmayerSystem(String, Map, int)}.
*/
@Test
public void testGenerateLindenmayerSystems() {
String axiom = "A";


Map<Character, String> productionRules = new HashMap<>();
productionRules.put('A', "AB");
productionRules.put('B', "A");


assertEquals(
"AB",
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 1)
);
assertEquals(
"ABA",
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 2)
);
assertEquals(
"ABAAB",
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 3)
);
assertEquals(
axiom,
LindenmayerSystemSnippet.generateLindenmayerSystem(axiom, productionRules, 0)
);
}
}
Loading