diff --git a/exercises/capitalize/solution.py b/exercises/capitalize/solution.py index e1c7ae3..eb6fbb2 100644 --- a/exercises/capitalize/solution.py +++ b/exercises/capitalize/solution.py @@ -8,12 +8,9 @@ def solve(s): if __name__ == '__main__': - fptr = open(os.environ['OUTPUT_PATH'], 'w') + with open(os.environ['OUTPUT_PATH'], 'w') as fptr: + s = input() - s = input() + result = solve(s) - result = solve(s) - - fptr.write(result + '\n') - - fptr.close() + fptr.write(result + '\n') diff --git a/exercises/count_substring/solution.py b/exercises/count_substring/solution.py index a53322c..f35f4e7 100644 --- a/exercises/count_substring/solution.py +++ b/exercises/count_substring/solution.py @@ -2,11 +2,7 @@ # coding=utf-8 def count_substring(string, sub_string): - count = 0 - for i in range(len(string)): - if string[i:].startswith(sub_string): - count += 1 - return count + return sum(bool(string[i:].startswith(sub_string)) for i in range(len(string))) if __name__ == '__main__': string = input().strip() diff --git a/exercises/lists/find_the_runner_up_score/solution.py b/exercises/lists/find_the_runner_up_score/solution.py index 7e3d005..4fe7085 100644 --- a/exercises/lists/find_the_runner_up_score/solution.py +++ b/exercises/lists/find_the_runner_up_score/solution.py @@ -5,9 +5,5 @@ n = int(input()) arr = map(int, input().split()) - # remove duplicates - arr = list(dict.fromkeys(arr)) - # sort - arr.sort() - + arr = sorted(dict.fromkeys(arr)) print(arr[-2]) diff --git a/exercises/lists/nested_lists/solution.py b/exercises/lists/nested_lists/solution.py index a71c1cd..dcff0e4 100644 --- a/exercises/lists/nested_lists/solution.py +++ b/exercises/lists/nested_lists/solution.py @@ -1,13 +1,9 @@ #!/usr/bin/env python # coding=utf-8 -scores_stats = [] - -for _ in range(0,int(input())): - scores_stats.append([input(), float(input())]) - +scores_stats = [[input(), float(input())] for _ in range(int(input()))] # Make a unique list of score, sort it and get the lowest second score -second_lowest = sorted(list(set([score for name, score in scores_stats])))[1] +second_lowest = sorted(list({score for name, score in scores_stats}))[1] # Iterate over the list of scores and names and print every match to the # lowest score we got above diff --git a/solutions/loops/refactor_1.py b/solutions/loops/refactor_1.py index 1e30b5b..3a3576f 100644 --- a/solutions/loops/refactor_1.py +++ b/solutions/loops/refactor_1.py @@ -10,10 +10,7 @@ def before_refactor(): mushrooms = [Mushroom('Portabello', False), Mushroom('Oyster', False), Mushroom('Death Cap', True)] - i = 0 - - for mushroom in mushrooms: - i += 1 + for i, mushroom in enumerate(mushrooms, start=1): name = mushroom.name print('%d:"%s"' % (i, name))