X Tutup
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions exercises/capitalize/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Comment on lines -11 to +16
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 11-19 refactored with the following changes:

6 changes: 1 addition & 5 deletions exercises/count_substring/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function count_substring refactored with the following changes:


if __name__ == '__main__':
string = input().strip()
Expand Down
6 changes: 1 addition & 5 deletions exercises/lists/find_the_runner_up_score/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 8-12 refactored with the following changes:

This removes the following comments ( why? ):

# sort
# remove duplicates

print(arr[-2])
8 changes: 2 additions & 6 deletions exercises/lists/nested_lists/solution.py
Original file line number Diff line number Diff line change
@@ -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]
Comment on lines -4 to +6
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 4-10 refactored with the following changes:


# Iterate over the list of scores and names and print every match to the
# lowest score we got above
Expand Down
5 changes: 1 addition & 4 deletions solutions/loops/refactor_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function before_refactor refactored with the following changes:

name = mushroom.name
print('%d:"%s"' % (i, name))

Expand Down
X Tutup