【每日一题20220329】字母方阵

:man_mage: 我们今天的任务是,编写一个函数,接收一个数字n,返回一个由英文字母组成的 n*n 方阵。

示例:
输入:4
输出:

a b c d
b c d e
c d e f
d e f g

题目难度:简单
题目来源:CodeWar:Alphabetical Grid

def solution(n: int) -> str:
    # your code here

assert solution(0) == ""
assert solution(1) == "a"
assert solution(2) == "a b\nb c"
assert solution(4) == "a b c d\nb c d e\nc d e f\nd e f g"
assert solution(6) == "a b c d e f\nb c d e f g\nc d e f g h\nd e f g h i\ne f g h i j\nf g h i j k"
    def solution(n: int) -> str:
        square = ''
        for i in range(n):
            chr_int = 97 + i
            for j in range(n):
                chr_int += j if j == 0 else 1
                square += chr(chr_int)
                if j < n-1:square += ' '
            if i < n-1:square += '\n'
        return square

def solution(n):
    if n < 0:
        return None
    if n == 0:
         return ''

    #从letter里获取具体的字符数
    letters = [chr(i) for i in range(97, 123)]
    letter_numbers = n + (n-1)
    if letter_numbers // len(letters) !=0:
        letters *= letter_numbers // len(letters)
    if letter_numbers % len(letters) != 0:
        letters .extend(letters[0:letter_numbers % len(letters)])
    get_letters = letters[0:n + (n-1)]

    results = []
    for i in range(n):
        res = list(get_letters[i: n + i])
        _results = ' '.join(res)
        results.append(_results)
    return '\n'.join(results)
def solution(N):
    return '\n'.join(' '.join(chr(k % 26 + 97) for k in range(i, i + N)) for i in range(N)) if N >= 0 else None
# 解法一
def solution(n: int) -> str:
    str_li1 = []
    str_li2 = []
    if n == 0:
        return ""
    else:
        for i in range(1,n+1):
            for j in range(0,n):
                str_li1.append(chr(j+i+96))
            str_1 = " ".join(str_li1)
            str_li2.append(str_1)
            str_li1.clear()  # 将str_li1清空,为下一次遍历做准备
        return "\n".join(str_li2)

# 解法二:嵌套的列表推导式
def solution2(n: int) -> str:
    str_li2 = [" ".join([chr(i+j+96) for i in range(1,n+1)]) for j in range(0,n)]
    return "\n".join(str_li2)

assert solution(0) == ""
assert solution(1) == "a"
assert solution(2) == "a b\nb c"
assert solution(4) == "a b c d\nb c d e\nc d e f\nd e f g"
assert solution(6) == "a b c d e f\nb c d e f g\nc d e f g h\nd e f g h i\ne f g h i j\nf g h i j k"
assert solution2(0) == ""
assert solution2(1) == "a"
assert solution2(2) == "a b\nb c"
assert solution2(4) == "a b c d\nb c d e\nc d e f\nd e f g"
assert solution2(6) == "a b c d e f\nb c d e f g\nc d e f g h\nd e f g h i\ne f g h i j\nf g h i j k"

for x in range(n):
print(’’.join([chr(97+y) for y in range(x,x+n)]))

import string
def solution(n: int) -> str:
    list_letter = string.ascii_lowercase
    return '\n'.join(' '.join(list_letter[i+j] for i in range(n)) for j in range(n))
def grid2(n):
    a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
    for i in range(0, n):
        for j in range(1, n+1):
            if i == 0:
                print(a[j-1], end=" ")  #end使该函数关闭“在输出中自动包含换行”的默认行为
            else:
                row = (i + j) % n
                if row == 0:
                    print(a[n-1], end=" ")
                else:
                    print(a[row-1], end=" ")
        print()
def solution(n: int) -> str:                                                                        
    j = 97                                                                                          
    result = []                                                                                     
    for i in range(n):                                                                              
        line = []                                                                                   
        for j in range(97 + i, 97 + n + i):                                                         
            line.append(chr(j))                                                                     
        result.append(' '.join(line))                                                               
    return '\n'.join(result)                                                                        
                                                                                                    
                                                                                                    
assert solution(0) == ""                                                                            
assert solution(1) == "a"                                                                           
assert solution(2) == "a b\nb c"                                                                    
assert solution(4) == "a b c d\nb c d e\nc d e f\nd e f g"                                          
assert solution(6) == "a b c d e f\nb c d e f g\nc d e f g h\nd e f g h i\ne f g h i j\nf g h i j k"
                                                                                                    

集思广益

def solution2(n):
    alpha = [chr(x) for x in range(ord('a'), ord('z') + 1)]

    return '\n'.join([' '.join(alpha[i:i + n]) for i in range(n)])
def solution(n: int) -> str:
    alph = list('abcdefghijklmnopqrstuvwsyz')
    #l1=[]
    #for i in range(n):
        #l1.append(alph[i:n+i])
    #return  '\n'.join([' '.join(i) for i in l1])
    return  '\n'.join([' '.join(alph[i:n+i]) for i in range(n)])
def solution(n: int) -> str:
    res_list = []
    for i in range(n):
        line_list = []
        for j in range(n):
            line_list.append(chr(97 + i + j))
        res_list.append(' '.join(line_list))
    return '\n'.join(res_list)


assert solution(0) == ""
assert solution(1) == "a"
assert solution(2) == "a b\nb c"
assert solution(4) == "a b c d\nb c d e\nc d e f\nd e f g"
assert solution(6) == "a b c d e f\nb c d e f g\nc d e f g h\nd e f g h i\ne f g h i j\nf g h i j k"