Creating a chess board for the 8 Queens puzzle - python

s = [0,2,6,4,7,1,5,3]
def row_top():
print("|--|--|--|--|--|--|--|--|")
def cell_left():
print("| ", end = "")
def solution(s):
for i in range(8):
row(s[i])
def cell_data(isQ):
if isQ:
print("X", end = "")
return ()
else:
print(" ", end = "")
def row_data(c):
for i in range(9):
cell_left()
cell_data(i == c)
def row(c):
row_top()
row_data(c)
print("\n")
solution(s)
My output has a space every two lines, when there shouldn't be, I'm not sure where it's creating that extra line.
The output is suppose to look like this:
|--|--|--|--|--|--|--|--|
| | | | | | X| | |
|--|--|--|--|--|--|--|--|
| | | X| | | | | |
|--|--|--|--|--|--|--|--|
| | | | | X| | | |
|--|--|--|--|--|--|--|--|
| | | | | | | | X|
|--|--|--|--|--|--|--|--|
| X| | | | | | | |
|--|--|--|--|--|--|--|--|
| | | | X| | | | |
|--|--|--|--|--|--|--|--|
| | X| | | | | | |
|--|--|--|--|--|--|--|--|
| | | | | | | X| |
|--|--|--|--|--|--|--|--|
I know this chess board isn't very square but this is only a rough draft at the moment.

Here is an alternative implementation:
def make_row(rowdata, col, empty, full):
items = [col] * (2*len(rowdata) + 1)
items[1::2] = (full if d else empty for d in rowdata)
return ''.join(items)
def make_board(queens, col="|", row="---", empty=" ", full=" X "):
size = len(queens)
bar = make_row(queens, col, row, row)
board = [bar] * (2*size + 1)
board[1::2] = (make_row([i==q for i in range(size)], col, empty, full) for q in queens)
return '\n'.join(board)
queens = [0,2,6,4,7,1,5,3]
print(make_board(queens))
which results in
|---|---|---|---|---|---|---|---|
| X | | | | | | | |
|---|---|---|---|---|---|---|---|
| | | X | | | | | |
|---|---|---|---|---|---|---|---|
| | | | | | | X | |
|---|---|---|---|---|---|---|---|
| | | | | X | | | |
|---|---|---|---|---|---|---|---|
| | | | | | | | X |
|---|---|---|---|---|---|---|---|
| | X | | | | | | |
|---|---|---|---|---|---|---|---|
| | | | | | X | | |
|---|---|---|---|---|---|---|---|
| | | | X | | | | |
|---|---|---|---|---|---|---|---|
It is now very easy to change the width of the board by changing the strings passed to row, empty, full; I added an extra char to each, resulting in a (somewhat) squarer board.

You are still printing an extra newline:
def row(c):
row_top()
row_data(c)
print("\n")
Remove the explicit ''\n'` character:
def row(c):
row_top()
row_data(c)
print()
or better still, follow my previous answer more closely and print a closing | bar:
def row(c):
row_top()
row_data(c)
print('|')

Related

Button Not Interacting (Custom Type) - Pywinauto

