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: 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"
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 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"