Skip to content

Instantly share code, notes, and snippets.

@huseyinyilmaz
Created July 31, 2022 22:10
Show Gist options
  • Save huseyinyilmaz/46a22e6d3f40e424d03108c269e35fae to your computer and use it in GitHub Desktop.
Save huseyinyilmaz/46a22e6d3f40e424d03108c269e35fae to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5d9eb7be-e742-4dc6-9a66-32b684928505",
"metadata": {},
"outputs": [],
"source": [
"# https://www.bundle.app/wordle-tr/"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "13e0ce25-24a7-4cf2-b6d9-0995ef4c1b97",
"metadata": {},
"outputs": [],
"source": [
"from collections import Counter\n",
"from dataclasses import dataclass"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "848432f6-cd23-42c9-9094-e7464583544f",
"metadata": {},
"outputs": [],
"source": [
"invalid_chars = {'/', ' '}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ec9042f6-97e0-4aa3-90d0-c45e1fcb0cb6",
"metadata": {},
"outputs": [],
"source": [
"def get_words():\n",
" word_set = set()\n",
" with open('list/turkce_kelime_listesi.txt') as f:\n",
" for line in f:\n",
" word = line.strip().lower()\n",
" if len(word) == 5 and not set(word).intersection(invalid_chars):\n",
" word_set.add(word)\n",
" word_list = list(word_set)\n",
" return word_list"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ecd6229a-25bd-424f-95bc-d0830c984300",
"metadata": {},
"outputs": [],
"source": [
"word_list = get_words()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c4148be-eae4-4ef9-b553-0fc7b97f4e95",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 6,
"id": "84484d20-033c-4701-b223-c776afffb294",
"metadata": {},
"outputs": [],
"source": [
"def count_chars(word_list):\n",
" char_counter = Counter()\n",
" for word in word_list:\n",
" for c in set(word):\n",
" char_counter[c] += 1\n",
" return char_counter"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa8ba1d5-2c31-46c0-8564-d98304b4f327",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ad7eac06-169d-48dc-9cdf-57472a5f6116",
"metadata": {},
"outputs": [],
"source": [
"@dataclass\n",
"class Guess:\n",
" word: str\n",
" result: (int, int, int, int, int)\n",
" \n",
"@dataclass\n",
"class Word:\n",
" word: str\n",
" score: int\n",
" index: int"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "67da67cc-efac-4c40-a945-73244db3b4ae",
"metadata": {},
"outputs": [],
"source": [
"def find_words_with_unused_chars(word_list, guesses):\n",
" for guess in guesses:\n",
" char_set = set(guess.word)\n",
" word_list = [word for word in word_list if not set(word).intersection(char_set)]\n",
" return parse_words(word_list)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1e696d06-8df9-4f51-81c8-697d1760ffdb",
"metadata": {},
"outputs": [],
"source": [
"def parse_words(word_list):\n",
" char_counter = count_chars(word_list)\n",
" most_used = {word: sum(char_counter[c] for c in word) for word in word_list}\n",
" ws = [(score, w) for w, score in most_used.items()]\n",
" ws.sort()\n",
" ws.reverse()\n",
" return [Word(w, score, i) for i, (score, w) in enumerate(ws) if len(set(w)) == len(w)]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "4a0c0659-d89c-464e-8b45-85bfe651085b",
"metadata": {},
"outputs": [],
"source": [
"def is_match(word, guess):\n",
" for (word_char, (guess_char, score)) in zip(word, zip(guess.word, guess.result)):\n",
" if score == 0 and guess_char in word:\n",
" return False\n",
" if score == 1 and (guess_char not in word or word_char == guess_char):\n",
" return False\n",
" if score == 2 and word_char != guess_char:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8682150c-5cbb-4486-a70a-a5302d82dda6",
"metadata": {},
"outputs": [],
"source": [
"def filter_words(word_list, guesses):\n",
" for guess in guesses:\n",
" word_list = [ word for word in word_list if is_match(word, guess) ]\n",
" return word_list"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "be0d1622-ba90-4d0e-843b-6fe8ae6ea806",
"metadata": {},
"outputs": [],
"source": [
"def find_words_with_chars(char_list):\n",
" chars = set(char_list)\n",
" word_list = get_words()\n",
" return [word for word in word_list if not chars.difference(word)]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "f6068a67-7ee2-46e5-af99-a488fab6089d",
"metadata": {},
"outputs": [],
"source": [
"guesses = [\n",
" Guess('krema', (0,1,1,0,2)),\n",
" Guess('bodur', (0,0,0,0,1)),\n",
" Guess('şerha', (2,2,2,0,2)),\n",
" #Guess('boyca', (0,0,0,0,0)),\n",
" #Guess('bahçe', (0,2,1,0,0)),\n",
"\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "e47e889d-706f-4481-af65-51e24e683ad8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Word(word='süfli', score=262, index=31),\n",
" Word(word='ölgün', score=239, index=71),\n",
" Word(word='gönül', score=239, index=72),\n",
" Word(word='ölçün', score=234, index=73),\n",
" Word(word='sözlü', score=233, index=75),\n",
" Word(word='tifüs', score=231, index=80),\n",
" Word(word='ilgın', score=231, index=82),\n",
" Word(word='özgül', score=222, index=89),\n",
" Word(word='gözlü', score=222, index=91),\n",
" Word(word='öncül', score=221, index=92)]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"find_words_with_unused_chars(get_words(), guesses)[:10]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "594cc643-9cc0-494e-b5d8-b54334a92139",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['şerpa']"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filter_words(get_words(), guesses)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "322aaaab-40b6-462b-9de8-941a67a81f59",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 16,
"id": "390efe9c-27b0-4449-97d6-393ef1e54ace",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['dubar',\n",
" 'dobra',\n",
" 'bordo',\n",
" 'bidar',\n",
" 'darbe',\n",
" 'bedir',\n",
" 'derbi',\n",
" 'bodur',\n",
" 'borda',\n",
" 'barda',\n",
" 'bardo']"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"find_words_with_chars(['b','r', 'd'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7440b409-928e-46f9-b9b9-b3f1a491e850",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3963fbc-5dae-4fdc-9e49-bc2fbb2f38c9",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "67c9dc02-3d76-419e-bf21-dda924e7eea3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 103,
"id": "ee4387c9-9806-432b-9b46-9933cef23531",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 110,
"id": "3424d0df-19a3-4c89-b076-1bd637945f7c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "73ff23ce-6c00-4275-8ae5-c99341dd020a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa70dc3e-a8d8-4108-84a4-0ac5816904ba",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "74161e45-d2d4-4eda-aad7-74ba08f9b8b6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment