r/PythonLearning 5d 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

4

u/Junior_Honey_1406 5d ago

Use loop to print the table

1

u/ArtisticFox8 4d ago

And a 2D list

4

u/P1ckl3R1ck101 5d 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 5d 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 5d ago edited 5d 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 4d 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.

5

u/JGB25 5d ago

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

1

u/No_Complex_18 1d ago

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

1

u/JGB25 1d 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 1d 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 1d ago

Thank you for explaining it

3

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

1

u/[deleted] 5d ago

[deleted]

2

u/Fair-Elevator6788 3d 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 5d ago

Probably print(*a)

1

u/notsaneatall_ 5d 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 5d 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 5d ago edited 5d 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/Heavy_Fall6883 4d ago

 print(" ".join(map(str,a)))
change array in map function

1

u/SaltCusp 4d ago

Yeah apparently these answers aren't loved.

-2

u/SaltCusp 5d ago

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

1

u/P1ckl3R1ck101 5d 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 4d ago edited 4d ago

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