Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Reviewing some of the new task-specific Explore GPTs in ChatGPT Part three of an eight-part series of blogs |
---|
Chat GPT now incorporates a range of mini GPTs dedicated to different tasks. We put a few through their paces, so that you don't have to! |
A dedicated GPT for writing Python code! This I had to try.
I've often wondered what is the longest chain of words that you can create where each word is an anagram of the previous one, plus one letter. For example:
A ==> AS ==> SAT ==> TEAS ==> SATED ==> SEDATE ==> DEBATES ==> ?
It's a tricky problem to code, so I thought I'd ask the Python GPT to do it for me:
The database of words could come from here, for example, but I can manage loading that myself.
Here was the initial rumination of the Python tool:
I think this will take a long time computationally, as it's building all possible links between two words which are near-anagrams of each other. It's a good approach, but my question is quite specific, and I think can be solved more quickly with a less sophisticated algorithm.
Here's the Python code generated:
from collections import defaultdict, deque
def preprocess_words(words):
sorted_words = defaultdict(list)
for word in words:
sorted_words[''.join(sorted(word))].append(word)
return sorted_words
def build_graph(words):
graph = defaultdict(list)
for word in words:
for i in range(len(word)):
subword = word[:i] + word[i+1:]
sorted_subword = ''.join(sorted(subword))
if sorted_subword in words:
graph[subword].append(word)
return graph
def find_longest_chain(graph):
max_path = []
memo = {}
def dfs(word, path):
nonlocal max_path
if word in memo:
return memo[word]
max_length = 1
max_path_local = [word]
for neighbor in graph[word]:
next_path = dfs(neighbor, path + [neighbor])
if 1 + next_path[0] > max_length:
max_length = 1 + next_path[0]
max_path_local = [word] + next_path[1]
memo[word] = (max_length, max_path_local)
if max_length > len(max_path):
max_path = max_path_local
return memo[word]
for word in graph:
dfs(word, [word])
return max_path
# Your list of words here
words = [...]
sorted_words = preprocess_words(words)
graph = build_graph(sorted_words)
longest_chain = find_longest_chain(graph)
print("Longest anagram chain:", longest_chain)
All this looks impressive. What the build_graph routine seems to do is to take each word, and add all possible slightly smaller anagrams to its lists. For example, for the word TEAS the program would generate:
AES
AET
EST
AST
All of these would be added to the list, because permutations of these words exist in English (eg SEA, TEA, SET and SAT). The find_longest_chain function can then follow every chain of words and find the chain which is longest.
This is seriously impressive. How on earth can this be possible? Not only has the Python GPT generated a sensible (if perhaps slightly OTT) algorithm, but it also seems to have implemented it correctly too. You'll need to be a Python programmer to sort out any bugs - if there are any - but help like this can save you a great deal of coding time. Awesome. And slightly scary.
Parts of this blog |
---|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.