Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

How do I write number pyramid in python without using range function?

I have to write a code in python to show the following output:
1
1 2 1
1 2 3 2 1
1 2 1
1
in a rhombus shape. But I can't use range function in the code.

I have written the following code using range:

rows = 4
for i in range(rows+1):
    s=0
    for k in range(rows-i):
        print(end=" ")
    for j in range(i+1):
        s=s+10**(i-j)
    print(s*s)
    print(" ")
for i in range(rows-1,-1,-1):
    s=0
    for k in range(rows-i):
        print(end=" ")
    for j in range(i+1):
        s=s+10**(i-j)
    print(s*s)
    print(" ")

Please help me out on how to write the code without using range function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create your own range or not_range :P

not_range returns a list from a to b

Not using inbuilt range anywhere!

def not_range(a,b):
    if a<b:
        return [a]+not_range(a+1,b)
    elif a>b:
        return [a]+not_range(a-1,b)
    else:
        return []

rows = 4
for i in not_range(0,rows+1):
    s=0
    for k in not_range(0,rows-i):
        print(end=" ")
    for j in not_range(0,i+1):
        s=s+10**(i-j)
    print(s*s)
    print(" ")
for i in not_range(rows-1,-1):
    s=0
    for k in not_range(0,rows-i):
        print(end=" ")
    for j in not_range(0,i+1):
        s=s+10**(i-j)
    print(s*s)
    print(" ")
    1
 
   121
 
  12321
 
 1234321
 
123454321
 
 1234321
 
  12321
 
   121
 
    1
 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...