柠檬班学员面试遇到的笔试题!!!
出招吧各位大佬!
柠檬班学员面试遇到的笔试题!!!
出招吧各位大佬!
# 镜像星星金字塔
def mirror_star_pyramid(n):
i = 1# 给定初始值
star_list1 = []# 存正金字塔每一层元素
star_list2 = []# 存倒金字塔每一层元素
while i <= n:
star_list1.append(' '*((n-i)//2) + '*'*i + ' '*((n-i)//2))
# n的奇偶性将决定倒金字塔的元素是跟正金字塔一样还是比正金字塔少一层
if n % 2:
star_list2.append(' '*((i+1)//2) + '*'*(n-i-1) + ' '*((i+1)//2))
else:
star_list2 = star_list1[::-1]
i += 2
for each in star_list1:print(each)
for each in star_list2:print(each)
if __name__ == '__main__':
mirror_star_pyramid(5)
mirror_star_pyramid(6)
代码付上,任意整数的阵列,还在公司不能电脑截图
def wan(k):
m=1
n=int((k+1)/2)
for i in range(1,k+1):
if i<=(k+1)/2:
n-=1
print(' '*n+'*'*m)
if i<(k+1)/2:
m+=2
else:
if k%2==0:
m-=2
print(' '*n+'*'*m)
n+=1
else:
n+=1
m-=2
print(' '*n+'*'*m)
wan(6)
# -*- coding: UTF-8 -*-
# @Author: Sai_Python12
# @Email: 932934045@qq.com
# @File: StarStar.py
class MyStar:
'''星星类'''
def __init__(self, n):
self.n = n
self.data_1 = [] #存放上半部左侧空格数量
self.data_2 = [] #存放上半部星号数量
def my_star(self):
K = int((self.n+1)/2) #K 为上部一半
for i in range(1, K+1):#i 为行数
self.data_1.append(K-i)
self.data_2.append(2*i-1)
print(' '*self.data_1[i-1] + '*'*self.data_2[i-1])
if self.n % 2 == 0:
for i in range(1, K+1):
print(' '*self.data_1[-i] + '*'*self.data_2[-i])
else:
for i in range(1, K):
print(' '*self.data_1[-i-1] + '*'*self.data_2[-i-1])
print(self.data_1)
print(self.data_2)
if __name__ == '__main__':
MyStar(6).my_star()
欢迎来到testingpai.com!
注册 关于