# RoboScript #1 - Implement Syntax Highlighting

## Disclaimer

The story presented in this Kata Series is purely fictional; any resemblance to actual programming languages, products, organisations or people should be treated as purely coincidental.

## About this Kata Series

This Kata Series is based on a fictional story about a computer scientist and engineer who owns a firm that sells a toy robot called MyRobot which can interpret its own (esoteric) programming language called RoboScript.  Naturally, this Kata Series deals with the software side of things (I'm afraid Codewars cannot test your ability to build a physical robot!).

## Story

You are a computer scientist and engineer who has recently founded a firm which sells a toy product called MyRobot which can move by receiving a set of instructions by reading a file containing a script.  Initially you have planned the robot to be able to interpret JavaScript files for its movement instructions but you later decided that it would make MyRobot too hard to operate for most customers out there who aren't even computer programmers in the first place.  For this reason, you have decided to invent a new (esoteric) scripting language called RoboScript which has a much simpler syntax so non-computer programmers can easily learn how to write scripts in this language which would enable them to properly operate MyRobot.  However, you are currently at the initial stage of inventing this new Esolang.  The first step to popularize this (esoteric) scripting language is naturally to invent a new editor for it which provides syntax highlighting for this language so your customers feel like they are writing a proper program when they are writing scripts for MyRobot.

## Task

Your MyRobot-specific (esoteric) scripting language called RoboScript only ever contains the following characters: `F`, `L`, `R`, the digits `0-9` and brackets (`(` and `)`).  Your goal is to write a function `highlight` which accepts 1 required argument `code` which is the RoboScript program passed in as a string and returns the script with syntax highlighting.  The following commands/characters should have the following colors:

- `F` - Wrap this command around `` and `` tags so that it is highlighted pink in our editor
- `L` - Wrap this command around `` and `` tags so that it is highlighted red in our editor
- `R` - Wrap this command around `` and `` tags so that it is highlighted green in our editor
- Digits from `0` through `9` - Wrap these around `` and `` tags so that they are highlighted orange in our editor
- Round Brackets - Do not apply any syntax highlighting to these characters

For example:

And for multiple characters with the same color, simply wrap them with a **single** `` tag of the correct color:

Note that the use of `` tags must be **exactly** the same format as demonstrated above.  Even if your solution produces the same visual result as the expected answers, if you miss a space betwen `"color:"` and `"green"`, for example, you will fail the tests.

## Kata in this Series

1. **RoboScript #1 - Implement Syntax Highlighting**
2. [RoboScript #2 - Implement the RS1 Specification](https://www.codewars.com/kata/5870fa11aa0428da750000da)
3. [RoboScript #3 - Implement the RS2 Specification](https://www.codewars.com/kata/58738d518ec3b4bf95000192)
4. [RoboScript #4 - RS3 Patterns to the Rescue](https://www.codewars.com/kata/594b898169c1d644f900002e)
5. [RoboScript #5 - The Final Obstacle (Implement RSU)](https://www.codewars.com/kata/5a12755832b8b956a9000133)

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def highlight(code):
	

Solutions:
import re

def highlight(code):
	code = re.sub('(F+)', '<span style="color: pink">\\g<1></span>', code)
	code = re.sub('(L+)', '<span style="color: red">\\g<1></span>', code)
	code = re.sub('(R+)', '<span style="color: green">\\g<1></span>', code)
	code = re.sub('(\\d+)', '<span style="color: orange">\\g<1></span>', code)
	return code


import re
HL = {'R+': 'green', 'F+': 'pink', 'L+': 'red', '\\d+': 'orange'}
PATTERN_HL = re.compile('|'.join(HL))
HL_FORMAT = '<span style="color: {}">{}</span>'

def replacment(m):
	(s, k) = (m.group(), m.group()[0] + '+')
	return HL_FORMAT.format(HL[k], s) if k in HL else HL_FORMAT.format(HL['\\d+'], s) if s.isdigit() else s

def highlight(code):
	return PATTERN_HL.sub(replacment, code)


wrap = lambda s: s if s[0] in '()' else '<span style="color: ' + {'F': 'pink', 'L': 'red', 'R': 'green'}.get(s[0], 'orange') + '">' + s + '</span>'

def highlight(code):
	(r, last) = ('', '')
	for c in code:
		if last and c != last[-1] and (not (c.isdigit() and last.isdigit())):
			r += wrap(last)
			last = ''
		last += c
	return r + wrap(last)


from re import sub

def repl(match):
	COLORS = {'F': 'pink', 'L': 'red', 'R': 'green'}
	match = match.group()
	color = COLORS.get(match[0], 'orange')
	return f'<span style="color: {color}">{match}</span>'

def highlight(code):
	return sub('F+|L+|R+|\\d+', repl, code)


import re
colors = dict.fromkeys('0123456789', 'orange')
colors.update(F='pink', L='red', R='green')

def repl(m):
	c = m.group()
	return '<span style="color: {}">{}</span>'.format(colors[c[:1]], c)

def highlight(code):
	return re.sub('F+|L+|R+|[0-9]+', repl, code)


import re

def highlight(code):
	colors = dict(zip('FLR0123456789', ['pink', 'red', 'green'] + ['orange'] * 10))
	span = '<span style="color: {}">{}</span>'
	regex = '(F+|L+|R+|[0-9]+|\\(|\\))'
	highlight_command = lambda g: g if g in '()' else span.format(colors[g[:1]], g)
	return ''.join((highlight_command(m.group(0)) for m in re.finditer(regex, code)))


import re
span = '<span style="color: {0}">{1}</span>'
subs = {'F': 'pink', 'L': 'red', 'R': 'green', 'digit': 'orange'}

def sub(match):
	char = match.group(0)[0]
	if char.isdigit():
		return span.format(subs['digit'], match.group(0))
	else:
		return span.format(subs[char], match.group(0))

def highlight(code):
	return re.sub('(F+|L+|R+|[0-9]+)', sub, code)


import re

def highlight(code):
	patterns = {'F': 'pink', 'L': 'red', 'R': 'green', '0-9': 'orange'}
	new_string = code
	for (pattern, color) in patterns.items():
		_pattern = re.compile('([{}]+)'.format(pattern))
		new_string = _pattern.sub('<span style="color: {}">\\1</span>'.format(color), new_string)
	return new_string


highlight = lambda s: __import__('re').sub('([FLR])\\1*|\\d+', lambda m: '<span style="color: %s">%s</span>' % ({'F': 'pink', 'L': 'red', 'R': 'green'}.get(m.group()[0], 'orange'), m.group()), s)