From ef65ca38bb4e21f466f359ca31432f5cc89b2f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sezai=20Burak=20Kantarc=C4=B1?= <45287996+kantarcise@users.noreply.github.com> Date: Sat, 11 Nov 2023 22:53:58 +0300 Subject: [PATCH] ord and enumerate. lower is cool :love: --- .../easy/171_excel_sheet_column_number.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Leet_Code/easy/171_excel_sheet_column_number.py diff --git a/Leet_Code/easy/171_excel_sheet_column_number.py b/Leet_Code/easy/171_excel_sheet_column_number.py new file mode 100644 index 00000000..538601b5 --- /dev/null +++ b/Leet_Code/easy/171_excel_sheet_column_number.py @@ -0,0 +1,21 @@ +# my first approach +class Solution: + def titleToNumber(self, columnTitle: str) -> int: + result = 0 + + j = 1 + for i in range(len(columnTitle)-1, -1, -1): + # result = ord(f"{columnTitle[i].lower()}") * j + result += (ord(columnTitle[i].lower()) - ord('a') + 1) * j + j *= 26 + + return result + +# fast and clean +class Solution: + def titleToNumber(self, columnTitle: str) -> int: + out = 0 + for i, x in enumerate(columnTitle[::-1]): + out += (ord(x) - 64) * 26**i + + return out