for loop with recursion failing - loops

So I have been trying to make a function that gives me random co-ordinates (RAND_X, RAND_Y), then checks them against a list of previous co-ordinates (history_x, history_y), using a for loop, to make sure I haven't used them before and then update them into a list.
def get_rand(x, y, snake_length, history_x, history_y, RANDOM):
RAND_X = random.randrange(0, x)
RAND_Y = random.randrange(0, y)
i = 0
for i in range(len(snake_length)):
if RAND_X == history_x[i] and RAND_Y == history_y[i]:
get_rand(x, y, snake_length, history_x, history_y, RANDOM)
RANDOM.insert(0, RAND_Y)
RANDOM.insert(0, RAND_X)
Everything seems to go well, until I hit the recursion event. The recursion event itself happens smoothly, but then the next time I call this function the "i" value in the for-loop doesn't start from 0 and skips a couple of steps, as you can see from these terminal prints:
i: 0
RAND_X: 4
history_x: 9
RAND_Y: 4
history_y: 6
i: 1
RAND_X: 4
history_x: 9
RAND_Y: 4
history_y: 5
i: 2
RAND_X: 4
history_x: 9
RAND_Y: 4
history_y: 4
i: 3
RAND_X: 4
history_x: 8
RAND_Y: 4
history_y: 4
i: 4
RAND_X: 4
history_x: 7
RAND_Y: 4
history_y: 4
i: 5
RAND_X: 4
history_x: 6
RAND_Y: 4
history_y: 4
i: 6
RAND_X: 4
history_x: 5
RAND_Y: 4
history_y: 4
i: 7
RAND_X: 4
history_x: 4
RAND_Y: 4
history_y: 4
i: 0
RAND_X: 6
history_x: 9
RAND_Y: 2
history_y: 6
i: 1
RAND_X: 6
history_x: 9
RAND_Y: 2
history_y: 5
i: 2
RAND_X: 6
history_x: 9
RAND_Y: 2
history_y: 4
i: 3
RAND_X: 6
history_x: 8
RAND_Y: 2
history_y: 4
i: 4
RAND_X: 6
history_x: 7
RAND_Y: 2
history_y: 4
i: 5
RAND_X: 6
history_x: 6
RAND_Y: 2
history_y: 4
i: 6
RAND_X: 6
history_x: 5
RAND_Y: 2
history_y: 4
i: 7
RAND_X: 6
history_x: 4
RAND_Y: 2
history_y: 4
i: 8
RAND_X: 6
history_x: 3
RAND_Y: 2
history_y: 4
i: 9
RAND_X: 6
history_x: 3
RAND_Y: 2
history_y: 5
i: 10
RAND_X: 6
history_x: 3
RAND_Y: 2
history_y: 6
i: 11
RAND_X: 6
history_x: 4
RAND_Y: 2
history_y: 6
RANDOM: [6, 2, 9, 6, 5, 6, 7, 9, 3, 2, 8, 0, 3, 8, 6, 6, 7, 0, 8, 8, 2, 0, 7, 6, 5, 3]
i: 8
RAND_X: 4
history_x: 3
RAND_Y: 4
history_y: 4
i: 9
RAND_X: 4
history_x: 3
RAND_Y: 4
history_y: 5
i: 10
RAND_X: 4
history_x: 3
RAND_Y: 4
history_y: 6
i: 11
RAND_X: 4
history_x: 4
RAND_Y: 4
history_y: 6
RANDOM: [4, 4, 6, 2, 9, 6, 5, 6, 7, 9, 3, 2, 8, 0, 3, 8, 6, 6, 7, 0, 8, 8, 2, 0, 7, 6, 5, 3]
You can see that the value of "i" increases, until it finds a match and then starts at 0 again with a new pair of random numbers. It then goes over all the iterations and adds them to the list. However, the next time I call the function, "i" starts at 8, instead of 0. Also, it only goes up to 11 again, even though it now should go up to 12.
I hope I have explained the problem clear enough and one of you knows what I did wrong here. Pretty new to all of this.

Related

Julia: join two matrices using the same memory

