lundi 24 août 2020

How to print a graph like pattern( ZIG ZAG ) in JAVA with respect to the given integers

I have attached the pic of sample input and output......If you see sample i/o you will get a clear picture

Sample i/o

Else see below....

Input: 3 2 4 3 2 5

Output:

        /\
       /  \  /\
  /\  /    \/  \
 /  \/          \
/                \
                  \

I need full code please...Thanks

2 commentaires:

  1. number_input = [4, 3, 2, 2, 5]
    # default pattern
    pattern = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

    height = 5 # starting row (adaptable)
    rows = 14 # rows of the pattern
    lines = 0 # lines added to the pattern

    def figure(height, lines, nums):
    print(nums)
    for i in range(len(nums)):
    # if i is even it adds '/'
    if i % 2 == 0:
    for j in range(nums[i]):
    pattern[rows-height-1][lines] = '/'
    height += 1 # increase height by 1
    lines += 1 # count the lines added to the pattern
    height -= 1
    # if i is odd it adds '\'
    elif i % 2 == 1:
    for j in range(nums[i]):
    pattern[rows-height-1][lines] = '\\'
    height -= 1 # decrease height by 1
    lines += 1
    height += 1

    figure(height, lines, number_input)

    # replace zeros with spaces
    for line in pattern:
    for i, num in enumerate(line):
    if num == 0:
    line[i] = ' '
    # print the lines
    print(*line)

    RépondreSupprimer