From 7557019b74eb8f42d030e6e3a274d277246fa2c5 Mon Sep 17 00:00:00 2001 From: Sadaf Date: 2024年5月17日 17:00:06 +0530 Subject: [PATCH 1/2] Added sudoku mini-project --- contrib/mini-projects/sudoku.py | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 contrib/mini-projects/sudoku.py diff --git a/contrib/mini-projects/sudoku.py b/contrib/mini-projects/sudoku.py new file mode 100644 index 00000000..b9cc2478 --- /dev/null +++ b/contrib/mini-projects/sudoku.py @@ -0,0 +1,79 @@ +import random + +class Sudoku: + def __init__(self, grid): + self.grid = grid + + @staticmethod + def generate_empty_grid(): + return [[0 for _ in range(9)] for _ in range(9)] + + @staticmethod + def is_valid_move(grid, row, col, num): + # Check if the number is already present in the row, column, or 3x3 subgrid + return (num not in grid[row] and + num not in (grid[i][col] for i in range(9)) and + num not in (grid[i][j] for i in range(row - row % 3, row - row % 3 + 3) + for j in range(col - col % 3, col - col % 3 + 3))) + + @staticmethod + def generate_sudoku(): + grid = Sudoku.generate_empty_grid() + Sudoku.solve_sudoku(grid) + Sudoku.remove_cells(grid, random.randint(30, 45)) # Adjust the range for the number of empty cells + return grid + + @staticmethod + def solve_sudoku(grid): + empty_cell = Sudoku.find_empty_cell(grid) + if not empty_cell: + return True + row, col = empty_cell + for num in random.sample(range(1, 10), 9): + if Sudoku.is_valid_move(grid, row, col, num): + grid[row][col] = num + if Sudoku.solve_sudoku(grid): + return True + grid[row][col] = 0 + return False + + @staticmethod + def find_empty_cell(grid): + for i in range(9): + for j in range(9): + if grid[i][j] == 0: + return i, j + return None + + @staticmethod + def remove_cells(grid, num_cells_to_remove): + for _ in range(num_cells_to_remove): + row = random.randint(0, 8) + col = random.randint(0, 8) + if grid[row][col] != 0: + grid[row][col] = 0 + + @staticmethod + def print_grid(grid): + for row in grid: + print(" ".join(map(str, row))) + +def main(): + print("Generating Sudoku puzzle...") + sudoku_grid = Sudoku.generate_sudoku() + print("Sudoku puzzle:") + Sudoku.print_grid(sudoku_grid) + + user_input = input("\nWould you like to see the solution? (yes/no): ").strip().lower() + if user_input in ['yes', 'y']: + solved_grid = [row[:] for row in sudoku_grid] # Create a copy of the grid to solve + if Sudoku.solve_sudoku(solved_grid): + print("Sudoku solution:") + Sudoku.print_grid(solved_grid) + else: + print("No solution found.") + else: + print("Solution not displayed.") + +if __name__ == "__main__": + main() From 5994a120288be3248d33691aa60acc348876fcf5 Mon Sep 17 00:00:00 2001 From: Sadaf Date: 2024年5月17日 21:15:28 +0530 Subject: [PATCH 2/2] Added content for Importing and Exporting Data with Pandas --- .../importing_and_exporting_data_from_csv.md | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 contrib/pandas/importing_and_exporting_data_from_csv.md diff --git a/contrib/pandas/importing_and_exporting_data_from_csv.md b/contrib/pandas/importing_and_exporting_data_from_csv.md new file mode 100644 index 00000000..65ac4020 --- /dev/null +++ b/contrib/pandas/importing_and_exporting_data_from_csv.md @@ -0,0 +1,66 @@ +# Importing and Exporting Data with Pandas + +Pandas is a powerful data manipulation library in Python that provides data structures and functions needed to work on structured data seamlessly. One of the most common tasks in data analysis is importing data from external sources and exporting data after manipulation. This guide covers the basics of importing and exporting data using Pandas. + +## Table of Contents +1. [Importing Data from CSV](#importing-data-from-csv) +2. [Exporting Data to CSV](#exporting-data-to-csv) + +## Importing Data from CSV + +CSV (Comma-Separated Values) is one of the most common data formats. Pandas provides a convenient function `read_csv` to load data from a CSV file into a DataFrame. + +### Syntax + +import pandas as pd
+df = pd.read_csv('path_to_your_file.csv') + +### Example + +import pandas as pd
+df = pd.read_csv('data.csv')
+print(df.head()) + +### Additional Parameters + +- `sep`: Specify a different delimiter (default is comma). +- `header`: Row number(s) to use as the column names. +- `names`: List of column names to use. +- `index_col`: Column(s) to set as index (make sure the index column is unique). +- `usecols`: Return a subset of the columns. + +### Example with Additional Parameters + +df = pd.read_csv('data.csv', sep=';', header=0, index_col='id', usecols=['id', 'name', 'age'])
+print(df.head()) + + +## Exporting Data to CSV + +After processing the data, you may want to save the DataFrame back to a CSV file. Pandas provides the `to_csv` function for this purpose. + +### Syntax + +df.to_csv('path_to_save_file.csv') + +### Example + +import pandas as pd
+data = {
+ 'Name': ['Alice', 'Bob', 'Charlie'],
+ 'Age': [25, 30, 35]
+}
+df = pd.DataFrame(data)
+df.to_csv('output.csv', index=False)
+ + +### Additional Parameters + +- `sep`: Specify a different delimiter (default is comma). +- `index`: Whether to write row names (default is True). +- `header`: Write out the column names (default is True). + +### Example with Additional Parameters + +df.to_csv('output.csv', sep=';', index=False, header=True) +

AltStyle によって変換されたページ (->オリジナル) /