I was trying a Backtracking problem (SudokuSolver) I am getting an error which no-one could resolve or even understand. Hence I am seeking help here.
The problem is as follows:
And the main function/part of my code is:
Here my removerow, removecol and removebox functions are removing the numbers which have already occurred in that row, column and sub-box of 3x3 respectively.
The error I am getting is very long that is:
Can someone please help me with this? I'm stuck at it since 2 days. Ready to provide further clarification if needed.
Please find the entire code here: link
I have created a test class, to try understand what your functions are doing - or rather, to make sure they do what I think they do...
import unittest
from Otter.Help_Parth.entire_code import tellbox
from Otter.Help_Parth.entire_code import removerow
from Otter.Help_Parth.entire_code import removecol
from Otter.Help_Parth.entire_code import removebox
class MyTestCase(unittest.TestCase):
def test_tellbox(self):
""" Put each of the 81 x,y combination to tellbox
and retrieve the 81 corresponding box-numbers
assert that the numbers are as expected. """
lst_results = list()
for i in range(9):
for j in range(9):
lst_results.append(tellbox(i, j))
self.assertEqual(lst_results, [0, 0, 0, 3, 3, 3, 6, 6, 6, 0, 0, 0, 3, 3, 3, 6, 6, 6, 0, 0, 0, 3, 3, 3, 6, 6, 6, 1, 1, 1, 4, 4, 4, 7, 7, 7, 1, 1, 1, 4, 4, 4, 7, 7, 7, 1, 1, 1, 4, 4, 4, 7, 7, 7, 2, 2, 2, 5, 5, 5, 8, 8, 8, 2, 2, 2, 5, 5, 5, 8, 8, 8, 2, 2, 2, 5, 5, 5, 8, 8, 8])
def test_removerow(self):
""" Given a partly filled board, a list and an x,y coordinate
try call removerow() and assert that the returned list are as expected. """
board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[7, 8, 9, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x, y = 2, 4
res = removerow(l, board, x, y)
self.assertEqual([2, 3, 4, 5, 6], res)
def test_removecol(self):
""" Given a partly filled board, a list and an x,y coordinate
try call removecol() and assert that the returned list are as expected. """
board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[7, 8, 9, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x, y = 2, 4
res = removecol(l, board, x, y)
self.assertEqual([1, 2, 3, 4, 6, 7, 9], res)
def test_removebox(self):
""" Given a partly filled board, a list and an x,y coordinate
try call removebox() and assert that the returned list are as expected. """
board = [[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[7, 8, 9, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
x, y = 2, 4
bno = tellbox(x, y)
res = removebox(l, board, x, y, bno)
self.assertEqual([2, 3], res)
if __name__ == '__main__':
unittest.main()
It all goes quite well, until removebox().
If you can read and understand my test_removebox(self) Are you kind to confirm that, under the given circumstances, it should in fact return [2, 3]?
Otherwise, please tell what it should return, and why?
When I run the test class, it returns the following:
NB: To be able to run the test class against your code, you need to change it like this:
if __name__ == '__main__':
board = [[ int(ele) for ele in input().split() ] for i in range(9)]
ans=solveSudoku(board,0,0)
if ans:
print('true')
else:
print('false')
Both your code-sample and the output seems incomplete.
We don't know your variables, so I assume 'board' is a list of 81 integers, but what is the mysterious (and newer used) boxl?
And why would you set board[x][y] = 0 (in the line with the comment), when you have just gone through the trouble of setting it = ele?
We would be able to help you better, if you provided the entire code, in a run-able form.
Sorry! I just realized that I answered to the wrong post!
I am trying to set up a simulation but I struggle with something.
I know which variables are relevant or not in the beginning as I set them myself. They are stored in a beta matrix taking 0 if insignificant and 1 (same amplitude for all) if they are significant.
I run a procedure which gives me its list of relevant variables. The problem is that, this procedures is not giving me a 0-1 matrix but just a list of the selected variables using their "place" in number (so 38 for the 38th variable in my list). I know wish to test whether this procedure is powerful or not and I want to display the confusion matrix (among other things).
Below you can find those two vectors. I've tried using the "%in%" operator but it does not work and I don't want to use a loop as the simulation takes enough time already.
Thanks in advance!
res=c( 1, 9, 11 ,18, 19, 25, 26, 28, 31, 34 ,37 ,38, 39, 42 ,43 ,47 ,48, 50) #From my procedure
beta=c(1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1)
I am learning the Qt 3D module. I found the Qt.matrix4x4 provide different API than QMatrix4x4 in C++ side (by using F1 check the API doc).
look likes Qt.matrix4x4 don't have such scale or rotation operations. even not in autocomplete.
BUT! these functions do exist and work!
var m = Qt.matrix4x4(1, 0, 0, -0.5, 0, 1, 0, 0, 0, 0, 1, 0.5, 0, 0, 0, 1)
m.scale(2)
console.log(m)
// qml: QMatrix4x4(2, 0, 0, -0.5, 0, 2, 0, 0, 0, 0, 2, 0.5, 0, 0, 0, 1)
// the type shows QMatrix4x4
https://doc.qt.io/qt-5/qmatrix4x4.html vs https://doc.qt.io/qt-5/qml-matrix4x4.html
I understand what these is, But why the API show different in API doc and autocomplete?
It is interesting to note that in Qt6 the documentation appears different than Qt5. In Qt6 we see clearly there are methods for constructing the matrix4x4 basic type from a vector and for multiplying them with vectors.
// translate(vector3d)
var m = Qt.matrix4x4();
m.translate(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 0, 0, 0, 1)
// matrix multiplication
var m = Qt.matrix4x4();
m.translate(Qt.vector3d(1,2,3));
console.log(m.toString());
// QMatrix4x4(1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3, 0, 0, 0, 1)
// applying a matrix to a vector
var a = Qt.matrix4x4(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
var b = Qt.vector3d(5,6,7);
var c = a.times(b);
console.log(c.toString()); // QVector3D(0.155556, 0.437037, 0.718518)
Note I tried all of the above in Qt5.15.6 but it doesn't work. So it appears that support for this type really kicks in at Qt6.
For further information see: https://doc.qt.io/qt-6/qml-matrix4x4.html
Thank you for your time in advance. I am attempting to identify a method to calculate in-degree Bonacich Power Centrality in R. I'm a long-time UCINET user attempting to make the switch. In UCINET, this is done selecting Beta Centrality (Bonacich Power), and selecting "in-centrality" for the direction.
In R, it doesn't seem as though there is a way to calculate this using either sna or igraph packages. Here it is for bonpow in sna:
bonpow(dat, g=1, nodes=NULL, gmode="digraph", diag=FALSE, tmaxdev=FALSE,
exponent=1, rescale=FALSE, tol=1e-07)
I do specify digraph, but I am not able to replicate the analysis in R.
Similarly, here it is for power_centrality in igraph:
power_centrality(graph, nodes = V(graph), loops = FALSE,
exponent = 1, rescale = FALSE, tol = 1e-07, sparse = TRUE)
Here, there does not seem to be a way to specify that it is a directed graph (although you can specify it when defining the network). However, you can estimate it for betweenness centrality.
In neither case do I seem to be able to specify in-degree or out-degree power centrality. Any help is appreciated. Is there something either in these or in a different package that I may be overlooking?
I'm not sure about what do you mean by direction since the original paper, seems to me, does not deal with it. Now, a thing that is usually done with these statistics that are calculated directly from the adjacency matrix is "change the direction" by taking the transpose of the statistic (for example, when computing exposure in the netdiffuseR package we allow the user to compute "incoming" or "outgoing" exposure by just taking the transpose of the adjacency matrix). When you take the transpose, you are essentially flipping the directionality of the ties, i.e. i->j turns to j->i.
If that's what UCINET does (again, not completely sure what it is), then you can get the "incoming"/"outgoing" version by transposing the network. Here is a toy example:
# Loading the sna package (btw: igraph's implementation is a copy of
# sna's). I wrap it around suppressMessages to avoid the verbose
# print that the package has
suppressMessages(library(sna))
# This is random graph I generated with 10 vertices
net <- structure(
c(0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1,
0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1,
0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1,
0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0),
.Dim = c(10L, 10L)
)
# Here is the default
bonpow(net)
#> [1] -0.8921521 -0.7658658 -0.9165947 -1.4176664 -0.6151369 -0.7862345
#> [7] -0.9206684 -1.3565601 -1.0347335 -1.0062173
# Here I'm getting the transpose of the adjmat
net2 <- t(net)
# The output is different (as you can see)
bonpow(net2)
#> [1] -0.8969158 -1.1026305 -0.6336011 -0.7158869 -1.2960022 -0.9545159
#> [7] -1.1684592 -0.8845729 -1.0368018 -1.1190876
Created on 2019-11-20 by the reprex package (v0.3.0)