-3

hello guys so i think this is very basic but for some reason i can't get my head around it to make it work. so basically i need to create a function or anything that will allow me to print # in a certain way

so what i want to do basically is for example if i have:

print_line(2,4,20)

output: ##           ##
          ##       ##  ##
            ##   ##      ##
              ##           ##

so the 2 stands for the amount of # next to each others, the 4 is 4 lines and 20 is the amount characters per line

what i tried is to use * for the amount of # and for loop for the lines but i didn't know how to make the last one

M.I
  • 379
  • 3
  • 18
  • 1
    Please post your current code and its output versus the expected output. See the [mre] page. – Random Davis Sep 21 '20 at 17:12
  • @RandomDavis what you see on the screen is the expected output i'll post what i did but its not much – M.I Sep 21 '20 at 17:12
  • 2
    I expect you mean, `20` is the amount characters per line. Otherwise, your example output does not meet your own requirements. – h0r53 Sep 21 '20 at 17:13
  • Where is `20` in your result? – PM 77-1 Sep 21 '20 at 17:14
  • @h0r53 yes exactly sorry for not able to clarify it correctly i''ll update the question but what u said is correct this is what i need and i don't know how to do it – M.I Sep 21 '20 at 17:15
  • @PM77-1 20 is the amount characters per line – M.I Sep 21 '20 at 17:16
  • Even with updated requirements there are many ways to define this function that would be valid based on the requirements but would not produce the output you desire. For example, simply printing `##` once on each line would be valid. If you define the distance between symbols then the definition becomes more concrete – h0r53 Sep 21 '20 at 17:18
  • Post the desired result (complete) based on your input parameters. – PM 77-1 Sep 21 '20 at 17:19

2 Answers2

1

Instead of forming the lines directly, it might be simpler (but still gave me a bit of a headache with all those +1 and -1) to form the columns and then transpose those:

def print_line(repeat, lines, total):
    cols = [s if (i // (lines-1)) % 2 == 0 else s[::-1]
            for i in range(total // repeat)
            for s in ["#".rjust((i % (lines-1))+1).ljust(lines)] * repeat]
    for line in map(''.join, zip(*cols)):
        print(line)

>>> print_line(2,4,20)
##          ##      
  ##      ##  ##    
    ##  ##      ##  
      ##          ##

Breaking this down line by line:

  • for i in range(total // repeat) iterate "blocks"
  • "#".rjust((i % (lines-1))+1).ljust(lines) create column with rjust and ljust and modulo %
  • for s in [...] * repeat repeat those columns as often as needed
  • s if (i // (lines-1)) % 2 == 0 else s[::-1] add the column or the reversed column
  • for line in map(''.join, zip(*cols)) transpose columns to lines and print them
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

Here is something I've tried:


def print_line(x, y, z):
    for row in range(0, y + 1): # row
        reverse = False
        pattern_length = (y*x) - x

        for col in range(-x + 1, z): # column

            if reverse:
                for a in range(0,x):
                    # print("%d-%d "%(col, row), end='')
                    if (((col + a) % pattern_length) == x * (y - row -1)):
                        # print('*** ', end="")
                        print("#", end="")
                    else:
                        # print("%d-%d "%(col, row), end='')
                        print(" ", end="")
            else:
                for a in range(0,x):
                    if ((col + a) % pattern_length== row*x):
                        print('#', end="")
                    else:
                        print(" ", end='')

            if (col + x) % pattern_length == 0:
                # print('|', end="")      # uncomment this line to see the separation line
                reverse = not reverse

        print()

outputs:

>>> print_line(2, 4, 20)
 ##                      ##                 
     ##              ##      ##             
         ##      ##              ##      ## 
             ##                      ##     
>>> print_line(2, 6, 40)
 ##                                      ##                                      ## 
     ##                              ##      ##                              ##     
         ##                      ##              ##                      ##         
             ##              ##                      ##              ##             
                 ##      ##                              ##      ##                 
                     ##                                      ##                     
                                                                                    
>>> print_line(3, 5, 40)
 # # #                                                                   # # #                                                  
           # # #                                                 # # #             # # #                                         
                    # # #                               # # #                               # # #                               #
                             # # #             # # #                                                 # # #             # # #     
                                      # # #                                                                   # # #              
                                                                                                                                

The way it works is that, we first need to divide the board into sections (those we print the forward slash pattern, and those with the backward) - we are using reverse variable to represent that. and then for every cell in the backward slash section we check whether we the row number and row*x number match as well as whether it is to the right of a cell with the # printed in and within x units distance, if so we print # there as well. The forward slash section follows similar logic but we check the column number against x * (y - row -1) instead (the reverse).

Although the output may not be the exact match of your desired output, this may help you get started and get some ideas.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36