< PREVIOUS KATA
NEXT KATA >
 
###Task:

You have to write a function `pattern` which returns the following Pattern(See Examples) upto (3n-2) rows, where n is parameter.

* Note:`Returning` the pattern is not the same as `Printing` the pattern.

####Rules/Note:
* The pattern should be created using only unit digits.
* If `n < 1` then it should return "" i.e. empty string.
* `The length of each line is same`, and is equal to the length of longest line in the pattern i.e. `length = (3n-2)`.
* Range of Parameters (for the sake of CW Compiler) :
  + `n ∈ (-∞,50]`

###Examples:

  + pattern(5) :

            11111    
            22222    
            33333    
            44444    
        1234555554321
        1234555554321
        1234555554321
        1234555554321
        1234555554321
            44444    
            33333    
            22222    
            11111    
        
  + pattern(11):

                  11111111111          
                  22222222222          
                  33333333333          
                  44444444444          
                  55555555555          
                  66666666666          
                  77777777777          
                  88888888888          
                  99999999999          
                  00000000000          
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
        1234567890111111111110987654321
                  00000000000          
                  99999999999          
                  88888888888          
                  77777777777          
                  66666666666          
                  55555555555          
                  44444444444          
                  33333333333          
                  22222222222          
                  11111111111          
                  
>>>LIST OF ALL MY KATAS<<<

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code: def pattern(n):
	

Solutions:
def pattern(n):
	top = [(str(i % 10) * n).center(n * 3 - 2) for i in range(1, n)]
	left = ''.join((str(i % 10) for i in range(1, n)))
	middle = left + str(n % 10) * n + left[::-1]
	return '\n'.join(top + [middle] * n + top[::-1])


def f(n):
	w = 3 * n - 2
	for i in range(1, n):
		yield (str(i % 10) * n).center(w)
	line = ''.join((str(i % 10) for i in range(1, n))) + str(n % 10) * n + ''.join((str(i % 10) for i in reversed(range(1, n))))
	for i in range(n):
		yield line
	for i in reversed(range(1, n)):
		yield (str(i % 10) * n).center(w)

def pattern(n):
	return '\n'.join(f(n))


def pattern(n):
	if n == 1:
		return '1'
	if n < 1:
		return ''
	top = '\n'.join([' ' * (n - 1) + str(i % 10) * n + ' ' * (n - 1) for i in range(1, n)])
	result = [top]
	nums = [str(i % 10) for i in range(1, n)]
	result.append('\n'.join([''.join(nums) + str(n % 10) * n + ''.join(nums[::-1])] * n))
	result.append(top[::-1])
	return '\n'.join(result)


def pattern(n):
	return ''.join(((str((k + 1) % 10) * n).center(3 * n - 2) + '\n' for k in range(n - 1))) + (''.join((str((i + 1) % 10) for i in range(n - 1))) + n * str(n % 10) + ''.join((str(j % 10) for j in range(n - 1, 0, -1))) + '\n') * n + '\n'.join(((str(g % 10) * n).center(3 * n - 2) for g in range(n - 1, 0, -1)))


def pattern(n):
	lines = []
	for p in range(1, n):
		lines.append((str(p % 10) * n).center(3 * n - 2))
	for p in range(1, n + 1):
		lines.append(''.join([str(m % 10) for m in range(1, n)] + [str(n % 10) for m in range(n)] + [str(m % 10) for m in range(1, n)[::-1]]))
	for p in range(n - 1, 0, -1):
		lines.append((str(p % 10) * n).center(3 * n - 2))
	return '\n'.join(lines)


def pattern(n):
	h = ''.join((str(i % 10) for i in range(1, n)))
	h = h + str(n % 10) * n + h[::-1]
	v = [(str(i % 10) * n).center(len(h)) for i in range(1, n)]
	return '\n'.join(v + [h] * n + v[::-1])


get_r = lambda y: str(y % 10)

def pattern(n):
	if n < 2:
		return '' if n < 1 else '1'
	(l, r) = (''.join(map(get_r, range(1, n))), ''.join(map(get_r, range(n - 1, 0, -1))))
	top = [' ' * (n - 1) + get_r(x) * n + ' ' * (n - 1) for x in range(1, n)]
	return '\n'.join(top) + '\n' + (l + get_r(n) * n + r + '\n') * n + '\n'.join(list(reversed(top)))


pattern = lambda n: '\n'.join([''.join([' '] * (n - 1) + [str(i % 10)] * n + [' '] * (n - 1)) for i in range(1, n)] + [''.join([str(x % 10) for x in range(1, n)] + [str(n % 10)] * n + [str(x % 10) for x in range(n - 1, 0, -1)]) for i in range(n)] + [''.join([' '] * (n - 1) + [str(i % 10)] * n + [' '] * (n - 1)) for i in range(n - 1, 0, -1)])


def pattern(n):
	s = ''.join((str((i if i < n else n if i < n * 2 else n * 3 - i - 1) % 10) for i in range(1, n * 3 - 1)))
	return '\n'.join(((s[i] * n).center(n * 3 - 2) if i < n - 1 or i >= n * 2 - 1 else s for i in range(n * 3 - 2)))