Skip to content

Commit ddb3495

Browse files
Aadit KamatMadhavBahl
authored andcommitted
Add Day 17 Python Implementation (#167)
* Add @aaditkamat as a contributor * Add Ruby code for Day 1: FizzBuzz problem * Add Ruby code for Day 2: String reverse problem * Update README.md for Day 2 * Modify Ruby code and README * Add condition for nil and wrong type edge cases * Add a seperate Ruby source code file for palindrome * Modify code for reverse.rb * Add seperate palindrome and reverse code sections in README * Update gitignore * Refactor palindrome.rb and rename heading in README * Add solution for Day 3: Hamming Problem * Add condition for strings of unequal lengths * Update README * Change project name and owner in.all-contributorsrc * Remove merge conflict lines * Add @shivank86 as a contributor * Add C++ files for different patterns * Add author and date comments at the top of C++ files * Update README.md * Add solution for Day 6 Problem in Python * Update README * Refactor code files * Modify string representation of output in python files * Add Ruby solutions for Day 6 problem * Update README for Ruby code * Add first version of solutions for Day 7 problem in C++, Java & Ruby * Modify solutions * Update Day 7 README * Remove merge conflicts from CONTRIBUTORS.md * Add back removed lines in CONTRIBUTORS.md * Add code sections contributed by @imkaka to day 6 README * Update README.md * Add C++ solution * Add Day 8 solution in C++ * Add Day 8 solution in Java * Add Day 8 solution in Ruby * Add Day 8 solution in Python * Add credits at the top of the code * Update README * Update C++ implementation * Update Python implementation * Add solution for Day 10: String Permutation Problem in Python Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Update Day 10 README Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Change heading in README and remove empty directory in day 9 Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Update README.md * Add Ruby solutions for Day 13 * Update README * Add credits to the code * Add Python solution for Day 13 * Update Day 13 README * Update fibonacci.py * Modify Fibonacci python code section in day 13 README * Add Ruby solution to Day 16 problem Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Update Day 16 README with Ruby implementation Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Change wording of print statement Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Add Python solution to Day 17 problem Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Add Python implementation to Day 17 README Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com>
1 parent 83b1a33 commit ddb3495

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

day17/Python/n_queens.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
@author:aaditkamat
3+
@date: 13/01/2019
4+
"""
5+
def print_chessboard(chessboard):
6+
for i in range(len(chessboard)):
7+
print(chessboard[i])
8+
9+
def fill_chessboard(i, j,chessboard):
10+
chessboard[i][j] = 1
11+
12+
for x in range(len(chessboard)):
13+
if x != i:
14+
chessboard[x][j] = 0
15+
16+
for y in range(len(chessboard)):
17+
if y != j:
18+
chessboard[i][y] = 0
19+
20+
x, y = (i + 1, j + 1)
21+
while x < len(chessboard) and y < len(chessboard):
22+
chessboard[x][y] = 0
23+
x += 1
24+
y += 1
25+
26+
x, y = (i - 1, j - 1)
27+
while x >= 0 and y >= 0:
28+
chessboard[x][y] = 0
29+
x -= 1
30+
y -= 1
31+
32+
x, y = (i + 1, j - 1)
33+
while x < len(chessboard) and y >= 0:
34+
chessboard[x][y] = 0
35+
x += 1
36+
y -= 1
37+
38+
x, y = (i - 1, j + 1)
39+
while x >= 0 and y < len(chessboard):
40+
chessboard[x][y] = 0
41+
x -= 1
42+
y += 1
43+
44+
def try_position(i, chessboard):
45+
fill_chessboard(0, i, chessboard)
46+
47+
for i in range(1, len(chessboard)):
48+
for j in range(len(chessboard)):
49+
if chessboard[i][j] == -1:
50+
fill_chessboard(i, j, chessboard)
51+
52+
def reset_chessboard(chessboard):
53+
for i in range(len(chessboard)):
54+
for j in range(len(chessboard)):
55+
chessboard[i][j] = -1
56+
57+
def is_correct(chessboard):
58+
for i in range(len(chessboard)):
59+
if chessboard[i].count(1) == 0:
60+
return False
61+
return True
62+
63+
def n_queens(num):
64+
chessboard = []
65+
for i in range(num):
66+
chessboard.append([-1] * num)
67+
68+
for i in range(num):
69+
try_position(i, chessboard)
70+
if (is_correct(chessboard)):
71+
print_chessboard(chessboard)
72+
return
73+
reset_chessboard(chessboard)
74+
75+
def main():
76+
print("Enter number of queens: ", end="")
77+
num = int(input())
78+
n_queens(num)
79+
80+
main()

day17/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,93 @@ int main() {
210210
}
211211
```
212212
213+
## Python Implemetation
214+
215+
### [Solution](./Python/n_queens.py)
216+
217+
```python
218+
"""
219+
@author:aaditkamat
220+
@date: 13/01/2019
221+
"""
222+
def print_chessboard(chessboard):
223+
for i in range(len(chessboard)):
224+
print(chessboard[i])
225+
226+
def fill_chessboard(i, j,chessboard):
227+
chessboard[i][j] = 1
228+
229+
for x in range(len(chessboard)):
230+
if x != i:
231+
chessboard[x][j] = 0
232+
233+
for y in range(len(chessboard)):
234+
if y != j:
235+
chessboard[i][y] = 0
236+
237+
x, y = (i + 1, j + 1)
238+
while x < len(chessboard) and y < len(chessboard):
239+
chessboard[x][y] = 0
240+
x += 1
241+
y += 1
242+
243+
x, y = (i - 1, j - 1)
244+
while x >= 0 and y >= 0:
245+
chessboard[x][y] = 0
246+
x -= 1
247+
y -= 1
248+
249+
x, y = (i + 1, j - 1)
250+
while x < len(chessboard) and y >= 0:
251+
chessboard[x][y] = 0
252+
x += 1
253+
y -= 1
254+
255+
x, y = (i - 1, j + 1)
256+
while x >= 0 and y < len(chessboard):
257+
chessboard[x][y] = 0
258+
x -= 1
259+
y += 1
260+
261+
def try_position(i, chessboard):
262+
fill_chessboard(0, i, chessboard)
263+
264+
for i in range(1, len(chessboard)):
265+
for j in range(len(chessboard)):
266+
if chessboard[i][j] == -1:
267+
fill_chessboard(i, j, chessboard)
268+
269+
def reset_chessboard(chessboard):
270+
for i in range(len(chessboard)):
271+
for j in range(len(chessboard)):
272+
chessboard[i][j] = -1
273+
274+
def is_correct(chessboard):
275+
for i in range(len(chessboard)):
276+
if chessboard[i].count(1) == 0:
277+
return False
278+
return True
279+
280+
def n_queens(num):
281+
chessboard = []
282+
for i in range(num):
283+
chessboard.append([-1] * num)
284+
285+
for i in range(num):
286+
try_position(i, chessboard)
287+
if (is_correct(chessboard)):
288+
print_chessboard(chessboard)
289+
return
290+
reset_chessboard(chessboard)
291+
292+
def main():
293+
print("Enter number of queens: ", end="")
294+
num = int(input())
295+
n_queens(num)
296+
297+
main()
298+
```
299+
213300
## Java Implementation
214301

215302
### [Solution](./Java/nQueen.java)

0 commit comments

Comments
 (0)