aboutsummaryrefslogtreecommitdiff
path: root/challenge-214/lubos-kolouch/python
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-05-02 14:47:44 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-05-02 14:47:44 +0800
commitfb913ef4e973a29450b92ca89588e839bf6635f6 (patch)
tree94abff55c6006a2b0a4b1da93a5e661753e00fea /challenge-214/lubos-kolouch/python
parentefea8410c80647939ae587536a4f1f66dd2c3a4a (diff)
parent6cc2e38f43011f65d7deaf1e03cf55e4306a53e5 (diff)
downloadperlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.tar.gz
perlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.tar.bz2
perlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-214/lubos-kolouch/python')
-rw-r--r--challenge-214/lubos-kolouch/python/ch-1.py31
-rw-r--r--challenge-214/lubos-kolouch/python/ch-2.py43
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-214/lubos-kolouch/python/ch-1.py b/challenge-214/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..710c81ea42
--- /dev/null
+++ b/challenge-214/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from typing import List
+
+
+def rank_scores(scores: List[int]) -> List[str]:
+ sorted_scores = sorted(scores, reverse=True)
+ ranks = {}
+ rank = 1
+ for i in range(len(sorted_scores)):
+ if i > 0 and sorted_scores[i] == sorted_scores[i - 1]:
+ ranks[sorted_scores[i]] = ranks[sorted_scores[i - 1]]
+ else:
+ ranks[sorted_scores[i]] = rank
+ rank += 1
+ output = []
+ for score in scores:
+ if ranks[score] == 1:
+ output.append('G')
+ elif ranks[score] == 2:
+ output.append('S')
+ elif ranks[score] == 3:
+ output.append('B')
+ else:
+ output.append(str(ranks[score]))
+ return output
+
+
+scores = [1, 2, 4, 3, 5]
+result = rank_scores(scores)
+print(result)
diff --git a/challenge-214/lubos-kolouch/python/ch-2.py b/challenge-214/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..72d7207b50
--- /dev/null
+++ b/challenge-214/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def max_score(numbers):
+
+ def helper(numbers, memo):
+ if not numbers:
+ return 0
+
+ key = tuple(numbers)
+ if key in memo:
+ return memo[key]
+
+ max_score = 0
+ i = 0
+ while i < len(numbers):
+ j = i
+ while j < len(numbers) and numbers[j] == numbers[i]:
+ j += 1
+
+ length = j - i
+ next_numbers = numbers[:i] + numbers[j:]
+ score = length * length + helper(next_numbers, memo)
+ max_score = max(max_score, score)
+ i = j
+
+ memo[key] = max_score
+ return max_score
+
+ return helper(numbers, {})
+
+
+# Test cases
+numbers1 = [2, 4, 3, 3, 3, 4, 5, 4, 2]
+numbers2 = [1, 2, 2, 2, 2, 1]
+numbers3 = [1]
+numbers4 = [2, 2, 2, 1, 1, 2, 2, 2]
+
+print(max_score(numbers1)) # Output: 23
+print(max_score(numbers2)) # Output: 20
+print(max_score(numbers3)) # Output: 1
+print(max_score(numbers4)) # Output: 40