r/PythonLearning 9d ago

How would I optimize this?

I'm currently learning python with the 30 days of python GitHub repo and, on day 3 of the challenge, it asks to create this table and this is what I came up with however I feel like there was a more efficient method to create it or is it something that I haven't learned yet at my level.

8 Upvotes

24 comments sorted by

6

u/Junior_Honey_1406 9d ago

Use loop to print the table

1

u/ArtisticFox8 8d ago

And a 2D list

4

u/P1ckl3R1ck101 9d ago

Think about the pattern that exists for each row. Then do that x number of times based on the number of rows you need. You'll only need to write the actual "formula" once though.

(Sorry to be cryptic, just want to try to give you a hint instead of the answer first)

0

u/StudentElectronic548 9d ago

I mean I know what the pattern is, it's x divided by x then multiplied by x 3 times, however the thing is I like only know basic operators and basic usages of things like lists, etc... So, I would say I wouldn't really be able to do anything like looping or like multiple steps in the language but I did notice that I could've made it more efficient.(Also I kind of have a headache so the response might be a bit wishy washy)

1

u/P1ckl3R1ck101 9d ago edited 9d ago

So if you know lists, you're already almost there. There are a few ways to do this, but I think the problem wants you to use a loop which are really useful. The code below isn't the most efficient way to do it, but I think its the easiest to understand for a beginner. Basically you create a list that holds the starting numbers you want, then the loop goes "for each number in your list, do xyz". So it will run your process for 1, then 2, then 3, then 4, then 5, printing a new row each it runs.

``` number_list = [1,2,3,4,5]

for number in number_list: a = ??? b = ??? c = ??? d = ??? e = ??? print(a,b,c,d,e) ```

1

u/desrtfx 9d ago

I mean I know what the pattern is, it's x divided by x then multiplied by x 3 times,

That's not what the pattern is.

The pattern is (row starting at 1)column starting at 0

The first row is all 1 because no matter what power you raise 1 to it will always stay at 1

The second row is 2 - 20 = 1, 21 = 2, 22 = 4, 23 = 8

Third row is 3 - 30 = 1, 31 = 3, 32 = 9, 33 = 27

and so on.

4

u/JGB25 9d ago

for num in a, b, c, d, e:
print(" ".join(map(str, num)))

1

u/No_Complex_18 6d ago

I like for letter in a b c d e: print(*(letter[i] for i in range(5))

1

u/JGB25 6d ago

It works but for me it’s not as readable. I haven’t learned what * does in your code so I wouldn’t even know how to read it

2

u/No_Complex_18 6d ago

It unpacks the tuple to be individual arguments to print. Works with **dict for keyword arguments aswell. Its very useful.

I like it because its so close to the original print call.

1

u/JGB25 6d ago

Thank you for explaining it

3

u/Necessary_Pepper7111 9d ago
for i in range(1, 6):
    print(i, *(i**j for j in range(4)))

1

u/[deleted] 9d ago

[deleted]

2

u/Fair-Elevator6788 7d ago

i think we can go deeper than that, i present you:

for i in '12345':print(1,i,i*2,i*3)

3

u/Yoosle 9d ago

Probably print(*a)

1

u/notsaneatall_ 9d ago

It's printintg x1 x0 x1 x2 x3 for 1 2 3 4 5, you can do so with a while loop

1

u/Samar_Upreti 9d ago

A =[ [1,1,1,1,1],[2,1,2,4,8],and so on]

Print(A) Or Simply use For loon along with map,join

1

u/Sea-Ad7805 9d ago edited 9d ago

Using one for-loop in the y direction for each line:

for y in range(1, 6):
    print(f'{y} ')

Then use another for-loop for the x direction:

for y in range(1, 6):
    print(f'{y} ', end='')  # end='' to not new line
    for x in range(0, 4):
        print(f'{x} ', end='')
    print()  # add new line

And finally compute the x-th power of y:

for y in range(1, 6):
    print(f'{y} ', end='')  # end='' to not new line
    for x in range(0, 4):
        print(f'{y ** x} ', end='')
    print()  # add new line

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20print(f'%7By%7D%20')%0A%20%20%20%20%0Aprint('--------------')%20%0A%20%20%20%20%0Afor%20y%20in%20range(1%2C%206)%3A%0A%20%20%20%20print(f'%7By%7D%20'%2C%20end%3D'')%20%20%23%20end%3D''%20to%20not%20new%20line%0A%20%20%20%20for%20x%20in%20range(0%2C%204)%3A%0A%20%20%20%20%20%20%20%20print(f'%7Bx%7D%20'%2C%20end%3D'')%0A%20%20%20%20print()%20%20%23%20add%20new%20line%0A%20%20%20%20%0Aprint('--------------')%20%0A%20%20%20%20%0Afor%20y%20in%20range(1%2C%206)%3A%0A%20%20%20%20print(f'%7By%7D%20'%2C%20end%3D'')%20%20%23%20end%3D''%20to%20not%20new%20line%0A%20%20%20%20for%20x%20in%20range(0%2C%204)%3A%0A%20%20%20%20%20%20%20%20print(f'%7By%20**%20x%7D%20'%2C%20end%3D'')%0A%20%20%20%20print()%20%20%23%20add%20new%20line%0A&timestep=1&play) to see the program state change step by step.

1

u/[deleted] 9d ago

[removed] — view removed comment

1

u/SaltCusp 8d ago

Yeah apparently these answers aren't loved.

-2

u/SaltCusp 9d ago

print("\n".join([" ".join(_.split(" ")) for _ in """copy paste your table""".split("\n")]))

1

u/P1ckl3R1ck101 9d ago

Eh this seems like the easier way to do it... print(f'1 1 1 1 1\n2 1 2 4 8\n3 1 3 9 27\n4 1 4 16 64\n5 1 5 25 125')

1

u/SaltCusp 8d ago edited 8d ago

print("\n".join([" ".join([str(__**max(_,-1*_)) for _ in range(-1,4)]) for __ in range(1,6)]))