The following python3 function is presented to you with no information about its purpose outside of the test below. Please describe, what you think the function below does

        
  def is_valid(amount, ip, previous_amounts, previous_ips):
    return ip in previous_ips or (amount <= max(previous_amounts) and amount <= 3*mean(previous_amounts))
        
      

The following test cases are present for the function above function.

        
  assert(is_valid(1200, '87.165.238.1', [214,112,155], ['87.165.238.1', '77.30.159.217', '251.9.20.138']))
  assert(is_valid(180, '87.165.238.1', [214,112,155], ['251.9.20.138']))
  assert(not is_valid(215, '87.165.238.1', [214,112,155], ['77.30.159.217', '251.9.20.138']))
  assert(not is_valid(900, '87.165.238.1', [1200,431,15,102, 55, 10, 15], ['77.30.159.217', '251.9.20.138']))
        
      

Please describe below, in your own words, what you believe the function above does:

The following python3 function is presented to you with no information about its purpose outside of the tests below. Read the function and, afterwards, please describe, what you think the function below does

        
  def is_valid(amount, ip, previous_amounts, previous_ips):
    max_previous_amount = max(previous_amounts)
    average_amount = sum(previous_amounts)/len(previous_amounts)

    larger_than_max = amount > max_previous_amount
    far_from_average = amount > 3*average_amount
    unknow_ip = ip not in previous_ips

    if unknow_ip:
        if far_from_average or larger_than_max:
            return False

    return True
            
      

The following test cases are present for the function above function.

        
  assert(is_valid(1200, '87.165.238.1', [214,112,155], ['87.165.238.1', '77.30.159.217', '251.9.20.138']))
  assert(is_valid(180, '87.165.238.1', [214,112,155], ['251.9.20.138']))
  assert(not is_valid(215, '87.165.238.1', [214,112,155], ['77.30.159.217', '251.9.20.138']))
  assert(not is_valid(900, '87.165.238.1', [1200,431,15,102, 55, 10, 15], ['77.30.159.217', '251.9.20.138']))
        
      

Please describe below, in your own words, what you believe the function above does:

You are presented with the following two implementation of the function `is_valid`.

This is a naive algorithm for determining whether or not a transaction should be viewed as valid, based on the amount of the transaction and the ip it came from.

If the ip of the transaction has no previous transactions associate to it, the amount of the transaction is checked. If this amount is 3 times higher than the average of all other transactions or if it's higher than the largest transaction ever recorded, the transaction is considered invalid.

The last snippet of code represents the tests provided to verify this function.

Please analyze them and chose the one which you think is easiest to read and understand, from the perspective of someone completely new to the codebase, which has only the body of the function and the test cases to go by.

        
  # Version 1
  def is_valid(amount, ip, previous_amounts, previous_ips):
    max_previous_amount = max(previous_amounts)
    average_amount = sum(previous_amounts)/len(previous_amounts)

    larger_than_max = amount > max_previous_amount
    far_from_average = amount > 3*average_amount
    unknow_ip = ip not in previous_ips

    if unknow_ip:
        if far_from_average or larger_than_max:
            return False

    return True
        
      

The second implementation (same exact functionality as the first version):

        
  # Version 2
  def is_valid(amount, ip, previous_amounts, previous_ips):
    return ip in previous_ips or (amount <= max(previous_amounts) and amount <= 3*mean(previous_amounts))
        
      

The tests for the function:

        
  assert(is_valid(1200, '87.165.238.1', [214,112,155], ['87.165.238.1', '77.30.159.217', '251.9.20.138']))
  assert(is_valid(180, '87.165.238.1', [214,112,155], ['251.9.20.138']))
  assert(not is_valid(215, '87.165.238.1', [214,112,155], ['77.30.159.217', '251.9.20.138']))
  assert(not is_valid(900, '87.165.238.1', [1200,431,15,102, 55, 10, 15], ['77.30.159.217', '251.9.20.138']))
        
      

Please select and submit your answer below.





Submit