From c9877a3b0acd08da35b848395c850bb5c17e8e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D8=A3=D8=AD=D9=85=D8=AF=20=D8=A7=D9=84=D9=85=D8=AD=D9=85?= =?UTF-8?q?=D9=88=D8=AF=D9=8A=20=28Ahmed=20El-Mahmoudy=29?= Date: Thu, 26 Feb 2026 00:20:18 +0100 Subject: [PATCH] Use max function instead of looping to find last/longest word also no need to check if word is alphabetic --- ch02-strings/e08b2_last_word_in_file.py | 8 +++----- ch02-strings/e08b3_longest_word_in_file.py | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/ch02-strings/e08b2_last_word_in_file.py b/ch02-strings/e08b2_last_word_in_file.py index 4a4ab72..78b6d59 100755 --- a/ch02-strings/e08b2_last_word_in_file.py +++ b/ch02-strings/e08b2_last_word_in_file.py @@ -9,9 +9,7 @@ def last_word_alphabetically(filename): """ output = '' for one_line in open(filename): - for one_word in one_line.split(): - if not one_word.isalpha(): - continue - if one_word > output: - output = one_word + split_line=one_line.split() + split_line.append(output) + output=max(split_line) return output diff --git a/ch02-strings/e08b3_longest_word_in_file.py b/ch02-strings/e08b3_longest_word_in_file.py index 9557144..c17fada 100755 --- a/ch02-strings/e08b3_longest_word_in_file.py +++ b/ch02-strings/e08b3_longest_word_in_file.py @@ -8,9 +8,7 @@ def longest_word(filename): """ output = '' for one_line in open(filename): - for one_word in one_line.split(): - if not one_word.isalpha(): - continue - if len(one_word) > len(output): - output = one_word + split_line=one_line.split() + split_line.append(output) + output=max(split_line) return output