I am new to python.
May I ask why the sixth outcome is 8 instead of 5? As I learnt from "scope" that the later statement should not be affected by whatever happened in another inner scope, so i+=3 should have no effect on what "i" is going to be printed? Thank you for the help.
for i in range (0,10):
if i==5:
i+=3
print i
outcome:
0
1
2
3
4
8
6
7
8
9
In the code you created a condition that if the i reaches number 5 it will add +3 giving an 8.
the += adds do not replace.
if you expect that change the number 5 with a 3 try:
for i in range (0,10):
if i==5:
i = 3
print i
Related
I have array of integer and I am trying to send this array as a sub block from esp32 to another one.
According this code I get on output like this:
output:
1 2 3 4 5
6 7 8 9 10
11 12 0 0 0
the expected output:
1 2 3 4 5
6 7 8 9 10
11 12
How can I update on esp_now_send to get like the expected output? how can I deal with the last sub block if it is less than 5 numbers?
The code needs to send up to only the available data. To do that the general approach would be to send full sub-blocks until the last sub-block which may be a partial one. That can be determined by simple maths logic to work out how much the current iteration should send based on how much data is left.
The code changes would be:
Change siz to be the real number of entries in the array: siz = sizeof(data)/sizeof(data[0]).
Change rang in the function call to `(ind + rang <= size ? rang : size - ind)``. That is, the size passed to the function call depends on how much data is left.
The original problem is like this:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
The link is here https://oj.leetcode.com/problems/search-in-rotated-sorted-array/
I don't know the meaning of the 'target value' here. Is it the value we want to find or something else? Why it is given to me?
Is it the value we want to find or something else?
Yes, for example, if you have rotated array:
4 5 6 7 0 1 2
and you are given number 6, you should return 2 - the index of 6 in the array (assuming indexes start from 0). If you are given number 8, which doesn't occur in the array - return -1.
i am doing very simple Matrix indexing examples . where code is as give below
>> A=[ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ]
A =
1 2 3 4
5 6 7 8
9 10 11 12
>> A(end, end-2)
ans =
10
>> A(2:end, end:-2:1)
ans =
8 6
12 10
here i am bit confused . when i use A(end, end-2) it takes difference of two till first column and when there is just one column left there is no further processing , but when i use A(2:end, end:-2:1) it takes 6 10 but how does it print 8 12 while there is just one column left and we have to take difference of two from right to left , Pleas someone explain this simple point
The selection A(end, end-2) reads: take elements in last row of A that appear in column 4(end)-2=2.
The selection A(2:end, end:-2:1) similarly reads: take elements in rows 2 to 4(end) and starting from last column going backwards in jumps of two, i.e. 4 then 2.
To check the indexing, simply substitute the end with size(A,1) or size(A,2) if respectively found in the row and col position.
First the general stuff: end is just a placeholder for an index, namely the last position in a given array dimension. For instance, for an arbitrary array A(end,1) will pick the last element in column 1, and A(1,end) will pick the last element in the first row.
In your example, A(end, end-2) picks an element in the last row two columns before the last one.
To interpret a statement such as
A(2:end, end:-2:1)
it might help to substitute end with the actual index of the last row/column elements, so this is equivalent to
A(2:3, 4:-2:1)
Furthermore 4:-2:1 is equivalent to the list 4,2 since we are instructing to make the list starting at 4, decreasing in steps of 2, up to (minimum) 1. So this is equivalent to
A([2 3],[4 2])
Finally, the following combination of indices is implied by A([2 3],[4 2]):
A(2,4) A(2,2)
A(3,4) A(3,2)
I want to better understand what is going on here with PowerShell's range operator.
$myArray = 1..10
so we have $myArray with 1 2 3 4 ... 10
Now I want to use -1 to get the last value in the array and show 1 - 10 in reverse, so I do
$myArray[(-1)..0] but this yields only 10 1 (those two values only, nothing in between).
But if I do $myArray[$myArray[-1]..0] this will yield all the values expected 10 9 8 ... 1
Can anyone give an explanation for this? I would think the (-1) being inside [] would evaluate to the last element or value 10 which it seems to be doing then the range would kick in as 10..0 but it seems like the range is being skipped and giving only the two listed values. This is an exercise just to learn PowerShell, there is no specific application of this I'm after. Btw, I get the same 10 1 only if I run the -1 without the ().
Thanks,
Jason
It is quite simple
Let's see what -1..0 returns:
-1
0
So $myArray[-1..0] is equivalent to the $myArray[-1, 0] hence the result.
But the 10..0 expression returns an entire range reversed. Hence the $myArray[$myArray[-1]..0] expression works as you would expected.
An implementation of a brute-force algorithm to solve Sudoku puzzles fails if a cell is discovered in which placing any of the digits 1-9 would be an illegal move.
The implementation is written in C, with the board represented by a 9x9 array. The solver counts down from 9 until a legal number's reached, and if none can be reached, it outputs a zero in its place.
A zero also represents a cell to be filled in. Here's the output (truncated) if a string of zeros (an empty board) is the input:
9 8 7 6 5 4 3 2 1
6 5 4 9 8 7 0 0 0
Those last three zeros are there because the values filled in previously aren't changing. How can I stop the solver from failing like this?
If you would currently put a zero in a spot, instead go back to the previous spot you put a number in and continue to count down till you find another value number for that spot.
For instance, in your example:
9 8 7 6 5 4 3 2 1
6 5 4 9 8 7 0 0 0
Instead of putting the zero in below the three, you would instead go back and try putting a 6 in below the 4.
don't treat every "move" like the right move. E.g. placing the last 7 seemed ok but makes it so that in the next cell no valid moves are left. So upon hitting the "no move possible" situation, go back, and try the next option. Iterate and you will have your solution.
A better way of course would be to start brute forcing for places with a small set of options left; run through all cells and start brute forcing with the cell with the least number of options left. When starting out with all-zero, you would then end up with
9 8 7 6 5 4 3 2 1
6 5 4 0 0 0 0 0 0
3 2 1 0 0 0 0 0 0
which is legal, without backtracking once.
You can do this by pushing your guesses onto a stack. Every time you end up wanting to output a zero, instead pop your last answer off the board and continue counting from it.
So if you guess 3 in (2,3) and next you're looking at (3,3) and get to zero, go back to (2,3) and try 2, then 1, then pop to before your (2,3) guess, etc.