I want to fuse two arrays without using more memory, it's posible?, for instance:
a=[1 2 3
4 5 6
7 8 9]
b=[11 12 13
14 15 16
17 18 19]
I need to get the array:
c=[a b]
but using the same memory as a and b, i.e, any change in a or b must be reflected in c.
There's also another package CatViews.jl
julia> x = CatView(a, b); # no copying!!!
julia> reshape(x, size(a, 1), :)
3×6 reshape(::CatView{2,Int64}, 3, 6) with eltype Int64:
1 2 3 11 12 13
4 5 6 14 15 16
7 8 9 17 18 19
If you start in reverse, define C first
julia> C = rand(0:9, 3, 6)
3×6 Array{Int64,2}:
3 2 4 4 9 8
8 8 6 5 5 9
0 7 5 8 7 5
then have A and B be views of C
julia> A = #view C[:, 1:3]
3×3 view(::Array{Int64,2}, :, 1:3) with eltype Int64:
3 2 4
8 8 6
0 7 5
julia> B = #view C[:, 4:6]
3×3 view(::Array{Int64,2}, :, 4:6) with eltype Int64:
4 9 8
5 5 9
8 7 5
then it works.
julia> A[2,2] = -1
-1
julia> C
3×6 Array{Int64,2}:
3 2 4 4 9 8
8 -1 6 5 5 9
0 7 5 8 7 5

How to access adjacent elements from a current starting position?

I have a matrix:
E = [ 3 6 3 7 2 ; 3 8 3 7 2; 1 9 5 4 1; 7 5 1 6 9; 10 8 4 3 10 ]
If I started at E(3,3) = 5, how do I access the adjacent numbers to the east (or west). For example, I wanted to access the 3 elements to the east of E(3,3), which are 7, 4 and 6.
% define matrix and point
E = [ 3 6 3 7 2 ; 3 8 3 7 2; 1 9 5 4 1; 7 5 1 6 9; 10 8 4 3 10 ];
Raw=3;
Col=3;
% find the value of the point, 3 east values and 3 west values
Point=E(Raw,Col)
EastVal=E(max(1,Raw-1):min(size(E,1),Raw+1),Col+1)
WestVal=E(max(1,Raw-1):min(size(E,1),Raw+1),Col-1)
Produce:
Point =
5
EastVal =
7
4
6
WestVal =
8
9
5

How to do print formatting in Python with chunks of strings?

