[Challenge] Anagrams


#1

It’s about time we had a new challenge!


This one has a few new rules, so if you want to be in with a chance of winning make sure you read them all at the bottom!


Basic Difficulty

Write a function, anagram that takes two words and returns True or False if they are anagrams of each other

  • Function Name : anagram
  • Input : two strings which each have a single word in
  • Output : a boolean (True/False)
  • Example : anagram("dog", "god") => True

Intermediate difficulty

Write a function, anyAnagrams , that will take a sentence as input and return True if that sentence contains at least two words which are anagrams of each other

  • Function Name : anyAnagrams
  • Input : a single sentence in the form of a string
  • Output : a boolean (True/False)
  • Example : anagram("My cat can act") => True
  • Sentence’s can include punctuation and a mixture of upper and lower case letters; all of these should be ignored when checking for anagrams. I.e. to check for an anagram of can’t. you should check only the letters c a n t

Hard Difficulty

Write a function whichAnagrams that works the same as anyAnagrams but instead of returning a boolean it returns a nested list of words that are anagrams of each other.

  • Function Name : whichAnagrams
  • Input : a single sentence in the form of a string
  • Output : a 2D list of anagrams, which each list contains all the words that are anagrams of each other
  • Example : anagram("My cat can act") => [["cat", "act"]]
    anagram("My cat can act. Stressed spelt backwards is desserts!") => [["cat", "act"], ["stressed", "desserts"]]
  • Any repeat words should be ignored i.e. anagram("My cat can act like a cat") => [["cat", "act"]]
  • All words in the output should be free of punctuation and be in lowercase.
  • The lists should be in order of the first anagram and the innner list should also be in the order seen i.e. anagram("my cat and my dog can act but one has a god complex") => [["cat", "act"], ["dog", "god"]]
  • You must explain your submission to be able to win!

Rules

There will be three winners to this challenge:

  • Python 3 Winner - the python entry which runs against our test cases in the quickest time will win (as long as the code is well explained)
  • JavaScript Winner - the JavaScript entry which runs against our test cases in the quickest time will win (as long as the code is well explained)
  • Community Winner - this category is open to any programming language taught on Codecademy. You get to pick the winner! Whichever answer has the most “likes” will win.

Reply to this thread with a link to your code on repl.it and paste your properly formatted code to participate! Don’t just submit your code, remember to explain your solution, too! If you want to be considered as the winner, your function must have the correct name and provide output in the format specified above.

As always solutions using imports to do all the heavy lifting such as itertools will not be considered for the winner.

When including your repl.it link, please just give us the link and not the code preview! Just put a space before your URL so that the preview box doesn’t show – too many preview boxes can make the thread hard to read.

Deadline: Sunday 7th October 12:00-noon Eastern time


Templates to test your code in are available for Python and JavaScript.

Good Luck!!


Challenge is now CLOSED.

You’re still more than welcome to have a go at the challenge.


#2

Do you have a list of test cases so we don’t submit something that won’t work on edge cases but rather work to solve those deficiencies before submitting?


#3

Edited in at the bottom.


#4

Are helper functions allowed?


#5

Yes, not a problem at all as long as you’ve written them yourself


#6

How will the testing take place? Should it be expecting a single string for each call or multiple simultaneous strings?


#7

Everything we need is in the template.


#9

https://repl.it/@kaumaron/AnagramChallengePy


#10

Nice entry @akmauro if you have the time maybe have a go at anyAnagrams again, and see how you could save time as compared to whichAnagrams


#11

Recall that the rules exclude any entries that import libraries.


#12

@alexcraig and @mtf I’ll have another go at anyAnagrams. In the meantime, do re or collections.OrderedDict() count as a heavy lifting library?

The only one I used that I thought was questionable might be collections.Counter() but I just reimplemented that (and it’s an order of magnitude faster). In regards to collections.OrderedDict(), as I noted in my comments dict is implemented in Py3.6 with Cython in the same way and it’s now standard in Py3.7 (as per Guido after doing further research). I just assigned the name OrderedDict to dict() and my script worked without issue demonstrating this.

I’ve always thought of re as a standard solution to any string cleaning problem. The other good solution is to import string but I’m pretty sure that one should count as a heavy lifting library as well. As this is the only one that remains in my code, is re allowed?


#13

I wrote another way but its actually slower by 50%


#14

Considering the extra challenge that it is to do those things without helper libraries, any import is an import, regardless how common or typical. We are writing challenge code, not production code.

Is your time on the order of magnitude -5?


#15

We generally allow libraries that aren’t integral to the challenge. You’ve used re which just cleans strings up, so that would be fine. On the other hand where you used Counter is probably a step to far as it is doing a lot of the work for you. In general my tip would be complete the challenge using as many libraries as you want and then attempt to rewrite it using as few libraries as possible.


#16

A little critique…The challenges need a true overhaul

Currently it sounds like a Homework assignment more than a coding challenge!
Especially with the prize being…“Winning”

There is a disconnect between the level of instruction on site and these challenges as well.
There needs to be more of a Codecademy identity to the challenges.
It could be something as simple as relating the challenges to instruction or making the prize codecademy related (Forum Flair/ a month of pro) etc etc

Right now there is no reason to do a challenge here instead of…a website that rhymes with snackerrank.


#17

Yes, once I removed the imports from collections the time dropped to 10s of microseconds


#18

I’ve removed all of the libraries. I think I was approaching it more from a data science coding challenge rather than software development coding challenge mindset (solve and explain over implement everything ad initio).


#19

For these challenges, considering where we are, libraries are a reach, as are regexs. That’s my observation, of course. Better we see these patterns in exhibit mode, rather than submission. Let learners pick up on it without exceeding the need for the rudiments.


#20

Great stuff, I’ve found that’s often the problem with using libraries. You’re never quite sure what they’re doing so they can really bloat up your program.


#21

Good point, will have a think.

Not quite sure what you mean here? Do you mean the challenges should come with instructions on how to solve them?