Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Solving Word Cookies Puzzles: A Python Adventure_创想鸟

Solving Word Cookies Puzzles: A Python Adventure

玩游戏是让大脑从一天的压力中放松下来的一种方式,或者只是从工作中休息一下。然而,有时,游戏本身就会带来压力,所以我认为“word cookies”就是这样,这是一款有趣的益智游戏,你会得到一组打乱的字母,并被要求解决其中包含的单词。

Solving Word Cookies Puzzles: A Python Adventure

随着我在游戏中的进展,解决问题变得越来越困难,几乎没有资源可以帮助我,我多次陷入困境。但是等一下,我用 python 编写代码,为什么我找不到出路呢?这就是 python 语言大放异彩的地方。

现在我如何使用python来解决混乱的问题。我需要一种方法来检查乱序字母中的单词,我将实现分解为简单的步骤。

计划:

获取单词词典来检查打乱的字母。创建一个仅包含 n 个字母单词的 csv,在本例中我创建了一个包含 3 个字母单词到 7 个字母单词的 csv检查 csv 中的某个单词是否其所有字母都包含在打乱的字母中将其保存到自己的单词数列表中,例如,如果一个单词是“age”,那么它将保存到 3 个字母的单词列表中,依此类推。显示结果

让我们开始工作吧:

首先,我在网上搜索并找到了一本可以下载 csv 格式的字典,并将其分成包含每个字母的单独 csv 文件。它看起来像这样:

立即学习“Python免费学习笔记(深入)”;

Solving Word Cookies Puzzles: A Python Adventure

接下来,我有一个 python 代码来从 a-z 检查 csv,并挑选出 3 个字母的单词,并省略带有空格和其他不可用格式的单词。这是同时对 4、5、6 和 7 个字母的单词进行的。

它看起来像这样:

import osimport csvimport reimport pandas as pd# define folder pathsinput_folder = 'c:userszenbookdesktopword lists in csv'output_folder = 'c:userszenbookdesktopword list output'# function to find words of specific lengths in textdef find_words_of_length(text, length):    words = re.findall(r'w+', text)    return [word for word in words if len(word) == length]# initialize dictionaries to store words of each lengthwords_by_length = {3: set(), 4: set(), 5: set(), 6: set(), 7: set()}# loop through all csv files in the input folderfor filename in os.listdir(input_folder):    if filename.endswith('.csv'):        filepath = os.path.join(input_folder, filename)        # read each csv file with a fallback encoding        with open(filepath, 'r', encoding='iso-8859-1') as file:            reader = csv.reader(file)            for row in reader:                # loop through each cell in the row                for cell in row:                    for length in words_by_length.keys():                        words = find_words_of_length(cell, length)                        words_by_length[length].update(words)# save words of each length to separate csv filesfor length, words in words_by_length.items():    output_file = os.path.join(output_folder, f'{length}_letters.csv')    with open(output_file, 'w', newline='', encoding='utf-8') as file:        writer = csv.writer(file)        for word in sorted(words):  # sort words for neatness            writer.writerow([word])print("words have been saved to separate csv files based on their length.")

这是我指定的输出文件夹中的结果:

Solving Word Cookies Puzzles: A Python Adventure

现在有了这个输出文件夹,我只需检查其中的单词,看看它们是否包含在打乱的字母中。这是执行此操作的代码:

import csv# Define the string to check againstcheck_string = 'langaur'# Define the folder path for CSV filesinput_folder = 'C:UsersZenbookDesktopWord list output'# Function to check if all letters in word can be found in check_stringdef is_word_in_string(word, check_string):    # Check if each letter in the word is in the string    for letter in word:        if word.count(letter) > check_string.count(letter):            return False    return True# Check words for 3, 4, 5, 6 and 7-letter CSVsfor length in [3, 4, 5, 6, 7]:    input_file = f'{input_folder}/{length}_letters.csv'    print(f"Length {length}:")    with open(input_file, 'r', encoding='utf-8') as file:        reader = csv.reader(file)        found_words = []        for row in reader:            word = row[0].strip()  # Remove any extra whitespace            if is_word_in_string(word, check_string):                found_words.append(word)        # Print all found words for the given length        for i in found_words:            print(i)

快速细分:

我们从前面的代码中获取输出文件夹,并将其用作上面实际解决方案代码中的输入文件夹。该解决方案的优点在于函数“is_word_in_string”的简单性。我们不必检查打乱的单词中是否包含单个字母,因为这将是为出现多次的字母编写的额外逻辑。

我们只需要检查字典单词中的每个字母是否小于或等于它在打乱单词中出现的次数,然后我们就可以确认字典单词的每个字母是否确实存在在打乱的信件中。

让我们看看代码的实际效果:

Solving Word Cookies Puzzles: A Python Adventure

万岁!现在,当我陷入困境时,我有办法继续前进。它不仅仅是总是欺骗系统,这没有什么乐趣,但是当我真正需要它时,这个解算器就会派上用场。我还可以获得尽可能多的额外单词,这样我就可以填满那个罐子并获得一些很棒的资源。

就是这样。 python 是一种多功能语言,可以自动化快速完成任务。您可以简单地在日常活动中使用它,例如这样的,或者复杂的工作任务,甚至更高级的工作,例如机器学习。找到一个今天要处理的 python 项目。干杯。

嘿,我的名字是 ifedolapo,我是一名前端开发人员和 python 程序员(顺便说一下,我也进行设计)。您可以通过 portfolio 网站了解更多关于我

感谢您浏览这篇文章。

以上就是Solving Word Cookies Puzzles: A Python Adventure的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1354691.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
使用python字典统计CSV数据的步骤和示例代码
上一篇 2025年12月13日 18:44:28
Python Day- 使用循环、任务形成模式
下一篇 2025年12月13日 18:44:36

相关推荐

发表回复

登录后才能评论
关注微信