#đź”’ i need to make my code faster

12 messages · Page 1 of 1 (latest)

supple jolt
#

my task is to do alphametics in python which i did but it didnt pass time tests, any ideas please ?

wise fossilBOT
#

@supple jolt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

supple jolt
#

i need to make my code faster

#

also i cant use set.pop() or iter function

obtuse moon
#

I need the detailed information about what you want

supple jolt
#

«Word arithmetic¹» (sometimes also cryptarithm or algebrogram) is a mathematical

puzzle given as an equation with words, e.g. "SEND + MORE = MONEY".

The goal is to assign a unique² digit to each letter so that after their

substitution, the equality holds. In this case, none of the digits must start with a zero.

In this particular case (and in the decimal system), there is only one possible

solution, which is S → 9, E → 5, N → 6, D → 7, M → 1, O → 0, R → 8, Y → 2.

After this substitution with digits, ⟦9567 + 1085 = 10652⟧ is indeed true.

¹ ‹https://en.wikipedia.org/wiki/Verbal_arithmetic›

² “Unique” means that no two different letters can have the same

digit assigned to them.

The goal of this task is to write a pure function that solves similar puzzles,

in a given positional system (the basis will always be an integer between 2 and 26

inclusive). We will limit ourselves to addition only, we will not consider other arithmetic operations

. The equation at the input is given with two parameters. The left-hand side of the equation

‹lhs› is a list of (at least two) words, with each word given as a list

of letters (single-character strings). The right-hand side of the equation is then always exactly

one word (a list of letters).

The function returns a dictionary that assigns a unique

digit value to each letter of the puzzle. If there are multiple solutions, the function returns any

of them. If there is no solution, the function returns ‹None›.

Hint: Use the backtracking technique. Think back to your

elementary school years – especially the addition of numbers one after another, which always starts

from the right. Here too, a good solution is to gradually try to assign values ​​to

the digits that are as far to the right as possible in each addend, and to end the recursion

in good time when it is clear that the result cannot be achieved.

#

this is my assingment

#

letter = ""
for _ in letters:
letter = _
break
letters.remove(letter)

this for loop could be easily replaced by letter = letters.pop() and it would be much faster, but i cant use it

#

!close

wise fossilBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.