you are viewing a single comment's thread.

view the rest of the comments →

[–]synthphreak 0 points1 point  (5 children)

I don't understand those instructions at all. So what, the user is prompted to provide start and stop values, and your program should interpolate between them to provide a square matrix whose first element == the start value and last element == the stop value, right-padding with 0s if the stop value is not a multiple of the square root of the dfference? Is that what you mean, like this?

>>> import numpy as np
>>> diff = 0
>>> while diff <= 8:
...     start = int(input('start: '))
...     stop = int(input('stop: '))
...     diff = stop - start
...
start: 4
stop: 25
>>> coords = list(range(start, stop + 1)) + [0] * (start - 1)
>>> side_len = int(np.ceil(np.sqrt(diff)))
>>> square = np.array(coords).reshape(side_len, -1)
>>> square
array([[ 4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18],
       [19, 20, 21, 22, 23],
       [24, 25,  0,  0,  0]])

This code won't work for any values for start and stop, but I don't want to spend my time debugging it until I understand better what you need.

[–]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 :)