-1

I' m stuck of last part of my code. I've to sort long string by sum of digit in it. That's part of my code which one I tried to solve it.

import re

@classmethod
    def modified_user_checked_input(cls, user_checked_input: str) -> str:
        return " ".join(sorted([(f"{a[0]}" + str((sum([int(i)for i in a if re.match(r"[1-9]", i)]))) + f"{a[-1]}") \
                for a in user_checked_input.split(" ")])

User can entry that code which is (user_checked_input);

 l99999999l a24124b c140w K125K C20A

The propoer output should be:

l72l c14w a13b K8K C2A

Does anyone know any simple algorythm for that one ?

  • You didn't provide your input as a proper [mre], you didn't provide the exact output you're expecting and you claim your question is not a duplicate without even explaining. Did you even try any of the answers? Can you explain ***why*** it doesn't solve your problem? Did you try ***anything*** to solve your own problem? – Tomerikoo Jun 02 '22 at 15:08
  • Use `sorted`. You need `split()` for the first parameter, an appropriate key function (hint: print `A164122B`[1:-1]) and the `reverse` parameter. – Matthias Jun 02 '22 at 15:13
  • @Tomerikoo I updated question by your tips I m thinking right now this should be more clear to read. – Rafal Jagas Jun 02 '22 at 15:22
  • Your example is not clear at all. Anyway, it should probably be as simple as `lst.sort(key=lambda s: re.search(r"\d+", s), reverse=True)` if you don't care about the letters – Tomerikoo Jun 02 '22 at 15:57
  • @Tomerikoo I thought about that way but look at propadly re.match error – Rafal Jagas Jun 02 '22 at 16:06

0 Answers0