查看: 518| 回复: 2
收起左侧

[其他] 练基本功recursion (在Jupyter Notebook,python 3)写Boggle(:

Muser 来自APP | 显示全部楼层
本楼:   👍  0
0%
0%
0   👎
全局:   20
100%
0%
0

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
!pip install pyspellchecker
from spellchecker import SpellChecker

#create a spell checker instance
spell = SpellChecker()

#range over each possible combination with for loop and recursion.
def go(input_string, origin, word, letter_count, dic):
  #the base case
  if letter_count == 0 and ''.join(spell.known([word + input_string])) not in dic and len(''.join(spell.known([word + input_string]))) > 1:
    dic[len(dic)] = ''.join(spell.known([word + input_string]))   

  for i in range(letter_count): #add each letter of input_string to each position in word
    #check word + input_string[i]
    if ''.join(spell.known([word + input_string[i]])) not in dic and len(''.join(spell.known([word + input_string[i]]))) > 1:
       dic[len(dic)] = ''.join(spell.known([word + input_string[i]]))      

    go(input_string[:i] + input_string[i+1:],origin, word+input_string[i], letter_count-1, dic) #use recursion to work as nested for loops

  if letter_count == origin:
      for j in dic:
        print(dic[j])
      
#to process input
def boggle(input_string):
   go(input_string, len(input_string), "", len(input_string), {})

boggle("adieu")

一开始用return发现无法会跳出循环就用print了
以下是改进的代码,(刚发现recursion不一定用base case),可能还能改进,先把input_string放在dic里,最后for i in range(len(dic)-1):
print(dic[i+1])

#no base case
!pip install pyspellchecker
from spellchecker import SpellChecker

#create a spell checker instance
spell = SpellChecker()

#range over each possible combination with for loop and recursion.
def go(input_string, origin, word, letter_count, dic):
  
  for i in range(letter_count): #add each letter of input_string to each position in word
    #check word + input_string[i]
    if ''.join(spell.known([word + input_string[i]])) not in dic and len(''.join(spell.known([word + input_string[i]]))) > 1:
       dic[len(dic)] = ''.join(spell.known([word + input_string[i]]))      

    go(input_string[:i] + input_string[i+1:],origin, word+input_string[i], letter_count-1, dic) #use recursion to work as nested for loops

  if letter_count == origin:
      for j in dic:
        print(dic[j])
      
#to process input
def boggle(input_string):
   go(input_string, len(input_string), "", len(input_string), {})

boggle("adieu")

上一篇:design gurus课程质量
下一篇:segment tree比bit简单易懂更普适
 楼主| Muser 2024-11-17 08:23:26 来自APP | 显示全部楼层
本楼:   👍  0
0%
0%
0   👎
全局:   20
100%
0%
0
规则是用给定单词的字母拼出新单词,原词同一个位置的字母不能用两次,长度不定(当然不超过原词长度)
回复

使用道具 举报

 楼主| Muser 2024-11-17 10:02:51 来自APP | 显示全部楼层
本楼:   👍  0
0%
0%
0   👎
全局:   20
100%
0%
0
新人求米188看面经,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

Advertisement
>
快速回复 返回顶部 返回列表