Algorithms, Blockchain and Cloud

Teaching Kids Programming – Converting Spreadsheet Column Titles to Number


Teaching Kids Programming: Videos on Data Structures and Algorithms

Spreadsheets often use this alphabetical encoding for its columns: “A”, “B”, “C”, …, “AA”, “AB”, “AC”, …, “ZZ”, “AAA”, “AAB”, “AAC”, …. Given a string s representing an alphabetical column id, return its column number. For example, given “A”, return 1. Given “AA”, return 27.

Example 1
Input
s = “AA”
Output
27

Multiply by Base

Like converting other base numbers to decimals, we can multiply the current answer by base iteratedly and add the new value.

class Solution:
    def excelColumnTitleToDecimal(self, s):
        a = 0
        for i in s:
            a = a * 26 + (ord(i) - 65 + 1)
        return a

The Excel/Spreadsheet starts with one instead or zero, thus we have to add one during conversion. The time complexity is O(N).

–EOF (The Ultimate Computing & Technology Blog) —

219 words
Last Post: Getting a List of Old Files in a Directory in Java by Comparing the Files Creation Time
Next Post: How to Expire DoFollow Links Automatically (SEO Links Management Function in PHP)

The Permanent URL is: Teaching Kids Programming – Converting Spreadsheet Column Titles to Number (AMP Version)

Exit mobile version