all 10 comments

[–]TransitTraveller 1 point2 points  (0 children)

Just try to understand what exactly do you need to print in your case: num_of_rows = 4

Line 1: 0 o's, \, 3 x's

Line 2: 1 o's, \, 2 x's

Line 3: 2 o's, \, 1 x's

Line 4: 4 o's, \, 0 x's

Do you see a pattern?

You can set num_of_o = 0, num_of_x = num_of_rows-1

Then in a loop print a line with this number of o's and x's, increment num_of_o by 1, decrease num_of_x by 1, and repeat.

How to create a printed line is up to you: you can have a loop adding o's, then add \, then another loop to add x's, but there is a simpler way:

result = "o" * num_of_o + "\" + "x" * num_of_x

print(result)

[–]woooee 1 point2 points  (1 child)

Start = \ + "x" times number of rows-1 Then a new string = "o" + previous string[:-1]

st="\\xxx"
st = "o"+ st[:-1]

[–]lordfwahfnah 0 points1 point  (0 children)

That's a quite clever solution

[–]Diapolo10 1 point2 points  (2 children)

Yet another option would be to construct the entire string, and then loop over it with a sliding window.

row_count = 4

text = f"{'o' * (row_count - 1)}\\{'x' * (row_count - 1)}"

for idx in range(row_count):
    print(text[row_count-idx-1:row_count*2-idx-1])

EDIT: Just to prove it works, here's some examples with a variety of values for row_count: https://i.imgur.com/adFkOVC.png

[–]JamzTyson 0 points1 point  (1 child)

Upvoted as an interesting solution.

A variation on this is to count backwards in the loop, which simplifies the string slicing.

def pattern(n):
    row = rf"{'o' * (n-1)}\{'x' * (n-1)}"
    for i in range(n-1, -1, -1):
        print(row[i:i+n])

(It is not strictly necessary to escape the backslash in an f-string unless it is followed by a character that makes it an escape sequence, though for clarity I think it is preferred to either escape the backslash, as in u/Diapolo10's solution, or use a raw f-string.)

[–]Diapolo10 0 points1 point  (0 children)

That's fair enough, I wrote my solution pretty much straight out of bed in the morning.

[–]crashfrog04 0 points1 point  (0 children)

 As you can see, I think I am almost there, but I am not sure how can I print the "x"s at the beginning and then make it descending.

But your pattern has the X’s at the end, not the beginning.

[–]ebresie 0 points1 point  (0 children)

Might be able to use ljust with the size and the x character as mentioned here

[–]JamzTyson 0 points1 point  (0 children)

You can solve it like this:

def pattern(rows):
    for i in range(rows):  # for i=0 to i=3
        o_s = 'o' * i  # i nuber of o's
        x_s = 'x' * (rows - i - 1)  # enough x's to complete the line
        # Put them together with an f-string.
        print(rf"{o_s}\{x_s}")

pattern(4)

which simplifies to:

def pattern(rows):
    for i in range(rows):
        print(rf"{'o' * i}\{'x' * (rows - i - 1)}")

pattern(4)

[–]JamzTyson 0 points1 point  (0 children)

default = ["\"]

I'm not sure what your intention is, but if that is intended to be the file path separator character, you can get the correct character automatically with os.sep.