[Codility/Lesson6]MaxProductOfThree(python)

|1特
import math
def solution(A):
length_A = len(A)
if length_A== 3:
return math.prod(A)
sorted_A = sorted(A)
min_num_multiply = sorted_A[0] * sorted_A[1]
max_num_multiply = sorted_A[length_A - 3] * sorted_A[length_A - 2]
last_num = sorted_A[length_A-1]
answer = last_num * max(min_num_multiply, max_num_multiply) if last_num >= 0 else last_num * min(min_num_multiply, max_num_multiply)
return answer
3つの結果はここです。

例外処理を行ったため、3つの設計テスト例はありません.
そんな結果….🤷♀️
|2トン
import math
def solution(A):
length_A = len(A)
if length_A== 3:
return A[0] * A[1] * A[2]
sorted_A = sorted(A)
#print(sorted_A)
min_num_multiply = sorted_A[0] * sorted_A[1]
max_num_multiply = sorted_A[length_A - 3] * sorted_A[length_A - 2]
last_num = sorted_A[length_A-1]
#print(min_num_multiply, max_num_multiply, last_num)
answer = last_num * max(min_num_multiply, max_num_multiply) if last_num >= 0 else last_num * min(min_num_multiply, max_num_multiply)
return answer
結果はここです。 Reference
この問題について([Codility/Lesson6]MaxProductOfThree(python)), 我々は、より多くの情報をここで見つけました https://velog.io/@zzarbttoo/CodilityLesson6Distinctpython-tlzmpkhoテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol