Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python implementation of a Sudoku puzzle solver using backtracking #572

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions Web Development/WeatherAPP/index.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,67 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="custom.css" />
<title>Weather App</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet">
<link rel="stylesheet" href="./index.css">
<script src="index.js"></script>
</head>

<body>
<main>
<div class="row" style="text-align: center;">
<form action="">
<input type="search" id="search" placeholder="Search by city name">
</form>
<div class="card">
<div class="search">
<input type="text" class="search-bar" placeholder="Enter City Name">
<button id="getWeatherButton"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024" height="1.5em"
width="1.5em" xmlns="http://www.w3.org/2000/svg">
<path
d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0 0 11.6 0l43.6-43.5a8.2 8.2 0 0 0 0-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z">
</path>
</svg></button>
<button id="getLocationButton">Use My Location</button>
<select id="unitSelect">
<option value="metric" class="one">Celsius</option>
<option value="imperial" class="one">Fahrenheit</option>
</select>
</div>
<div class="row" id="weather">
<!-- <div>
<img src="https://openweathermap.org/img/wn/[email protected]" alt="">

<div class="weather initial">
<hr>
<h4 class="welcome">Welcome to the Weather App</h4>
<hr>
<h2 class="city"></h2>
<h1 class="temp"></h1>
<div class="flex">
<img src="" alt="" class="icon" />
<div class="description"></div>
</div>
<div>
<h2>32 ℃</h2>
<h4> Clear </h4>
</div> -->
<div class="humidity"></div>
<div class="wind"></div>
</div>
</main>
<script src="app.js"></script>
</body>

</html>
<!-- Favorite Cityes Section -->
<div class="favorite-cities">
<h3>Favorite Cities</h3>
<ul id="favoriteCitiesList">
<!-- Favorite city secton -->
</ul>
</div>

</div>

<script src="index.js"></script>
<script src="https://unpkg.com/[email protected]/dist/typed.umd.js"></script>
<script>
var typed = new Typed('#element', {
strings: ["Welcome To Weather App"],
typeSpeed: 50,
});
</script>
<footer>
<div class="footer-rights">
Copyright &#169; weather App | All rights reserved
</div>
</footer>
</body>
</html>
49 changes: 49 additions & 0 deletions sudoku.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
def solve_sudoku(board):
def is_valid(num, row, col):
for i in range(9):
if board[row][i] == num or board[i][col] == num:
return False

start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(3):
for j in range(3):
if board[start_row + i][start_col + j] == num:
return False
return True

def backtrack():
for row in range(9):
for col in range(9):
if board[row][col] == ".":
for num in map(str, range(1, 10)):
if is_valid(num, row, col):
board[row][col] = num
if backtrack():
return True
board[row][col] = "."
return False
return True

if not board or len(board) != 9 or len(board[0]) != 9:
return

backtrack()

# Example Sudoku puzzle as a 2D list, use "." for empty cells
sudoku_board = [
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
]

solve_sudoku(sudoku_board)

# Print the solved Sudoku board
for row in sudoku_board:
print(" ".join(row))