Skip to content

Instantly share code, notes, and snippets.

@tomrockdsouza
Last active June 5, 2023 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomrockdsouza/e96af7e590ef3aa9c09761377c5be8cb to your computer and use it in GitHub Desktop.
Save tomrockdsouza/e96af7e590ef3aa9c09761377c5be8cb to your computer and use it in GitHub Desktop.
# Hennge Senior Software Engineer (Python Challenge)
#######################################################################
# Mission Description
#
# We want to calculate a sum of squares of some integers, excepting negatives
# * The first line of the input will be an integer N (1 <= N <= 100)
# * Each of the following N test cases consists of one line containing an integer X (0 < X <= 100),
# followed by X integers (Yn, -100 <= Yn <= 100) space-separated on the next line
# * For each test case, calculate the sum of squares of the integers excepting negatives,
# and print the calculated sum to the output. No blank line between test cases
# * (Take input from standard input, and output to standard output)
#
# ##Rules
# * Choose your favorite language from either of these: C, Erlang, Go, Python, Ruby
# * Do not use loop statements like while/until/for/each/loop, and goto
#
# ## Sample Input
# 2
# 4
# 3 -1 1 14
# 5
# 9 6 -53 32 16
#
# ##Sample Output
# 206
# 1397
#######################################################################
import sys
def function_recursive(balance_iterations):
if balance_iterations == 0:
return []
elif not (0 < (Xn := int(sys.stdin.readline().strip())) < 101):
raise Exception(f'Invalid X: {Xn}')
elif not (Y_len := len(testcase_array := sys.stdin.readline().strip().split())) == Xn:
raise Exception(f'Invalid array has ({Y_len}) values instead of ({Xn})')
return [testcase_array] + function_recursive(balance_iterations - 1)
def check_Yn(Yn):
if not (-101 < (int_Yn := int(Yn)) < 101):
raise Exception(f'Invalid Yn:({Yn})')
return int_Yn
def main():
int_num_testcases = int(sys.stdin.readline().strip())
if not (0 < int_num_testcases < 101):
raise Exception(f'Invalid N:{int_num_testcases}')
print(
'\n'.join(map(
lambda testcase: str(sum(map(lambda x: 0 if (int_Yn := check_Yn(x)) < 0 else int_Yn ** 2, testcase))),
function_recursive(int_num_testcases)
)),
end=''
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment