In the wake of the npm's `left-pad` debacle, you decide to write a new super padding method that superceds the functionality of `left-pad`. Your version will provide the same functionality, but will additionally add right, and justified padding of string -- the `super_pad`.

Your function `super_pad` should take three arguments: the string `string`, the width of the final string `width`, and a fill character `fill`. However, the fill character can be enriched with a format string resulting in different padding strategies. If `fill` begins with `'<'` the string is padded on the left with the remaining fill string and if `fill` begins with `'>'` the string is padded on the right. Finally, if `fill` begins with `'^'` the string is padded on the left and the right, where the left padding is always greater or equal to the right padding. The `fill` string can contain more than a single char, of course. 

Some examples to clarify the inner workings:

- `super_pad("test", 10)` returns "      test"
- `super_pad("test", 10, "x")` returns `"xxxxxxtest"`
- `super_pad("test", 10, "xO")` returns `"xOxOxOtest"`
- `super_pad("test", 10, "xO-")` returns `"xO-xO-test"`
- `super_pad("some other test", 10, "nope")` returns `"other test"`
- `super_pad("some other test", 10, "> ")` returns `"some other"`
- `super_pad("test", 7, ">nope")` returns `"testnop"`
- `super_pad("test", 7, "^more complex")` returns `"motestm"`
- `super_pad("test", 7, "")` returns `"test"`

The `super_pad` method always returns a string of length `width` if possible. We expect the `width` to be positive (including 0) and the fill could be also an empty string.

Difficulty: introductory

Starter code:

def super_pad(string, width, fill=" "):
	

Solutions:
def super_pad(string, width, fill=" "):
    if fill.startswith('>'):
        return (string + width * fill[1:])[:width]
    elif fill.startswith('^'):
        pad = (width * fill[1:])[:max(0, width - len(string) + 1) // 2]
        return (pad + string + pad)[:width]
    else:
        if fill.startswith('<'): fill = fill[1:]
        return (width * fill)[:max(0, width - len(string))] + string[max(0, len(string) - width):]

def super_pad(s, width, fill=" "):
    padding = width - len(s)
    if width == 0: return ''
    
    def _left_pad():
        if padding >= 0:
            return (fill * padding)[:padding] + s
        return s[-width:]
    
    def _right_pad():
        if padding >= 0:
            return s + (fill * padding)[:padding]
        return s[:width]
    
    def _center_pad():
        right = padding // 2
        left = padding - right
        if padding >= 0:
            return (fill * left)[:left] + s + (fill * right)[:right]
        return s[-left:right]
    
    if fill and fill[0] == '>':
        fill = fill[1:]
        return _right_pad()
    if fill and fill[0] == '^':
        fill = fill[1:]
        return _center_pad()
    if fill and fill[0] == '<':
        fill = fill[1:]
    return _left_pad()

def super_pad(string, width, fill=" "):
    if not fill or fill in '<^>':
        return string
    
    fill_len = width - len(string)
    
    # check padding mode
    if fill[0] == '>':
        l_pad, r_pad = 0, fill_len
    elif fill[0] == '^':
        l_pad, r_pad = (fill_len + 1) // 2, fill_len // 2
    else:
        l_pad, r_pad = fill_len, 0
    
    # if width < string length
    if fill_len < 0:
        return string[-l_pad if l_pad else None : r_pad if r_pad else None]
    
    # adjust the fill string
    if fill[0] in '<^>':
        fill = fill[1:]
    fill *= fill_len // len(fill) + 1
    
    return fill[:l_pad] + string + fill[:r_pad]

def super_pad(string, width, fill=" "):
  if not fill: return string
  needed = width - len(string)
    
  if fill[0] == ">":
      if needed < 0: return string[:needed]
      return string + (fill[1:] * needed)[:needed]
  elif fill[0] == "^":
      filler = fill[1:] * needed
      return (filler[:needed // 2 + needed % 2] + string + filler)[:width]
  else:
      if fill[0] == "<": fill = fill[1:]
      if needed < 0: return string[-needed:]
      return (fill * needed)[:needed] + string


def super_pad(s,w,f=" "):
    if not f:return s
    if not w:return ""
    if f[0]=='>':return (s+f[1:]*w)[:w]
    if f[0]=='^':
        l=r=(f[1:]*w)[:(w-len(s))//2+(1 if (w-len(s))&1 else 0)]
        return (l+s+r)[:w]
    return (((f[1:] if f[0]=='<' else f)*w)[:w-len(s)]+s)[-w:]

def super_pad(string, width, fill=" "):
    str_len = len(string)
    ###### RIGHT
    if fill.find('>')+1:
        fill = fill[1:]
        fill_len = len(fill)
        to_fill_len = width - str_len
        if width < str_len: # For any: fill_len
            retval = string[0:width]
        # For all below: width >= str_len
        elif fill_len == 0:
            retval = string
        elif fill_len == 1:
            RHS_to_fill = width - str_len
            retval = string + fill*RHS_to_fill
        elif fill_len > 1:
            chunks = int(to_fill_len / fill_len)
            parts  = to_fill_len % fill_len
            total_len = (chunks*fill_len+parts) # DEBUG
            retvalb = string + (fill*(chunks+1))[0:total_len]
            retval = retvalb
        else:
            retval = 'RIGHT: Oops'
    ##### CENTER
    elif fill.find('^')+1:
        fill = fill[1:]
        fill_len = len(fill)
        to_fill_len = int((width - str_len) / 2)
        LHS_to_fill_len = to_fill_len + (width - str_len) % 2
        RHS_to_fill_len = to_fill_len
        if width < str_len: # For any: fill_len
            retval = string[0:width]
        # For all below: width >= str_len
        elif fill_len == 0:
            retval = string
        else:
            LHS_chunks = int(LHS_to_fill_len / fill_len)
            LHS_parts  = LHS_to_fill_len % fill_len
            LHS_total_len = (LHS_chunks*fill_len+LHS_parts)
            RHS_chunks = int(RHS_to_fill_len / fill_len)
            RHS_parts  = RHS_to_fill_len % fill_len
            RHS_total_len = (RHS_chunks*fill_len+RHS_parts)
            retval = (fill*(LHS_chunks+1))[0:LHS_total_len] + string + (fill*(RHS_chunks+1))[0:RHS_total_len]
    ###### LEFT
    else:
        if fill.find('<')+1:fill = fill[1:]
        fill_len = len(fill)
        if width < str_len: # For any: fill_len
            retval = string[str_len-width:]
        # For all below: width >= str_len
        elif fill_len == 0:
            retval = string
        elif fill_len == 1:
            LHS_to_fill = width - str_len
            retval = fill*LHS_to_fill + string
        elif fill_len  > 1:
            LHS_to_fill = width - str_len
            fill_count = int(LHS_to_fill / fill_len)
            fill_count_rem = LHS_to_fill % fill_len
            retval = fill*fill_count + fill[0:fill_count_rem] +string
        else:
            retval = 'Left_TEST_Left'
    return retval


def super_pad(string, width, fill=" "):
    if width == 0:
        return ''
    diff = width - len(string)
    f = fill
    if diff >= 0:
        if len(f) > 0 and f[0] in ['<', '^', '>']:
            if f[0] == '<':
                f = fill[1:]
                return (f*width)[:width-len(string)] + string
            elif f[0] == '^':
                f = fill[1:]
                a = (width-len(string))//2
                b = width-len(string)-a
                return (f*width)[:b] + string + (f*width)[:a]
            elif f[0] == '>':
                f = fill[1:]
                return string + (f*width)[:width-len(string)]
        else:
            return (f*width)[:width-len(string)] + string
    else:
        if f[0] == '<':
            f = fill[1:]
            return string[len(string)-width:]
        elif f[0] == '^':
            f = fill[1:]
            a = abs(width-len(string))//2
            b = abs(width-len(string))-a
            return string[:a] + string[:-b]
        elif f[0] == '>':
            f = fill[1:]
            return string[:width-len(string)]
        else:
            return string[len(string)-width:]

def super_pad(string, width, fill=" "):
    if fill == '':
        return string
    if width == 0:
        return ''
    diff = width-len(string)
    if diff <= 0:
        if fill[0] == '>':
            return string[:width]
        elif fill[0] == '^':
            a = abs(diff)//2
            b = abs(diff)-a
            if a == 0 and diff == -1:
                return string[:-1]
            return string[b:] + string[:-a]
        elif fill[0] == '<':
            fill = fill[1:]
        return string[-width:]
    else:
        if fill[0] == '>':
            fill = fill[1:]
            if len(fill) > diff:
                return string + fill[:diff]
            else:
                ans, i = '', 0
                while i < diff:
                    ans += fill
                    if len(ans) >= diff:
                        ans = ans[:diff]
                        break
                    i += 1
                return string + ans        
        elif fill[0] == '^':
            fill = fill[1:]
            a = diff//2
            b = diff-a
            if len(fill) > diff:
                return fill[:b] + string + fill[:a]
            else:
                ans1, i, ans2 = '', 0, ''
                while i < b:
                    ans1 += fill
                    ans2 += fill
                    if len(ans2) >= diff//2 :
                        ans1 = ans1[:a]
                        if len(ans1) >= diff-len(ans2):
                            ans2 = ans2[:b]
                            break
                    i += 1
                return ans2 + string + ans1
        elif fill[0] == '<':
            fill = fill[1:]
            if len(fill) > diff:
                return fill[:diff] + string
            else:
                ans, i = '', 0
                while i < diff:
                    ans += fill
                    if len(ans) >= diff:
                        ans = ans[:diff]
                        break
                    i += 1
                return ans + string
        else:
            if len(fill) > diff:
                return fill[:diff] + string
            else:
                ans, i = '', 0
                while i < diff:
                    ans += fill
                    if len(ans) >= diff:
                        ans = ans[:diff]
                        break
                    i += 1
                return ans + string

def super_pad(string, width, fill=" "):
    if not width:
        return ''
    if not fill:
        return string
    direction = fill[0]
    if direction in "<^>":
        fill = fill[1:]
    else:
        direction = "<"
    
    if direction == "<":
        return ((fill*width)[:max(0,width-len(string))]+string)[-width:]
    elif direction == ">":
        return (string+fill*width)[:width]
    else:
        left  = (width - len(string) + 1) // 2
        right = (width - len(string)    ) // 2
        if right>=0:
            left_padding  = (fill*width)[:left]
            right_padding = (fill*width)[:right]
            return left_padding + string + right_padding
        else:
            return string[-left:right]
        
        
        
  
    return string

from itertools import chain, islice, cycle

padding = lambda fill, size: islice(cycle(fill), size)

def super_pad(string, width, fill=" "):
    if not width: return ''
    if not fill: return string[:width]
    size = width - len(string)
    if size <= 0: return string[:width] if fill[0] in '>^' else string[-width:]
    if fill[0] == '>': return string + ''.join(padding(fill[1:], size))
    if fill[0] == '^': return ''.join(padding(fill[1:], (size + 1) >> 1)) + string + ''.join(padding(fill[1:], size >> 1))
    if fill[0] == '<': fill = fill[1:]
    return ''.join(padding(fill, size)) + string