Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given Input 3 4 6 7 5 3 I need to print like this
            /\
           /  \  
  /\      /    \        /\
 /  \    /      \      /  \
/    \  /        \    /    \
      \/          \  /
                   \/


What I have tried:

I have tried all the possibilities kindly help
Python
def triangle(*args):
    main=[]
    for num in range(len(args)):
        k = args[num] - 1
        patt = []
        for i in range(0, args[num]):
            hello = ''
            for j in range(0, k):
                hello = hello + " "
            if num%2==0:
                k = k - 1
                hello = hello+"/"
            else:
                k = k+ 1
                hello = hello + "\\"
            patt.append(hello)
        main.append(patt)
    return main
lists = triangle(3,4,3)

Confused on how to print
Posted
Updated 4-Aug-21 21:20pm
v3
Comments
CHill60 4-Aug-21 7:24am    
As you have tried "all the possibilities" then you must have tried a version that worked. Sarcasm aside - share the code you tried and explain what went wrong
Richard MacCutchan 4-Aug-21 7:36am    
No, we do not offer personal tuition. Please make an effort to do your own work. If you still cannot get it to do what you want then show your code and people will try to help you.
Ghouse Syed 4-Aug-21 7:40am    
def triangle(*args):
main=[]
for num in range(len(args)):
k = args[num] - 1
patt = []
for i in range(0, args[num]):
hello = ''
for j in range(0, k):
hello = hello + " "
if num%2==0:
k = k - 1
hello = hello+"/"
else:
k = k+ 1
hello = hello + "\\"
patt.append(hello)
main.append(patt)
return main
lists = triangle(3,4,3)

Confused on how to print

To simplify your task, allocate a bidimensional matrix and assign the corresponding characters inside it. Then print the matrix to the console.
 
Share this answer
 
Comments
Richard MacCutchan 4-Aug-21 9:27am    
+5, neat idea.
CPallini 4-Aug-21 9:28am    
Thank you, Richard.
Patrice T 4-Aug-21 16:17pm    
+5 KISS principle.
CPallini 5-Aug-21 1:40am    
Thank you.
Maciej Los 5-Aug-21 4:05am    
The shortest instruction i've ever seen ;)
5ed!
Quote:
Confused on how to print

Indeed, and I don't see how you can get correct print from the lists you built.
Look at output :
            /\
           /  \  
  /\      /    \        /\
 /  \    /      \      /  \
/    \  /        \    /    \
      \/          \  /
                   \/

The output fits in a rectangle :
- so first you need to compute number of rows and columns.
- then the position of each lines (rows and columns).
Then you have 3 approaches:
- you have a 2D terminal with cursor positioning : you draw lines directly.
- you built a list of strings and built the strings as you draw the lines in strings, then print each resulting strings.
- you print directly each row as you build them on fly.

First choose a method you are comfortable with.
Built a toy program that draw only the first line.
Then extend the toy program to draw the first 2 lines.
then extend for all lines.

There is no fiw for your code because you are nowhere near a solution.
 
Share this answer
 
Comments
CPallini 5-Aug-21 1:40am    
And my 5 for the complete solution.
Patrice T 5-Aug-21 1:41am    
Thank you.
not really complete solution, just leads.
Thank yo all for your answers. After digging deep into patterns printing python i finally found my solution .

Python
# __author__ = "syedgit96@gmail.com"
# __description__ = "generates a graph by getting input from the user without using libraries"


class Graph:
    def __init__(self, array):
        self.array = array
        self.rows = []
        self.background = ' '
        self.symbol = ("/", "\\")

    def height(self):
        top = []
        temp = 0
        for val in range(len(self.array)):
            if val % 2 == 0:
                temp = temp + self.array[val]
                top.append(temp)
            else:
                temp = temp - self.array[val]
        return max(top)

    def graph_gen(self):
        self.rows = [self.background * sum(self.array)] * self.height()
        last_row = 1
        last_position = 0
        val = 0
        for num in range(len(self.array)):
            if num % 2 == 0:
                for val in range(self.array[num]):
                    # Updating the symbol to the rows
                    temp = list(self.rows[-last_row-val])
                    temp[last_position+val] = self.symbol[0]
                    self.rows[-last_row - val] = "".join(temp)
                last_row = last_row+val
                last_position = last_position+val
            else:
                for val in range(self.array[num]):
                    # Updating the symbol to the rows
                    temp = list(self.rows[-last_row + val])
                    temp[last_position + val+1] = self.symbol[1]
                    self.rows[-last_row + val] = "".join(temp)
                last_row = -(-last_row+val)
                last_position = last_position+val+2
        return '\n'.join(self.rows)

    def change_bg(self, bg):
        self.background = bg

    def change_symbol(self, up, down):
        self.symbol = (up, down)
        self.background = " "


if __name__ == "__main__":
    inputs = list(map(int, input("Input : ").strip().split()))
    eg = Graph(inputs)
    print("Output : ")
    print(eg.graph_gen())
 
Share this answer
 
Comments
Maciej Los 5-Aug-21 4:07am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900