File size: 1,775 Bytes
0d3476b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging
from flask import request, jsonify

from routes import app

logger = logging.getLogger(__name__)

@app.route('/the-clumsy-programmer', methods=['POST'])
def the_clumsy_programmer():
    data = request.get_json()
    logger.info(f"Data received for processing: {data}")
    
    if not data or not isinstance(data, list) or len(data) == 0:
        return jsonify({"error": "Invalid input format"}), 400
    
    results = []
    for input_entry in data:
        dictionary = input_entry.get("dictionary", [])
        mistypes = input_entry.get("mistypes", [])
        
        # Build the patterns dictionary
        patterns_dict = {}
        L = len(dictionary[0]) if dictionary else 0
        for word in dictionary:
            for i in range(L):
                pattern = word[:i] + '*' + word[i+1:]
                patterns_dict.setdefault(pattern, []).append(word)
        
        corrections = []
        for mistyped_word in mistypes:
            found = False
            for i in range(L):
                pattern = mistyped_word[:i] + '*' + mistyped_word[i+1:]
                candidate_words = patterns_dict.get(pattern, [])
                for candidate_word in candidate_words:
                    if candidate_word != mistyped_word:
                        corrections.append(candidate_word)
                        found = True
                        break
                if found:
                    break
            if not found:
                # This case should not happen as per problem constraints
                corrections.append(mistyped_word)
        results.append({"corrections": corrections})
        logger.info(f"Corrections made: {corrections}")
    return jsonify(results)