L-system: Difference between revisions

1,055 bytes added ,  17 days ago
Added C++ and Dart
(Added Python)
(Added C++ and Dart)
Line 328:
{{out}}
<pre>After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)</pre>
 
=={{header|C++}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <string>
 
using namespace std;
 
void lindenmayer(string s, map<char, string> rules, int count) {
for (int i = 0; i < count; ++i) {
cout << s << endl;
string nxt = "";
for (char c : s) {
if (rules.find(c) != rules.end()) {
nxt += rules[c];
} else {
nxt += c;
}
}
s = nxt;
}
}
 
int main() {
map<char, string> rules = {{'I', "M"}, {'M', "MI"}};
lindenmayer("I", rules, 5);
return 0;
}</syntaxhighlight>
{{out}}
<pre>I
M
MI
MIM
MIMMI</pre>
 
=={{header|Dart}}==
{{trans|C++}}
<syntaxhighlight lang="dart">void lindenmayer(String s, Map<String, String> rules, int count) {
for (int i = 0; i < count; ++i) {
print(s);
String nxt = "";
for (int j = 0; j < s.length; ++j) {
String c = s[j];
nxt += rules.putIfAbsent(c, () => c);
}
s = nxt;
}
}
 
void main() {
var rules = {"I": "M", "M": "MI"};
lindenmayer("I", rules, 5);
}</syntaxhighlight>
{{out}}
<pre>Same as C++ entry.</pre>
 
=={{header|FreeBASIC}}==
2,169

edits