From 386bab1de1a832aa30dcc552f139c73d14fa97d1 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Fri, 4 Nov 2022 01:59:05 +0300 Subject: [PATCH 1/5] Reduce the complexity of other/scoring_algorithm.py --- other/scoring_algorithm.py | 58 ++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 1e6293f8465c..3038a80e5c7b 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -23,30 +23,28 @@ """ -def procentual_proximity( - source_data: list[list[float]], weights: list[int] -) -> list[list[float]]: - +def get_data(source_data: list[list[float]]) -> list[list[float]]: """ - weights - int list - possible values - 0 / 1 - 0 if lower values have higher weight in the data set - 1 if higher values have higher weight in the data set - - >>> procentual_proximity([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]], [0, 0, 1]) - [[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]] + >>> get_data([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]]) + [[20, 23, 22], [60, 90, 50], [2012, 2015, 2011]] """ - - # getting data data_lists: list[list[float]] = [] for data in source_data: for i, el in enumerate(data): if len(data_lists) < i + 1: data_lists.append([]) data_lists[i].append(float(el)) + return data_lists + +def calculate_each_score( + data_lists: list[list[float]], weights: list[int] +) -> list[list[float]]: + """ + >>> calculate_each_score([[20, 23, 22], [60, 90, 50], [2012, 2015, 2011]], [0, 0, 1]) + [[1.0, 0.0, 0.33333333333333337], [0.75, 0.0, 1.0], [0.25, 1.0, 0.0]] + """ score_lists: list[list[float]] = [] - # calculating each score for dlist, weight in zip(data_lists, weights): mind = min(dlist) maxd = max(dlist) @@ -73,14 +71,44 @@ def procentual_proximity( score_lists.append(score) + return score_lists + + +def generate_final_scores(score_lists: list[list[float]]) -> list[float]: + """ + >>> generate_final_scores([[1.0, 0.0, 0.33333333333333337], [0.75, 0.0, 1.0], [0.25, 1.0, 0.0]]) + [2.0, 1.0, 1.3333333333333335] + """ # initialize final scores final_scores: list[float] = [0 for i in range(len(score_lists[0]))] - # generate final scores for slist in score_lists: for j, ele in enumerate(slist): final_scores[j] = final_scores[j] + ele + return final_scores + + +def procentual_proximity( + source_data: list[list[float]], weights: list[int] +) -> list[list[float]]: + + """ + weights - int list + possible values - 0 / 1 + 0 if lower values have higher weight in the data set + 1 if higher values have higher weight in the data set + + >>> procentual_proximity([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]], [0, 0, 1]) + [[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]] + """ + + data_lists: list[list[float]] = get_data(source_data) + + score_lists: list[list[float]] = calculate_each_score(data_lists, weights) + + final_scores: list[float] = generate_final_scores(score_lists) + # append scores to source data for i, ele in enumerate(final_scores): source_data[i].append(ele) From b3665ed9ceba2211244964349256a62aac9d85c2 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 24 Dec 2022 18:42:31 +0300 Subject: [PATCH 2/5] Increase the --max-complexity threshold in the file .flake8 --- .flake8 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.flake8 b/.flake8 index 77ca7a328a77..b68ee8533a61 100644 --- a/.flake8 +++ b/.flake8 @@ -1,7 +1,7 @@ [flake8] max-line-length = 88 # max-complexity should be 10 -max-complexity = 17 +max-complexity = 19 extend-ignore = # Formatting style for `black` # E203 is whitespace before ':' From 24c61f7e08b9e0036f2c7a79e695dc5eec6d3a49 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 24 Dec 2022 18:44:23 +0300 Subject: [PATCH 3/5] Fix test --- other/scoring_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 3038a80e5c7b..0aa6b5a4eb63 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -26,7 +26,7 @@ def get_data(source_data: list[list[float]]) -> list[list[float]]: """ >>> get_data([[20, 60, 2012],[23, 90, 2015],[22, 50, 2011]]) - [[20, 23, 22], [60, 90, 50], [2012, 2015, 2011]] + [[20.0, 23.0, 22.0], [60.0, 90.0, 50.0], [2012.0, 2015.0, 2011.0]] """ data_lists: list[list[float]] = [] for data in source_data: From c7c345b4776055c6876b0ffe463df6f5e740f967 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 24 Dec 2022 18:50:47 +0300 Subject: [PATCH 4/5] Fix format --- other/scoring_algorithm.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 0aa6b5a4eb63..53f84ea4bf05 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -41,7 +41,8 @@ def calculate_each_score( data_lists: list[list[float]], weights: list[int] ) -> list[list[float]]: """ - >>> calculate_each_score([[20, 23, 22], [60, 90, 50], [2012, 2015, 2011]], [0, 0, 1]) + >>> calculate_each_score([[20, 23, 22], [60, 90, 50], [2012, 2015, 2011]], + ... [0, 0, 1]) [[1.0, 0.0, 0.33333333333333337], [0.75, 0.0, 1.0], [0.25, 1.0, 0.0]] """ score_lists: list[list[float]] = [] @@ -76,7 +77,9 @@ def calculate_each_score( def generate_final_scores(score_lists: list[list[float]]) -> list[float]: """ - >>> generate_final_scores([[1.0, 0.0, 0.33333333333333337], [0.75, 0.0, 1.0], [0.25, 1.0, 0.0]]) + >>> generate_final_scores([[1.0, 0.0, 0.33333333333333337], + ... [0.75, 0.0, 1.0], + ... [0.25, 1.0, 0.0]]) [2.0, 1.0, 1.3333333333333335] """ # initialize final scores From 5c6c26df99136be4c010bd7cebb56e583b6a2e5c Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Wed, 1 Mar 2023 22:25:02 +0300 Subject: [PATCH 5/5] Fix review issues --- other/scoring_algorithm.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/other/scoring_algorithm.py b/other/scoring_algorithm.py index 99c9820915bb..8e04a8f30dd7 100644 --- a/other/scoring_algorithm.py +++ b/other/scoring_algorithm.py @@ -105,11 +105,9 @@ def procentual_proximity( [[20, 60, 2012, 2.0], [23, 90, 2015, 1.0], [22, 50, 2011, 1.3333333333333335]] """ - data_lists: list[list[float]] = get_data(source_data) - - score_lists: list[list[float]] = calculate_each_score(data_lists, weights) - - final_scores: list[float] = generate_final_scores(score_lists) + data_lists = get_data(source_data) + score_lists = calculate_each_score(data_lists, weights) + final_scores = generate_final_scores(score_lists) # append scores to source data for i, ele in enumerate(final_scores):