0
\$\begingroup\$

i have solved a question in codewars:

https://www.codewars.com/kata/5254ca2719453dcc0b00027d/python , here's my solution is it unefficient to use itertools because people are blaming that is cheating using libraries should i try to solve it on other ways and is it this bad way to implement the question this is my first time to solve kata 4kuy a months ago it wasn't possible today is my first one to be solved because of difficulty of the questions.Can you tell me is using libraries considered as a cheating because a lot of people say that is and as a begginer programmer i don't know if i am begginer i am one year into this should i try to focus more on not using libraries?

Thank you.

import itertools
def permutations(s):
      result = itertools.permutations(s,len(s))
      new_list = []
      for item in result:
            new_list.append("".join(item))
      return sorted(set(new_list))
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Could you please edit to add a short summary of the problem statement? You can keep the link as a reference, but your question does need to be complete even if the linked material isn't accessible. Thanks. \$\endgroup\$ Dec 12, 2022 at 16:10
  • \$\begingroup\$ i don't know how to add the text like in question format. \$\endgroup\$
    – mcccuklev
    Dec 12, 2022 at 18:39

1 Answer 1

2
\$\begingroup\$

As to whether or not you should use libraries - depends entirely on what you want to achieve. Perhaps briefly put, if you enjoy the algorithmic challenge of essentially building the algorithm behind itertools.permutations, then do it. If you want to build efficient software and reuse good work other people have already done, do use libraries. Libraries exist because well-crafted and well-tested solutions for general problems are obviously very useful.

As for your solution, it's usually a good idea to use list (or set) comprehension and to avoid explicit for loops. This is usually considered more Pythonic, and can often be more efficient. So we could just do:

import itertools

def permutations(s: str) -> list:
    return list({''.join(p) for p in itertools.permutations(s)})
\$\endgroup\$
2
  • \$\begingroup\$ Good answer! Just to be clear, are sets expected to be sorted out should we call sorted on it like in the original version of the code? \$\endgroup\$
    – SylvainD
    Dec 12, 2022 at 22:09
  • 1
    \$\begingroup\$ @SylvainD Good question! The problem statement says "The order of the permutations doesn't matter". \$\endgroup\$
    – Juho
    Dec 13, 2022 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.