I'm having some trouble with formatting the pyramid. I've tried to use format when printing from the loop but that didn't seem to work and just breaks the program. What would be different ways to format the output. The only trouble that I am having is when I am printing 10 and up when there's double digits. What would be the best approach formatting the printing output? I've tried variety of ways but couldn't make formatting work within the loop from documentation
https://docs.python.org/3.5/library/string.html#formatstrings
Here is the script:
userinput = int(input("Enter the number of lines: " )) # User input of the total number of lines
userinput = userinput + 1 # adding a value of 1 additionally with the user input to make numbers even
for i in range(1, userinput): # Loop through lines from 1 to userinput
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(" ", end = " ")
for j in range(i, 0, -1): # printing number decreasing from the line number j to 1
print(j, end = " ")
for j in range(2,i + 1): # Printing number increasing from 2 to line number j
print(j, end = " ")
print()
j += 1
The output when its less than 10
Enter the number of lines: 9
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
The output when it's 15 or more:
Enter the number of lines: 15
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
When I have reserved an extra space for 10 and up, here is what my outout looks like: (The dots were used to distinguish from empty space, all I did was added a " " quotes in the beginning of the print.
Enter the number of lines: 12
. . . . . . . . . . . . 1
. . . . . . . . . . . 2 1 2
. . . . . . . . . . 3 2 1 2 3
. . . . . . . . . 4 3 2 1 2 3 4
. . . . . . . . 5 4 3 2 1 2 3 4 5
. . . . . . . 6 5 4 3 2 1 2 3 4 5 6
. . . . . . 7 6 5 4 3 2 1 2 3 4 5 6 7
. . . . . 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
. . . . 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
. . . 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
. . 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
. 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
Here is what I've tried changing by adding aditional space
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(".", end = " ")
for j in range(i, 0, -1): # printing number decreasing from the line number j to 1
print(" ", j, end = "")
for j in range(2,i + 1): # Printing number increasing from 2 to line number j
print(" ", j, end = "")
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(" ", end = " ")
Here is the ideal output of what I am trying to accomplish:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Thank you!
The things to consider for this problem are
The length of the largest number.
The length of the current number being printed.
The difference in lengths.
In order to correctly space everything, you're going to need to print extra
spaces after the numbers with less digits (to compensate for the extra digits in the larger number).
For example, if you have a row that contains the number 10, in order to correctly space the other smaller numbers, you're going to need to use extra spaces to compensate for that second digit in the number 10.
This solution works for me.
userinput = int(input("Enter the number of lines: " ))
userinput = userinput + 1
# Here, you can see I am storing the length of the largest number
input_length = len(str(userinput))
for i in range(1, userinput):
# First the row is positioned as needed with the correct number of spaces
spaces = " " * input_length
for j in range(userinput - i):
print(spaces, end = " ")
for j in range(i, 0, -1):
# Now, the current numbers length is compared to the
# largest number's length, and the appropriate number
# of spaces are appended after the number.
spaces = " " * (input_length + 1 - len(str(j)))
print(j, end = spaces)
for j in range(2,i + 1):
# The same is done here as in the previous loop.
spaces = " " * (input_length + 1 - len(str(j)))
print(j, end = spaces)
print()
j += 1
Take a look at
https://stackoverflow.com/a/13077777/6510412
I think this might be what you're looking for. I hope it helps.

How do I accept info from a file and input it into an array?

I have a file with these values:
9 10 11 9.5 10.2 9.8 10 8.7 9.3 9.1 9 8.9
4 5 5 4 6 5 4 3 4 5 5 4
8 11 10 8.5 10.7 9 11 8.5 9.4 9 10 9.9
5 5 5 5 5 5 5 5 5 5 5 5
9 9 9 9 9 9 9 9 9 9 9 9
4 5 5 4 6 5 4 3 4 5 5 4
8 9 10 8.5 9.2 8.8 9 7.7 8.3 8.1 8 7.9
7 4 6 6 6 5 6 6 6 6 5 6
10 11 12 10 11.2 9 11 9.7 9 9.1 10 9.9
6 5 6 6 6 5 6 6 6 6 5 6
10 9 11.2 9.5 10.8 9 10.3 8 9.3 9.1 9 8.9
5 4 5 4 6 5 4 3 4 5 5 4
9 10 10.4 8.5 10.2 9.2 11.1 8 9.4 9 10 9.9
6 4 5 5 5 5 5 5 5 5 5 5
10 8 9.6 9 9.6 9.7 9 9 9.7 8 10 9
5 4 5 4 6 5 4 3 4 5 5 4
9 8 10.8 8.5 9.5 8 9.5 7 8.3 8.1 8.8 9
5 4 5 4 6 5 4 3 4 5 5 4
11 10 11 10 11.3 9.4 11 9 9.5 8 10 7
4 5 5 4 6 5 4 3 4 5 5 4
I need to accept these values into an array, so I can then get the largest number out of every other line, and the lowest out of the other lines.
Say I make an array
float array[20][12];
This doesn't seem to work:
fscanf(ifp, "%f", &main_array[20][12]);
So how can I accept these values into my array?
you need to read value by value, using a loop, you could make something like this:
float array[20][12];
int n = 20 * 12;
int i;
for(i = 0; i < n; i++)
fscanf(ifp, "%f", (&array[0][0] + i));
That's because you cannot read all file at once.
Another way, and maybe more easy to understand(because not uses pointer arithmetic) is:
int i, j;
for(i = 0; i < 20; i++)
for(j = 0; j < 12; j++)
fscanf(ifp, "%f", &array[i][j]);

Tacit function to multiply five consecutive number in a list: J, j701

I'm working on Project Euler, I'm on problem 8, and I'm trying a simple brute force: Multiply each consecutive 5 digit of the number, make a list with the results, and find the higher.
This is the code I'm currently trying to write in J:
n =: 731671765313x
NB. 'n' will be the complete 1000-digits number
itl =: (".#;"0#":)
NB. 'itl' transform an integer in a list of his digit
N =: itl n
NB. just for short writing
takeFive =: 5 {. ] }.~ 1 -~ [
NB. this is a dyad, I get this code thanks to '13 : '5{.(x-1)}.y'
NB. that take a starting index and it's applied to a list
How I can use takeFive for all the index of N?
I tried:
(i.#N) takeFive N
|length error: takeFive
| (i.#N) takeFive N
but it doesn't work and I don't know why.
Thank you all.
1. The reason that (i.#N) takeFive N is not working is that you are essentially trying to run 5{. ((i.#N)-1) }. Nbut you have to use x not as a list but as an atom. You can do that by setting the appropriate left-right rank " of the verb:
(i.#N) (takeFive"0 _) N
7 3 1 6 7
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3
5 3 1 3 0
3 1 3 0 0
1 3 0 0 0
2. One other way is to bind (&) your list (N) to takeFive and then run the binded-verb through every i.#N. To do this, it's better to use the reverse version of takeFive: takeFive~:
((N&(takeFive~))"0) i.#N
7 3 1 6 7
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3
5 3 1 3 0
3 1 3 0 0
1 3 0 0 0
or (N&(takeFive~)) each i.#N.
3. I think, though, that the infix dyad \ might serve you better:
5 >\N
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3

Resources