/**
* @description L-systems are used to model naturally occurring patterns in nature such as growth of trees, growth of multicellular organisms, fractals etc.
* by recursively applying a set of simple grammar rules over a set of variables starting with an Axiom.
* @param {number} numberOfIteration - The initial string of symbols to be used as the axiom.
* @param {Array<[string, string]>} condition - Array of pairs of strings (The initial string of symbols to be used as the axiom.) to be compared. Length of the array
* must be not more than 2. Extra elements will be ignored.
* @returns {string} - The final string of symbols after the specified number of iterations.
* @ Author: Sarthak Dwivedi
*/
const lSystemGenerator = (numberOfIteration, condition) => {
const [ruleOne, ruleTwo] = condition;
let txt = ruleOne[0];
for (let i = 0; i < numberOfIteration - 1; i++) {
let newTxt = '';
for (let j = 0; j < txt.length; j++) {
let letter = txt.substring(j, j + 1);
if (letter === ruleOne[0]) {
newTxt += ruleOne[1];
} else {
newTxt += ruleTwo[1];
}
}
txt = `${txt}\n${newTxt}`;
}
return txt;
};
// Test case 1 - for 3 iterations
console.log(
lSystemGenerator(3, [
['A', 'ABA'],
['B', 'BBB'],
])
);

Solve the following problem in your favorite programming language and send the program to us.
Problem: [L-Systems]: L-systems are used to model naturally occurring patterns in nature such as growth of trees, growth of multicellular organisms, fractals etc. by recursively applying a set of simple grammar rules over a set of variables starting with an Axiom.
For example: Growth of Algae in L-systems would be modelled as.
variables : A B
constants : none
axiom : A {starting character string}
rules : (A → AB), (B → A)
which produces:
n = 0 : S=A
n = 1 : S=AB
n = 2 : S=ABA
n = 3 : S=ABAAB
n = 4 : S=ABAABABA
n = 5 : S=ABAABABAABAAB
Given the upper bound ‘n’, design a class D for L-systems with variables, constants, axiom and rules. Write a function ‘produce’ that takes in the object of ‘D’ and ‘n’ as parameters and returns the L-systems string ‘S’. Tell the space and time complexity. How can you optimize your solution and what's the optimized space and time complexity?

// -Problem Statement----------

Test your program using the following L-system (Cantor fractal) where n=3 and write your answer:
variables : A B
constants : none
start : A
rules : (A → ABA), (B → BBB)

sign in to comment