Created
June 5, 2021 18:03
-
-
Save johnathaningle/6c68580cc5999fd91c040aabb2259688 to your computer and use it in GitHub Desktop.
Week 5 Lambda Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from typing import List, Optional, Tuple | |
def lambda_handler(event, context): | |
message = "Unable to calculate the cube root" | |
result = get_cuberoots(event) | |
if result is not None: | |
message = str(result) | |
debug_log(context) | |
return { | |
'statusCode': 200, | |
'body': { | |
"calculations": message | |
} | |
} | |
def get_cuberoots(event) -> Optional[List[Tuple[float, float]]]: | |
values: List[Optional[float]] = [ | |
event["x"], | |
event["x2"], | |
event["x3"] | |
] | |
for val in values: | |
if val is None: | |
return None | |
calc = [(x, calculate_cube_root(x)) for x in values] | |
return calc | |
def calculate_cube_root(x: float) -> float: | |
cube_root = 0 | |
if x < 0: | |
x = abs(x) | |
cube_root = x**(1/3)*(-1) | |
else: | |
cube_root = x**(1/3) | |
return cube_root | |
def debug_log(ctx): | |
print(f"Request ID: {ctx.aws_request_id}") | |
print(f"Function Name: {ctx.function_name}") | |
print(f"Milliseconds Remaining: {ctx.get_remaining_time_in_millis()}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment