## Task

Complete the function that receives an array of strings (`arr`) as an argument and returns all the valid Roman numerals.

Basic Roman numerals are denoted as:

```
I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000
```

For the purposes of this kata we will consider valid only the numbers in range 0 - 5000 (both exclusive) since numbers >= 5000 were written in a different way (you had to place a heavy bar over the numeral that meant it was multiplied with 1000).

There are other ways of tackling this problem but the easiest is probably writing a Regular Expression.

### Let's break the problem down:

To match a set of characters `/[1-9]/`(single digits) you should take into consideration the Roman numbers `I, II, III, IV, V, VI, VII, VIII, IX`. This could be done by testing with `/IX|IV|V?I{0,3}/`. This part `/I{0,3}/` matches `I, II or III` but we have a `V` appearing 0 or 1 times because of the `?` so `/V?I{0,3}/` would match `I,II,III,V,VI,VII or VIII`. However there is one flaw with this. Do you see it? It is the fact that it would also match an empty string `""` because of the {0,3}. In order to pass the tests you will have to **filter out the empty strings** as well. So the entire part matches `I to IX`(inclusive) but what about larger digits?

Use the same logic for the digit in the tens place and the hundreds place. Be sure to wrap each part (units, tens, hundreds, thousands) in a pair of braces `(IX|IV|V?I{0,3})` and for the digit in the thousands place the logic is pretty straight forward, you just have to match `M` 0 to 4 times (since 5000 is not included). Wrap everything up with `^` and `$` to make sure you match the entire string (^ matches from the beginning of the string, while $ denotes the end, meaning there is nothing after that sign.

## Examples

```
["I", "IIV", "IVI", "IX", "XII", "MCD"] ==> ["I", "IX", "XII", "MCD"]

["MMMMCMXCIX", "MMDCXLIV", "MMCIX", "CLD", "LCD"]) ==> ["MMMMCMXCIX", "MMDCXLIV", "MMCIX"]
```

Good luck!

Difficulty: introductory

Starter code:

def valid_romans(arr):
	

Solutions:
import re

PATTERN = re.compile("^"
    "M{0,4}"             # thousands
    "(CM|CD|D?C{,3})"    # hundreds
    "(XC|XL|L?X{,3})"    # tens
    "(IX|IV|V?I{,3})"    # units
    "$")

def valid_romans(arr):
    return [e for e in arr if e and PATTERN.match(e)]

import re


def valid_romans(arr):
    """Return all elements of 'arr' which are correct Roman numbers between 1 and 4999."""
    return [roman for roman in arr if roman and re.fullmatch(
        "(M{,4})"
        "(C[MD]|D?C{,3})"
        "(X[CL]|L?X{,3})"
        "(I[XV]|V?I{,3})", roman)]

check = __import__("re").compile(r"(?=[MDCLXVI])M*(C[MD]|D?C*)(X[CL]|L?X*)(I[XV]|V?I*)").fullmatch

def valid_romans(arr):
    return list(filter(check, arr))

import re
def valid_romans(arr):
    return [num for num in arr if num and re.match(r'^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$', num)]

import re

def valid_romans(arr):
    pattern = re.compile(r"^M{0,4}(CM|CD|D?C{0,3})?(XC|XL|L?X{0,3})?(IX|IV|V?I{0,3})$")
    return [number for number in arr if number and pattern.match(number)]

def valid(n):
    if not n:  return False
    import re
    return re.fullmatch('(M|MM|MMM|MMMM)?'
                        '(C|CC|CCC|CD|D|DC|DCC|DCCC|CM)?'
                        '(X|XX|XXX|XL|L|LX|LXX|LXXX|XC)?'
                        '(I|II|III|IV|V|VI|VII|VIII|IX)?', n)

def valid_romans(arr):
    return [n for n in arr if valid(n)]

import re

def valid_romans(arr):
    return [e for e in arr if e
        and re.match("^M{,4}(CM|CD|D?C{,3})(XC|XL|L?X{,3})(IX|IV|V?I{,3})$", e)]

ones = [''] + 'I II III IV V VI VII VIII IX'.split()
tens = [''] + 'X XX XXX XL L LX LXX LXXX XC'.split()
hundreds = [''] + 'C CC CCC CD D DC DCC DCCC CM'.split()
thousands = [''] + 'M MM MMM MMMM'.split()
good = ['MMMMM'] + [i+j+k+l for i in thousands for j in hundreds for k in tens for l in ones]

def valid_romans(arr):
    return [i for i in arr if i and i in good]

from re import fullmatch
def valid_romans(arr):
    units = '((IV)|(IX)|(VI{0,3})|(I{1,3}))?'
    tens = '((XC)|(XL)|(LX{0,3})|X{1,3})?'
    hndr = '((CD)|(CM)|(DC{0,3})|C{1,3})?'
    ths = '(M{1,4}){0,1}'
    regex = ths + hndr  + tens + units 
    return [s for s in arr if fullmatch(regex,s) and 0 < len(s)]