I am working on automating a process that uses our ancient HRIS system that unfortunately doesn't have API Access.
I am fairly new to Python, so I have been taking this task bit by bit. I've managed to connect to the app and input my username and password to sign in. However, I am stuck on selecting a menu item. I've tried everything that I know to do and have Googled until I've gone cross-eyed.
Dialog - 'City of Conway LIVE Springbrook V7' (L0, T0, R1032, B1039)
['Dialog', 'City of Conway LIVE Springbrook V7Dialog', 'City of Conway LIVE Springbrook V7', 'Dialog0', 'Dialog1']
child_window(title="City of Conway LIVE Springbrook V7", auto_id="MainMenu", control_type="Window")
|
| Pane - '' (L231, T87, R1024, B118)
| ['Pane', 'Pane0', 'Pane1']
| child_window(auto_id="_panelExWorkArea", control_type="Pane")
| |
| | Pane - 'Desktop' (L231, T90, R1021, B115)
| | ['DesktopPane', 'Desktop', 'Pane2']
| | child_window(title="Desktop", auto_id="_ssiGroupHeaderWorkArea", control_type="Pane")
|
| Pane - '' (L228, T87, R231, B1005)
| ['Pane3']
| child_window(auto_id="_ssiExpandableSplitter1", control_type="Pane")
|
| Pane - '' (L8, T87, R228, B1005)
| ['Pane4']
| child_window(auto_id="_panelTaskArea", control_type="Pane")
| |
| | Pane - '' (L11, T90, R228, B1002)
| | ['Pane5']
| | child_window(auto_id="328582", control_type="Pane")
| | |
| | | TreeView - '' (L11, T115, R228, B1002)
| | | ['TreeView', 'TreeView0', 'TreeView1']
| | | child_window(auto_id="1775914", control_type="Tree")
| | | |
| | | | Pane - '' (L28, T269, R194, B609)
| | | | ['Pane6']
| | | | child_window(auto_id="726924", control_type="Pane")
| | | | |
| | | | | Pane - '' (L28, T269, R194, B609)
| | | | | ['Pane7']
| | | | | child_window(auto_id="1317216", control_type="Pane")
| | | | | |
| | | | | | TreeView - '' (L28, T269, R194, B609)
| | | | | | ['TreeView2']
| | | | | | child_window(auto_id="2101028", control_type="Tree")
| | | | | | |
| | | | | | | Custom - 'Maintenance' (L0, T0, R0, B0)
| | | | | | | ['Custom', 'Maintenance', 'MaintenanceCustom', 'Custom0', 'Custom1', 'Maintenance0', 'Maintenance1', 'MaintenanceCustom0', 'MaintenanceCustom1']
| | | | | | | child_window(title="Maintenance", control_type="Custom")
I'm using a few tools to inspect the GUI, and this one specifically allows me to do the desired task by selecting "do it". It allows me to expand and collapse the section, so surely I've got to be missing something somewhere?
enter image description here
enter image description here
Here is my code:
from pywinauto import Application
app=Application(backend="uia").connect(path=r"C:\Users\skywalker\AppData\Local\Apps\2.0\C38DNYDP.PZ6\07BV1NGN.8G6\spri..ons1_b443b3e57637483a_0007.000f_52ec298e739bfebb", timeout = 30)
maintenance = app.CityofConwayLIVESpringbrookV7.WindowsForms10.Window.8.app.0.a0f91b_r8_ad1, 263022
maintenance.click()
I would also like to mention that I CAN get it to work with Click_Input, but I would like to avoid that if at all possible.

Handling Custom ComboBox in pywinauto

I am writing a script to select a region in a ComboBox. I can use app.dialog['Region:ComboBox'].select(index), but not app.dialog['Region:ComboBox'].select('string'). I notice the ComboBox is custom and is generated real time. How can I select the proper option using a string? The string would be a region like US West, US East, etc.
| | GroupBox - 'Preferences' (L811, T456, R1108, B593)
| | ['PreferencesGroupBox', 'Preferences', 'GroupBox', 'GroupBox0', 'GroupBox1']
| | child_window(title="Preferences", control_type="Group")
| | |
| | | Static - 'PREFERENCES' (L817, T462, R1102, B476)
| | | ['PREFERENCES', 'PREFERENCESStatic', 'Static25']
| | | child_window(title="PREFERENCES", control_type="Text")
| | |
| | | Static - 'Region Text:' (L0, T0, R0, B0)
| | | ['Region Text:Static', 'Region Text:', 'Static26']
| | | child_window(title="Region Text:", control_type="Text")
| | |
| | | Static - '' (L0, T0, R0, B0)
| | | ['Static27']
| | |
| | | Static - 'Region:' (L822, T498, R937, B512)
| | | ['Region:Static', 'Region:', 'Static28']
| | | child_window(title="Region:", control_type="Text")
| | |
| | | Custom - '' (L947, T492, R1097, B518)
| | | ['Custom3', 'Region:Custom']
| | | |
| | | | ComboBox - '' (L947, T492, R1097, B518)
| | | | ['ComboBox', 'Region:ComboBox', 'ComboBoxESRI.ArcGIS.Azure.IaaS.Interfaces.RegionInfo']
| | | | |
| | | | | Edit - '' (L0, T0, R0, B0)
| | | | | ['Edit', 'Edit0', 'Edit1']
| | | | | child_window(auto_id="PART_EditableTextBox", control_type="Edit")
| | |
| | | Static - 'Remote Desktop Port:' (L822, T534, R937, B548)
| | | ['Remote Desktop Port:', 'Remote Desktop Port:Static', 'Static29']
| | | child_window(title="Remote Desktop Port:", control_type="Text")
| | |
| | | Edit - '3389' (L947, T528, R1097, B554)
| | | ['Edit2', 'Remote Desktop Port:Edit']
| | | child_window(title="3389", control_type="Edit")
| | | |
| | | | ScrollBar - '' (L0, T0, R0, B0)
| | | | ['ScrollBar', 'ScrollBar0', 'ScrollBar1']
| | | | child_window(auto_id="VerticalScrollBar", control_type="ScrollBar")
| | | |
| | | | ScrollBar - '' (L0, T0, R0, B0)
| | | | ['ScrollBar2']
| | | | child_window(auto_id="HorizontalScrollBar", control_type="ScrollBar")
| | | |
| | | | Button - 'r' (L0, T0, R0, B0)
| | | | ['r', 'Button8', 'rButton']
| | | | child_window(title="r", auto_id="PART_ClearText", control_type="Button")
| | |
| | | CheckBox - 'Track application usage anonymously' (L822, T564, R1097, B582)
| | | ['Track application usage anonymously', 'CheckBox', 'Track application usage anonymouslyCheckBox', 'Track application usage anonymously0', 'Track application usage anonymously1']
| | | child_window(title="Track application usage anonymously", control_type="CheckBox")
| | | |
| | | | Static - 'Track application usage anonymously' (L846, T565, R1043, B581)
| | | | ['Track application usage anonymously2', 'Track application usage anonymouslyStatic', 'Static30']
| | | | child_window(title="Track application usage anonymously", control_type="Text")
Here's how I implemented it. This function emulates the select() function more efficiently.
def comboselect(combo,sel):
combo.type_keys("{ENTER}") # Selects the combo box
texts = combo.texts() #gets all texts available in combo box
try:
index = texts.index(str(sel)) #find index of required selection
except ValueError:
return False
sel_index = combo.selected_index() # find current index of combo
if(index>sel_index):
combo.type_keys("{DOWN}"*abs(index-sel_index))
else:
combo.type_keys("{UP}"*abs(index-sel_index))
return True

