The dragon's curve is a self-similar fractal which can be obtained by a recursive method. 

Starting with the string `D0 = 'Fa'`, at each step simultaneously perform the following operations:

```
replace 'a' with: 'aRbFR'
replace 'b' with: 'LFaLb'
```

For example (spaces added for more visibility) : 

```
1st iteration: Fa -> F aRbF R
2nd iteration: FaRbFR -> F aRbFR R LFaLb FR
```

After `n` iteration, remove `'a'` and `'b'`. You will have a string with `'R'`,`'L'`, and `'F'`. This is a set of instruction. Starting at the origin of a grid looking in the `(0,1)` direction, `'F'` means a step forward, `'L'` and `'R'` mean respectively turn left and right. After executing all instructions, the trajectory will give a beautifull self-replicating pattern called 'Dragon Curve'

The goal of this kata is to code a function wich takes one parameter `n`, the number of iterations needed and return the string of instruction as defined above. For example: 

```
n=0, should return: 'F'
n=1, should return: 'FRFR'
n=2, should return: 'FRFRRLFLFR'
```

`n` should be a number and non-negative integer. All other case should return the empty string: `''`.

Source: codewars | Difficulty: EASY
Tags: Mathematics

Complete search

Starter code: def Dragon(n):
	

Solutions:
def Dragon(n):
	if not isinstance(n, int) or n < 0:
		return ''
	value = 'Fa'
	for i in range(n):
		value = value.replace('a', 'aRcFR')
		value = value.replace('b', 'LFaLb')
		value = value.replace('c', 'b')
	return value.replace('a', '').replace('b', '')


def Dragon(n):
	if not isinstance(n, int) or n < 0:
		return ''
	s = 'Fa'
	for i in range(n):
		s = 'aRbFR'.join((t.replace('b', 'LFaLb') for t in s.split('a')))
	return s.replace('a', '').replace('b', '')


import re

def Dragon(n):
	if not isinstance(n, int) or n < 0:
		return ''
	sub_table = {'a': 'aRbFR', 'b': 'LFaLb'}
	s = 'Fa'
	for i in range(0, n):
		s = re.sub('[ab]', lambda m: sub_table[m.group()], s)
	return s.translate(None, 'ab')


def Dragon(n):
	if type(n) != type(1):
		return ''
	if n < 0:
		return ''
	if n == 0:
		return 'F'
	ret = 'Fa'
	while n > 0:
		temp = ''
		for word in ret:
			if word == 'a':
				temp += 'aRbFR'
			elif word == 'b':
				temp += 'LFaLb'
			else:
				temp += word
		ret = temp
		n -= 1
	ret = ret.replace('a', '')
	ret = ret.replace('b', '')
	return ret


def Dragon(n, strng='Fa'):
	if not isinstance(n, int) or n < 0:
		return ''
	if not n:
		return strng.translate(None, 'ab')
	next = ''.join(('aRbFR' if c == 'a' else 'LFaLb' if c == 'b' else c for (i, c) in enumerate(strng)))
	return Dragon(n - 1, next)


def Dragon(n):
	if not isinstance(n, int) or n < 0:
		return ''
	s = 'Fa'
	for _ in range(n):
		s = ''.join(({'a': 'aRbFR', 'b': 'LFaLb'}.get(c, c) for c in s))
	return s.replace('a', '').replace('b', '')


def Dragon(n):
	if type(n) != int or n < 0:
		return ''
	(stg, a, b) = ('F{a}', '{a}R{b}FR', 'LF{a}L{b}')
	for _ in range(n):
		stg = stg.format(a=a, b=b)
	return stg.replace('{a}', '').replace('{b}', '')


def Dragon(n):
	if not isinstance(n, int) or n < 0:
		return ''
	d = 'Fa'
	for i in range(n):
		d = ''.join(('aRbFR' if c == 'a' else 'LFaLb' if c == 'b' else c for c in d))
	return d.translate(dict.fromkeys(map(ord, 'ab')))


def Dragon(n, Curve='Fa'):
	if type(n) != int or n % 1 != 0 or n < 0:
		return ''
	elif n == 0:
		return Curve.replace('a', '').replace('b', '')
	else:
		return Dragon(n - 1, Curve.replace('a', 'c').replace('b', 'd').replace('c', 'aRbFR').replace('d', 'LFaLb'))