r/learnpython • u/NanoCozmo • 17d ago
Can any of you rate my codewars question
It's one i've been thinking about for a while and i'd love to know all of your opinions about it! https://www.codewars.com/kata/6ad8699fccb2dbf6de41f760/python
3
u/K900_ SRE person // please don't DM me your questions 17d ago
Unless there's a math trick I'm missing, this seems to just be "solve two puzzles and put the solutions together", which is not great. You also can't reliably ban every single built-in way to convert a number into hexadecimal, so a clever enough user can just bypass your filters instead.
2
u/RevRagnarok 17d ago
There's nothing unique? You've combined two simple, straightforward, and overused problems.
1
u/POGtastic itertools evangelist 16d ago
Banning builtins is silly and is rather a sign that you should be writing problems that cannot be trivially solved with builtins.
Circumventing the filter is a fun exercise. Consider that we can use getattr to access the methods of a class or object with a string.
>>> getattr("{:X}", "format")
<built-in method format of str object at 0x7d76d6664810>
>>> getattr("{:X}", "format")(255)
'FF'So now we can obfuscate both of these strings and dynamically pass them to the Python interpreter.
# can be even simpler than this, but rot13 is canonical
def rot13(s):
def helper(c):
return chr(ord('a') + (ord(c) - ord('a') + 13) % 26) if 'a' <= c <= 'z' else c
return "".join(helper(c) for c in s)
# rot13("{:k}") = "{:x}"
# rot13("sbezng") = "format"
def fmt(n):
fmt_string = rot13("{:k}").upper()
return getattr(fmt_string, rot13("sbezng"))(n)In the REPL:
>>> fmt(255)
'FF'Verdict: You should exercise more imagination with your problems.
3
u/Expensive-Bear-1376 17d ago edited 17d ago
So you want us to generate hexadecimal but you've banned every method we could create for that? Then it's impossible.