CoGroupByKey, emit after n records have been grouped

I'm trying to parallelize some heavier computations like so:
inputs = (p | "Read" >> beam.io.ReadFromAvro('/mypath/myavrofiles*')
| "Generate Key" >> beam.Map(lambda row: (gen_key(row), row)))
calc1_results = inputs | "perform calc1" >> beam.Pardo(Calc1())
calc2_results = inputs | "perform calc2" >> beam.Pardo(Calc2())
combined = (({"calc1": calc1_results, "calc2": calc2_results})
| beam.CoGroupByKey()
| beam.Values())
final = combined | "Use Grouped results" >> beam.ParDo(PerformFinalCalculation())
Each heavy calc emits (key, result)
Each key is unique for each input. One input, One result, one Key
Is there some way to emit from the CoGroupByKey after a single result1/result2 has been collected for each key?
Ultimately I'd like to achieve something along the lines of:
+------------+
| |
| Input |
| +-----------------+
+------------+ |
| |
v-------------------v |
+------------+ +------------+ |
| | | | |
| Heavy | | Heavy | |
| Calc 1 | | Calc 2 | |
| | | | |
+------------+ +------------+ |
| | |
| | |
| | |
+--v------------v--+ |
| Merged | |
| original dict, +<---------------+
|result 1, result2 |
| |
+------------------+

How can I turn symbols and whitespace into a 2D list? Best way?

Imagine that I have the follow in a .txt file:
+---+-----+-----+
| | | |
| | | |
| | +=====+
+---+ | | |
| | | | |
| +-+ +=====+
| | | | |
| | +-+ | |
| | | | |
| | +-+ |
| | |_____|
| | | |
+---+-----+-----+
What is the best way I can turn this into a 2D list that also includes the whitespace?
I wrote a function that will print it as a string.. But I simply do not know how to make this thing work. I'm just stumped.
You can use the .readlines() function, like so:
with open('in.txt') as input_file:
data = input_file.readlines()
assert data[0][0] == '+'
assert data[3][4] == '|'
with open('filename.txt', 'r') as f:
lines = [[letter for letter in line] for line in f.read().split('\n')]

