473,722 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Not Equal to Each Other?

Another question: I am writing a sudoku solving program. The
'solving' part of is just multiple iterations. It will take random
numbers and keep switching it all around until a set of logic
statements has been met (ie; all numbers in a row are not equal to each
other) ... that's where my question comes in.

Cellboard = my list for storing each row/column's data.

Rather than writing

cellboard[0] is not* (cellboard[1] and cellboard[2] and cellboard[3]
and cellboard[4] ... cellboard[8])
cellboard[1] is not (cellboard[0] and cellboard[2] and cellboard[3] and
cellboard[4] ... cellboard[8])
etc...

* should this be != ?

the above so that all the data in one row is not equal to each other,
is there something I can write to make it simpler? For example,
(cellboard[0] is not cellboard[1] is not ... cellboard[8]) only worked
for the numbers to the left and right of the cell - is there anyway I
can expand this to cover all numbers in a set range?

Nov 4 '05 #1
7 4371
On 3 Nov 2005 17:01:08 -0800, al***********@g mail.com
<al***********@ gmail.com> wrote:
the above so that all the data in one row is not equal to each other,
is there something I can write to make it simpler? For example,
(cellboard[0] is not cellboard[1] is not ... cellboard[8]) only worked
for the numbers to the left and right of the cell - is there anyway I
can expand this to cover all numbers in a set range?


Python has an operator call 'in', which will allow you to do what you're after.

"1 in (1,2,3)" will be true
"4 in (1,2,3)" will be false
"not 1 in (1,2,3)" will be false

So you'd be after something along the lines of:
not cellboard[0] in (cellboard[1], ...., celboard[8]).

This seems quite tedious to write, maybe you should consider something
along the lines of using slicing:

not celboard[0] in cellboard[1:8]

I hope i have given you enough tools to do what you're trying to do.

--
Stephen Thorne
Development Engineer
Nov 4 '05 #2
<al***********@ gmail.com> wrote:
...
Rather than writing

cellboard[0] is not* (cellboard[1] and cellboard[2] and cellboard[3]
and cellboard[4] ... cellboard[8])
cellboard[1] is not (cellboard[0] and cellboard[2] and cellboard[3] and
cellboard[4] ... cellboard[8])


Urgh... the fastest way to check that a list of N numbers has no
duplicates is:
if len(set(thelist )) == len(thelist):
print 'no duplicates'

But if your purpose is to generate N random samples out of a population
of M, look at function random.sample (in module random in the Pythons
standard library) and you'll do even better!-)
Alex
Nov 4 '05 #3
For the

not cellboard[0] in cellboard[1:8] (I knew about ranges/slicing using a
colon, can't believe I didn't think of that!)

line, will I have to write that out for each number?

So the line:

not cellboard in ((cellboard[1:8]) and (cellboard[9] and cellboard[18]
and cellboard[27] and cellboard[36] and cellboard[45] and cellboard[54]
and cellboard[63] and cellboard[72]) and (cellboard[1:2] and
cellboard[9:11] and cellboard[18:20]))

will cover all the logic requirements for the number in cell 0 (well,
row 1, column 1).

But will I have to copy + paste + edit that for all 81 cells? That
isn't complicated, just tedious - thanks though.

Nov 4 '05 #4
On 3 Nov 2005 17:01:08 -0800, al***********@g mail.com wrote:
Another question: I am writing a sudoku solving program. The
'solving' part of is just multiple iterations. It will take random
numbers and keep switching it all around until a set of logic
statements has been met (ie; all numbers in a row are not equal to each
other) ... that's where my question comes in.

Cellboard = my list for storing each row/column's data.

Rather than writing

cellboard[0] is not* (cellboard[1] and cellboard[2] and cellboard[3]
and cellboard[4] ... cellboard[8])
cellboard[1] is not (cellboard[0] and cellboard[2] and cellboard[3] and
cellboard[4] ... cellboard[8])
etc...

* should this be != ?

the above so that all the data in one row is not equal to each other,
is there something I can write to make it simpler? For example,
(cellboard[0] is not cellboard[1] is not ... cellboard[8]) only worked
for the numbers to the left and right of the cell - is there anyway I
can expand this to cover all numbers in a set range?


UIAM if you have a list of items that are comparable and hashable, like integers,
you can make a set of the list, and duplicates will be eliminated in the set.
Therefore if the resulting set has the same number of members as the list it
was made from, you can conclude that the list contains no duplicates. E.g.,
cellboard = range(8)
cellboard [0, 1, 2, 3, 4, 5, 6, 7] set(cellboard) set([0, 1, 2, 3, 4, 5, 6, 7]) len(set(cellboa rd)) 8 cellboard[2] = 7
cellboard [0, 1, 7, 3, 4, 5, 6, 7] set(cellboard) set([0, 1, 3, 4, 5, 6, 7]) len(set(cellboa rd)) 7

