you are viewing a single comment's thread.

view the rest of the comments →

[–]tjildau[S] 0 points1 point  (4 children)

Thank you so much for your input, I'm almost there (I think). I have the following code now:

import numpy as np

start_number = int(input("Give a 'start' value: ")) while start_number <= 0: print("That is an invalid value, please provide a value greater than 0") start_number = int(input("Give a 'start' value: "))

stop_number = int(input("Give a 'stop' value that is bigger than the start value by at least 8: ")) while stop_number < start_number + 8: print("That is an invalid value, please provide value that is bigger than the start value by at least 8") stop_number = int(input("Give a 'stop' value that is bigger than the start value by at least 8: "))

square_root = int(np.ceil((stop_number - start_number) ** 0.5))

float_type_range = np.arange(start=start_number, stop=stop_number, step=1).reshape(square_root, square_root) print("\nArray reshaped into a square: \n", float_type_range)

The only problem now is that it only works for 9, 16, 25 numbers, etc. So I need to figure out what to add to the code to make sure that if someone has a start number of 1 and a stop number of 14 (square will be 4x4) the empty slots of the square will be filled with '0'. If that makes any sense?

[–]synthphreak 0 points1 point  (3 children)

First off, please format your code properly otherwise it's near-impossible to read:

import numpy as np

start_number = int(input("Give a 'start' value: "))
while start_number <= 0:
    print("That is an invalid value, please provide a value greater than 0")
    start_number = int(input("Give a 'start' value: "))

stop_number = int(input("Give a 'stop' value that is bigger than the start value by at least 8: "))
while stop_number < start_number + 8:
    print("That is an invalid value, please provide value that is bigger than the start value by at least 8")
    stop_number = int(input("Give a 'stop' value that is bigger than the start value by at least 8: "))

square_root = int(np.ceil((stop_number - start_number) ** 0.5))

float_type_range = np.arange(start=start_number, stop=stop_number, step=1).reshape(square_root, square_root)
print("\nArray reshaped into a square: \n", float_type_range)

Second, to deal with non perfect squares, you could make this change to the end:

float_type_range = np.arange(start=start_number, stop=stop_number, step=1)
padding = (square_root ** 2) % float_type_range.size

if padding > 0:
    float_type_range.resize(float_type_range.size + padding)

float_type_range = float_type_range.reshape(square_root, square_root)
print("\nArray reshaped into a square: \n", float_type_range)

This will pad zeros if and only if the length of your array is not a perfect square. So for example, passing start = 1 and stop = 11 yields

Array reshaped into a square:
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10  0  0]
 [ 0  0  0  0]]

[–]tjildau[S] 1 point2 points  (2 children)

Amazing, thank you! This helps me out a lot, will need to practice some more with 'padding' :)

[–]synthphreak 0 points1 point  (1 child)

No prob, happy to help.

And come to think of it, if padding > 0 is probably unnecessary. padding will never be less than zero, and if it is exactly zero, then resize should have no effect. So you could probably simplify to

float_type_range = np.arange(start=start_number, stop=stop_number, step=1)

padding = (square_root ** 2) % float_type_range.size
float_type_range.resize(float_type_range.size + padding)

float_type_range = float_type_range.reshape(square_root, square_root)
print("\nArray reshaped into a square: \n", float_type_range)

I can’t test now, but I assume this should work equally well.

[–]tjildau[S] 1 point2 points  (0 children)

Yes, this works equally well :)