r/pythonhelp • • Feb 03 '24

Making an algorithm to solve a combination lock puzzle

2 Upvotes
def exhaustive_search_4tumblers(puzzle: CombinationProblem) -> list:
"""Simple brute-force search method that tries every combination until
it finds the answer to a 4-digit combination lock puzzle.
"""

# Check that the lock has the expected number of digits
assert puzzle.solution_length == 4, "This code only works for 4 digits"

attempt = CandidateSolution()

for digit1 in puzzle.value_set:
    for digit2 in puzzle.value_set:
        for digit3 in puzzle.value_set:
            for digit4 in puzzle.value_set:
                # Assign current values to attempt.variable_values
                attempt.variable_values = [digit1, digit2, digit3, digit4]

                # Call puzzle's evaluate() method with the attempt
                result = puzzle.evaluate(attempt)

                # If the first value in the result is '1', return the answer
                if result[0] == '1':
                    return attempt.variable_values

# Should never get here
return [-1, -1, -1, -1]

Why is this having an error everytime i run it. would greatly appreciate the assistance.


r/pythonhelp • • Feb 02 '24

search for an element in text file ; extract its timestamp and store in dictionary

1 Upvotes
d = {}

with open('C:/log') as f: 
    lines = f.read().splitlines() 
for line in lines: 
    if 'string_1' in line: 
        time = line[0:21] 
        d['string_1'] = time 
elif 'string_2' in line: 
        time = line[0:21] 
        d['string_2'] = time 
elif 'string_3' in line: 
        time = line[0:21] 
        d['string_3'] = time 
print(d)

ex of text big file lines

[20:25:48.923 -06:00] [thread 19] [Mobility.cpp] [string_1].......

output

{'stirng_1': '[20:25:48.923 -06:00]', 'string_2': '[20:89:48.275 -06:00]'}

I have a big text file. I need to search for multiple strings in the file ,extract their timestamp [20:89:48.275 -06:00] and store in a dictionary. Not all lines have strings only a few lines have the required string in the file. key being string and value being timestamp. I have the code above, how do I make it more efficient?. Mainly how to extract timestamp in a better way? beginner here


r/pythonhelp • • Feb 01 '24

Can someone explain this code for me from Elements of Programming.

1 Upvotes

I am trying to understand the DP solution for a variant of the climbing stairs problem with k steps instead of the regular 2. This is the solution in Elements of programming but I'm having trouble understanding the for loop inside the sum function. I've solved it myself but would like to understand this way. Some solutions in the book are more advanced than what i've seen. Thanks!

def number_of_ways_to_top(top, maximum_step):
    number_of_ways_to_h = [0] * (top + 1)

    def compute_number_of_ways_to_h(h):
        if h <= 1:
            return 1
        if number_of_ways_to_h[h] == 0:
            number_of_ways_to_h[h] = sum(
                compute_number_of_ways_to_h(h - i)
                for i in range(1, min(maximum_step, h) + 1)
            )
        return number_of_ways_to_h[h]

return compute_number_of_ways_to_h(top)

r/pythonhelp • • Feb 01 '24

Code stopped working

1 Upvotes

Why has my code all of a sudden stopped working? It normally did and now it doesnt im gonna send my code but its the “loan” part that isnt working I will send the point which im struggling.

loanconfirm=input('wouldyouliketotakeoutaloan?youmustpayitback')ifloan.lower()=='yes':loan=int(input('howmuchmoneydoyouwanttotakeout?')) balance=balance+loanprint(f'{loan}+{balance}={int(loan+balance)}')