So the test would be len(set(cellboa rd))==len(cellb oard) False
And after repairing the list to uniqueness of elements: cellboard[2] = 2
len(set(cellboa rd))==len(cellb oard)

True

HTH

Regards,
Bengt Richter
Nov 4 '05 #5
How do I 'define' set? Is there something to include (like import
random)?

while (choice == 3) and len(set(cellboa rd[0:8]))==len(cellboa rd[0:8]):
# DEFINE TWO RANDOM VARIABLES (ONE FOR ARRAY, ONE FOR NUMBER
VALUE)
solvingrandom = random.randint( 1,9)
cellboardrandom = random.randint( 0,8)
set(cellboard[0:8])

# CHECK TO MAKE SURE THE RANDOMLY ASSIGNED CELL DOES NOT HAVE A
VALUE
if (cellboard[cellboardrandom] is not ('1' or '2' or '3' or '4'
or '5' or '6' or '7' or '8' or '9')):
cellboard[cellboardrandom] = solvingrandom

The above is my code (right now it will only work for the first row's
numbers). Anything else I need to add?

Nov 4 '05 #6
al***********@g mail.com wrote:
How do I 'define' set? Is there something to include (like import
random)?
set is a built-in type in Python 2.4
If you use 2.3 you can use the sets module with "import sets"

while (choice == 3) and len(set(cellboa rd[0:8]))==len(cellboa rd[0:8]):
# DEFINE TWO RANDOM VARIABLES (ONE FOR ARRAY, ONE FOR NUMBER
VALUE)
solvingrandom = random.randint( 1,9)
cellboardrandom = random.randint( 0,8)
set(cellboard[0:8])

# CHECK TO MAKE SURE THE RANDOMLY ASSIGNED CELL DOES NOT HAVE A
VALUE
if (cellboard[cellboardrandom] is not ('1' or '2' or '3' or '4'
or '5' or '6' or '7' or '8' or '9')):
cellboard[cellboardrandom] = solvingrandom

The above is my code (right now it will only work for the first row's
numbers). Anything else I need to add?

Simplify your code a bit:

'2' is not ('1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9')
evaluates to True
'1' is not ('1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9')
evaluates to False
Somehow I do not believe you want that behavipur.

If cellboard contains characters, you could use:
if (cellboard[cellboardrandom] not in '123456789')

for integers, the following should work:
if not (1 <= cellboard[cellboardrandom] <= 9)

Using None to code empty cells, you could even have:
if (cellboard[cellboardrandom] is None)
Nov 4 '05 #7
> will I have to write that out for each number?

Not if you know how to use the 'for' statement. It will allow you to
iterate through the rows or columns or whatnot.

Nov 4 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9513
by: Jeremy Howard | last post by:
Hello everyone, I'm not a database guru so I'm sorry if this is a dumb question but here it goes... I have this sql query that I'm trying to run against a table on a Sql 2k server: SELECT * FROM ChangeTable
8
3162
by: Ayende Rahien | last post by:
I've a really strange problem, in some part of my code I compare two strings (through object), and while I *know* that they equal each other, and in the watch window they do equal each other, then the comparision return false. See here for details: http://www.ayende.com/Blog/PermaLink,guid,f5aa1a11-1710-434f-bfe0-39ac876a6acb.aspx
6
10918
by: veerleverbr | last post by:
Hi, I have the following html: <div id="content"> <div id="leftpart">...</div> <div id="rightpart">...</div> </div> leftpart en rightpart are in the css set to float left. The content of leftpart and rightpart div is so that both divs have different height.
1
2697
by: Glen Welsh via AccessMonster.com | last post by:
I am trying to set a control to equal the last value entered for that control on the previous form. In other words: I have a control called HullNumber, which will remain constant for approximately 25 forms and then change to a new value. It is a necessary control for my relationships to work but it sure would be nice not to have to enter all of that text each time I enter a new record. Help me please. -- Message posted via...
2
18213
by: Ed Sutton | last post by:
I have variables that are declared as objects. Ints, strings and other data types are then assigned to these objects. How can I write a compare objects function? I tried objOne==objTwo. I am guessing that this must be comparing references and not values because it returns false. // Test case objOne = strName1; objTwo = strName2;
7
10413
by: Piper707 | last post by:
Hi, I need to know how I can check to see if a particular node is NOT equal to a SET of values. i.e. a valid form of : <xsl:template match!="H" && match!="Y"&& match!="Z"> I have an XML document of the foll format:
5
12382
by: Yves Glodt | last post by:
Hello, I need to compare 2 instances of objects to see whether they are equal or not, but with the code down it does not work (it outputs "not equal") #!/usr/bin/python class Test:
17
4531
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are selected into 2 different SP variables and compared for equal. They are both NULL, but do not compare as equal. When the Not NULL columns (SALARY) are compared, they do compare as equal.
14
2532
by: serge calderara | last post by:
Dear all, What is the proper way to check if two object are equal ? I do not mean equal on Object type only but also object value's thnaks for help regards serge
0
8740
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8059
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3208
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2148
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.