Skip to content

Commit

Permalink
Add Day 17 Python Implementation (#167)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Aadit Kamat authored and MadhavBahl committed Jan 15, 2019
1 parent 83b1a33 commit ddb3495
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
80 changes: 80 additions & 0 deletions day17/Python/n_queens.py
@@ -0,0 +1,80 @@
"""
@author:aaditkamat
@date: 13/01/2019
"""
def print_chessboard(chessboard):
for i in range(len(chessboard)):
print(chessboard[i])

def fill_chessboard(i, j,chessboard):
chessboard[i][j] = 1

for x in range(len(chessboard)):
if x != i:
chessboard[x][j] = 0

for y in range(len(chessboard)):
if y != j:
chessboard[i][y] = 0

x, y = (i + 1, j + 1)
while x < len(chessboard) and y < len(chessboard):
chessboard[x][y] = 0
x += 1
y += 1

x, y = (i - 1, j - 1)
while x >= 0 and y >= 0:
chessboard[x][y] = 0
x -= 1
y -= 1

x, y = (i + 1, j - 1)
while x < len(chessboard) and y >= 0:
chessboard[x][y] = 0
x += 1
y -= 1

x, y = (i - 1, j + 1)
while x >= 0 and y < len(chessboard):
chessboard[x][y] = 0
x -= 1
y += 1

def try_position(i, chessboard):
fill_chessboard(0, i, chessboard)

for i in range(1, len(chessboard)):
for j in range(len(chessboard)):
if chessboard[i][j] == -1:
fill_chessboard(i, j, chessboard)

def reset_chessboard(chessboard):
for i in range(len(chessboard)):
for j in range(len(chessboard)):
chessboard[i][j] = -1

def is_correct(chessboard):
for i in range(len(chessboard)):
if chessboard[i].count(1) == 0:
return False
return True

def n_queens(num):
chessboard = []
for i in range(num):
chessboard.append([-1] * num)

for i in range(num):
try_position(i, chessboard)
if (is_correct(chessboard)):
print_chessboard(chessboard)
return
reset_chessboard(chessboard)

def main():
print("Enter number of queens: ", end="")
num = int(input())
n_queens(num)

main()
87 changes: 87 additions & 0 deletions day17/README.md
Expand Up @@ -210,6 +210,93 @@ int main() {
}
```
## Python Implemetation
### [Solution](./Python/n_queens.py)
```python
"""
@author:aaditkamat
@date: 13/01/2019
"""
def print_chessboard(chessboard):
for i in range(len(chessboard)):
print(chessboard[i])
def fill_chessboard(i, j,chessboard):
chessboard[i][j] = 1
for x in range(len(chessboard)):
if x != i:
chessboard[x][j] = 0
for y in range(len(chessboard)):
if y != j:
chessboard[i][y] = 0
x, y = (i + 1, j + 1)
while x < len(chessboard) and y < len(chessboard):
chessboard[x][y] = 0
x += 1
y += 1
x, y = (i - 1, j - 1)
while x >= 0 and y >= 0:
chessboard[x][y] = 0
x -= 1
y -= 1
x, y = (i + 1, j - 1)
while x < len(chessboard) and y >= 0:
chessboard[x][y] = 0
x += 1
y -= 1
x, y = (i - 1, j + 1)
while x >= 0 and y < len(chessboard):
chessboard[x][y] = 0
x -= 1
y += 1
def try_position(i, chessboard):
fill_chessboard(0, i, chessboard)
for i in range(1, len(chessboard)):
for j in range(len(chessboard)):
if chessboard[i][j] == -1:
fill_chessboard(i, j, chessboard)
def reset_chessboard(chessboard):
for i in range(len(chessboard)):
for j in range(len(chessboard)):
chessboard[i][j] = -1
def is_correct(chessboard):
for i in range(len(chessboard)):
if chessboard[i].count(1) == 0:
return False
return True
def n_queens(num):
chessboard = []
for i in range(num):
chessboard.append([-1] * num)
for i in range(num):
try_position(i, chessboard)
if (is_correct(chessboard)):
print_chessboard(chessboard)
return
reset_chessboard(chessboard)
def main():
print("Enter number of queens: ", end="")
num = int(input())
n_queens(num)
main()
```

## Java Implementation

### [Solution](./Java/nQueen.java)
Expand Down

0 comments on commit ddb3495

Please sign in to comment.