feat: First version of the script
This commit is contained in:
13
.gitignore
vendored
Normal file
13
.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
.vscode/
|
||||
.idea/
|
||||
|
||||
env/
|
||||
venv/
|
||||
*.venv
|
||||
*.env
|
||||
|
||||
*.json
|
||||
52
README.md
52
README.md
@ -1,3 +1,51 @@
|
||||
# secret-santa
|
||||
### Secret Santa
|
||||
Simple `python3` script for the "Secret Santa" game. Creates participants pairs from input json file and prints and saves them to the output json file.
|
||||
This small script might evolve into a simple website, API or `bash` script in the future!
|
||||
|
||||
"Secret Santa" game python script. Might evolve into simple website, API or bash script.
|
||||
### Run:
|
||||
- Create `participants.json` file as in the example
|
||||
- Run the next command in your shell (from the same directory as the `main.py` file):
|
||||
```sh
|
||||
python3 main.py
|
||||
```
|
||||
- Enjoy script results!
|
||||
|
||||
### Examples:
|
||||
- Input file (`participants.json`):
|
||||
```json
|
||||
[
|
||||
"Jayce",
|
||||
"Viktor",
|
||||
"Jinx",
|
||||
"Vi",
|
||||
"Ekko",
|
||||
"Caitlyn"
|
||||
]
|
||||
```
|
||||
|
||||
- Output file (`pairs.json`):
|
||||
```json
|
||||
{
|
||||
"Jayce": "Vi",
|
||||
"Vi": "Viktor",
|
||||
"Viktor": "Caitlyn",
|
||||
"Caitlyn": "Jinx",
|
||||
"Jinx": "Ekko",
|
||||
"Ekko": "Jayce"
|
||||
}
|
||||
```
|
||||
|
||||
- Console output:
|
||||
```
|
||||
Reading from participants.json.
|
||||
Writing to pairs.json.
|
||||
Finished!
|
||||
|
||||
Results:
|
||||
Jayce → Vi
|
||||
Vi → Viktor
|
||||
Viktor → Caitlyn
|
||||
Caitlyn → Jinx
|
||||
Jinx → Ekko
|
||||
Ekko → Jayce
|
||||
```
|
||||
|
||||
47
main.py
Normal file
47
main.py
Normal file
@ -0,0 +1,47 @@
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
|
||||
|
||||
def create_pairs_dict(lst: list[str]) -> dict[str, str]:
|
||||
result = {}
|
||||
lst_len = len(lst)
|
||||
|
||||
if lst_len < 2:
|
||||
print("\033[1;31mList length should be at least 2!\033[0m")
|
||||
raise RuntimeError
|
||||
|
||||
for i in range(lst_len - 1):
|
||||
result[lst[i]] = lst[i+1]
|
||||
result[lst[lst_len-1]] = lst[0]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# Main
|
||||
input_file = "participants.json"
|
||||
output_file = "pairs.json"
|
||||
|
||||
|
||||
if os.path.isfile(input_file):
|
||||
print(f"\033[1;33mReading from {input_file}.\033[0m")
|
||||
with open(input_file, "r", encoding="utf-8") as f:
|
||||
participants_list = json.load(f)
|
||||
else:
|
||||
print("\033[1;31mNo input file found! Please create participants.json file.\033[0m")
|
||||
participants_list: list[str] = ["1", "2", "3", "4"]
|
||||
|
||||
shuffled_list = copy.copy(participants_list)
|
||||
random.shuffle(shuffled_list)
|
||||
pairs = create_pairs_dict(shuffled_list)
|
||||
|
||||
print(f"\033[1;33mWriting to {output_file}.\033[0m")
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(pairs, f, ensure_ascii=False, indent=4)
|
||||
|
||||
print("\033[1;32mFinished!\033[0m")
|
||||
|
||||
print("\nResults:")
|
||||
for giver, receiver in pairs.items():
|
||||
print(f"{giver} → {receiver}")
|
||||
Reference in New Issue
Block a user