Skip to content
This repository was archived by the owner on Apr 24, 2020. It is now read-only.

Commit d2d0dea

Browse files
jez-wjstac
authored andcommitted
Simplified the answer to exercise 3 in python essentials (#688)
* Changed the implementation of exercise 3 in python essentials to make it faster, more pythonic and use techniques explained in the text * Reincorporated the original solution as the implementation is easy to understand. The short solution has been left as an alternative.
1 parent b04109d commit d2d0dea

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

source/rst/python_essentials.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,14 +1050,24 @@ Here's one solution:
10501050
.. code-block:: python3
10511051
10521052
def f(string):
1053-
count = 0
1054-
for letter in string:
1055-
if letter == letter.upper() and letter.isalpha():
1056-
count += 1
1057-
return count
1053+
count = 0
1054+
for letter in string:
1055+
if letter == letter.upper() and letter.isalpha():
1056+
count += 1
1057+
return count
10581058
f('The Rain in Spain')
10591059
10601060
1061+
An alternative, more pythonic solution, would be:
1062+
1063+
.. code-block:: python3
1064+
1065+
def count_uppercase_chars(s):
1066+
return sum([c.isupper() for c in s])
1067+
1068+
count_uppercase_chars('The Rain in Spain')
1069+
1070+
10611071
Exercise 4
10621072
----------
10631073

0 commit comments

Comments
 (0)