Deployment Parser

In this challenge you are provided with an array of deployment results. Parse the data and determine the number of deployments that were successful or unsuccessful. Also test that the json input is valid based on the parameters listed below, otherwise markt it as an error.

Return the result with the number deployments that were successful, failed or errors encountered during parsing the result.

deployment[n]: an array of JSON string data with two required fields: deployment_id: The valid format is a 12-character string starting with d- and 10 lowercase alphanumeric characters. status: Valid entries are Success and Fail Example:

deployements: [ “{“deployment_id”: “d-123456abcd”, “status”: “Success”}”, “{“deployment_id”: “d-098765efgh”, “status”: “Fail”}” ]

Output: [1, 1, 0]

Here is one approach to parse the deployment results and return successful, failed, and error counts:

 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
import json
import re

def parse_deployments(deployments):
  success = 0
  failed = 0
  errors = 0

  for d in deployments:
    
    try:
      result = json.loads(d)
    except json.JSONDecodeError:
      errors += 1
      continue

    if not re.fullmatch(r'd-[a-z0-9]{10}', result['deployment_id']): 
      errors += 1
      continue

    if result['status'] not in ['Success', 'Fail']:
      errors += 1 
      continue

    if result['status'] == 'Success':
      success += 1
    else:
      failed += 1

  return [success, failed, errors]

The key steps are:

  • Validate JSON format
  • Check deployment ID format
  • Check status is valid
  • Increment success/failed counters
  • Track errors

This validates each input record, categorizes results, and counts outliers. The try-except blocks and regex help handle bad inputs.