Understanding multiple recursive calls built into variable assignments in a recursive function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an object which is a binary tree, built by a recursive function. I'm trying to understand the recursive calls in this:
def buildDTree(sofar, todo):
if len(todo) == 0:
return binaryTree(sofar)
else:
withelt = buildDTree(sofar + [todo[0]], todo[1:])
withoutelt = buildDTree(sofar, todo[1:])
here = binaryTree(sofar)
here.setLeftBranch(withelt)
here.setRightBranch(withoutelt)
return here
I don't understand the order of execution of the statements inside of the function. Specifically I don't understand the variable assignment statements and the order in which they will be assigned and what they will be assigned.
I do understand tree structure, how classes are made, and how simpler recursive functions work in python using return statements to initiate the recursion.
Tree object:
class binaryTree(object):
def __init__(self, value):
self.value = value
self.leftBranch = None
self.rightBranch = None
self.parent = None
def setLeftBranch(self, node):
self.leftBranch = node
def setRightBranch(self, node):
self.rightBranch = node
def setParent(self, parent):
self.parent = parent
def getValue(self):
return self.value
def getLeftBranch(self):
return self.leftBranch
def getRightBranch(self):
return self.rightBranch
def getParent(self):
return self.parent
def __str__(self):
return self.value
The buildDTree function is called using the following variables and assignment statement:
a = [6,3]
b = [7,2]
c = [8,4]
d = [9,5]
treeTest = buildDTree([], [a,b,c,d])
well, when you don't understand a code, the methodology is always the same: run the code as if you were the computer. i.e. make a table with all your variables and execute each line of code and write each variable modification down.
buildDTree(sofar=[], todo=[[6,3],[7,2],[8,4],[9,5]]):
| sofar | todo |
| ----- | --------------------------- |
| `[]` | `[[6,3],[7,2],[8,4],[9,5]]` |
len(todo) == 0 → false
withelt = buildDTree(sofar + [todo[0]], todo[1:])
| sofar | todo |
| ---------- | --------------------- |
| `[[6,3]]` | `[[7,2],[8,4],[9,5]]` |
len(todo) == 0 → false
withelt = buildDTree(sofar + [todo[0]], todo[1:])
| sofar | todo |
| ---------------- | --------------- |
| `[[6,3],[7,2]]` | `[[8,4],[9,5]]` |
len(todo) == 0 → false
withelt = buildDTree(sofar + [todo[0]], todo[1:])
| sofar | todo |
| ----------------------- | --------- |
| `[[6,3],[7,2],[8,4]]]` | `[[9,5]]` |
len(todo) == 0 → false
withelt = buildDTree(sofar + [todo[0]], todo[1:])
| sofar | todo |
| ---------------------------- | ---- |
| `[[6,3],[7,2],[8,4],[9,5]]` | `[]` |
len(todo) == 0 → true
return binaryTree(sofar)
| sofar | todo | withelt |
| ----------------------- | --------- | --------------------------------------------- |
| `[[6,3],[7,2],[8,4]]]` | `[[9,5]]` | `binaryTree(value=[[6,3],[7,2],[8,4],[9,5]])` |
withoutelt = buildDTree(sofar, todo[1:])
| sofar | todo |
| ---------------------- | ---- |
| `[[6,3],[7,2],[8,4]]` | `[]` |
len(todo) == 0 → true
return binaryTree(sofar)
| sofar | todo | withelt | withoutelt |
| ----------------------- | --------- | --------------------------------------------- | --------------------------------------- |
| `[[6,3],[7,2],[8,4]]]` | `[[9,5]]` | `binaryTree(value=[[6,3],[7,2],[8,4],[9,5]])` | `binaryTree(value=[[6,3],[7,2],[8,4]])` |
here = binaryTree(sofar)
| sofar | todo | withelt | withoutelt | here |
| ----------------------- | --------- | --------------------------------------------- | --------------------------------------- | --------------------------------------- |
| `[[6,3],[7,2],[8,4]]]` | `[[9,5]]` | `binaryTree(value=[[6,3],[7,2],[8,4],[9,5]])` | `binaryTree(value=[[6,3],[7,2],[8,4]])` | `binaryTree(value=[[6,3],[7,2],[8,4]])` |
here.setLeftBranch(withelt)
here.setRightBranch(withoutelt)
| sofar | todo | withelt | withoutelt | here |
| ----------------------- | --------- | --------------------------------------------- | --------------------------------------- | ------------------------------------------------------------- |
| `[[6,3],[7,2],[8,4]]]` | `[[9,5]]` | `binaryTree(value=[[6,3],[7,2],[8,4],[9,5]])` | `binaryTree(value=[[6,3],[7,2],[8,4]])` | `binaryTree(value=[[6,3],[7,2],[8,4]]` |
| | | | | left=binaryTree(value=[[6,3],[7,2],[8,4],[9,5]]) |
| | | | | right=binaryTree(value=[[6,3],[7,2],[8,4]])) |
return here
| sofar | todo | withelt |
| ---------------- | --------------- | --------------------------------------------------------------|
| `[[6,3],[7,2]]` | `[[8,4],[9,5]]` | `binaryTree(value=[[6,3],[7,2],[8,4]]` |
| | | left=binaryTree(value=[[6,3],[7,2],[8,4],[9,5]]) |
| | | right=binaryTree(value=[[6,3],[7,2],[8,4]])) |
withoutelt = buildDTree(sofar, todo[1:])
...
...
...
I'm not finishing because I do have work on my own, and because anyway it's better for you to complete this. I hope you've got the ideas behind this.
I know how tricky can recursion feel like when begining, but in the end, it's just a question of methodology.
HTH

Categories

Resources