Algorithm


Problem Name: beecrowd | 3142

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/3142

Excel Bug

By Roger Eliodoro Condras, UFSC-ARA BR Brazil

Timelimit: 1

While our birthday boy was exchanging balloons, he received an email from a professor asking to urgently send some additional statistical data to report on a project in which he participates.

Tobias promptly opened the Excel spreadsheet where the data was saved to make the necessary accounts and send the requested data as quickly as possible. The sooner he finishes this task, the more time he has left to enjoy his birthday.

In Excel, each column in the spreadsheet is indicated by letters. The first column is the letter "A", the second column is the letter "B", the third "C" and so on to "Z", where the next column is "AA". Then comes "AB", "AC", "AD", ..., up to "AZ". Hence "BA", "BB", "BC", ..., and so on to "ZZ". Then comes "AAA", "AAB", ..., until you reach "XFD", which is the last column of the spreadsheet. (Yes, there is one last column! Sorry to destroy the dream of anyone who thought it was infinite).

In Excel, there are also formulas that perform specific operations and can take spreadsheet cells as parameters. One of these formulas is =COL(), which if called without parameters returns the numeric position of the column in which it was called. For example, calling the function =COL() on any line in column “A” would return 1. Calling in column “D” would return 4, in column “Z” it would return 26, in column “AB” it would return 28 and so on .

When trying to perform the necessary operations to recover the requested data, Tobias noticed that the formula =COL() was not working.

Can you help him solve this problem by writing an algorithm that, given the set of letters that identifies the column, returns to its numerical position? As he is in a bit of a hurry to finish this, it is possible that he accidentally typed in some column code that does not exist (outside the “A” - “XFD” range), in which case you should warn him about it.

Input

You will be asked to answer several test cases. Each test case has a line with an S (1 

|S|

 10) string that contains the sequence of letters that identify the column. S is guaranteed to have only uppercase letters ('A' - 'Z').

The entry ends with EOF.

Output

For each test case, print an integer containing the numeric index value for that column. If the column code entered is not within the limits of Excel, print “Essa coluna nao existe Tobias!” (without quotes).

 
Input Sample Output Sample

A
D
Z
ZZZ
AB

1
4
26
Essa coluna nao existe Tobias!
28

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <string.h>
#include <math.h>

int main(int argc, char **argv)
{
    
    char string[20];
    while (~scanf("%s", string))
    {

        unsigned long long sum = 0UL;
        for (int i = strlen(string) - 1, j = 0; i >= 0; --i, ++j)
            sum += (string[i] - 'A' + 1) * (unsigned long long)pow(26, j);

        if (sum  < = 16384)
            printf("%llu\n", sum);
        else
            puts("Essa coluna nao existe Tobias!");
    }

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
A D Z ZZZ AB

Output

x
+
cmd
1 4 26 Essa coluna nao existe Tobias! 28

#2 Code Example with C++ Programming

Code - C++ Programming


#include <bits/stdc++.h>

using namespace std;

int main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);

    string a;

    while(cin >> a){
        if(a.size() > 3)
            cout << "Essa coluna nao existe Tobias!\n";
        else if(a.size() == 1){
            char c = 'A';

            int cont = 1;
            while(c != a[0]){
                c++;
                cont++;
            }

            cout << cont << '\n';
        }else if(a.size() == 2){
            char c = 'A';

            int cont = 1;
            while(c != a[1]){
                c++;
                cont++;
            }   

            int cont1 = 1;
            c = 'A';
            while(c != a[0]){
                c++;
                cont1++;
            }   

            cout << cont + (cont1 * 26) << '\n';
        }else if(a.size() == 3){
            if(a > "XFD")
                cout << "Essa coluna nao existe Tobias!\n";
            else{
                char c = 'A';

                int cont = 1;
                while(c != a[2]){
                    c++;
                    cont++;
                }   

                c = 'A';
                int cont1 = 1;
                while(c != a[1]){
                    c++;
                    cont1++;
                }   
                cont1 *= 26;

                c = 'A';
                int cont2 = 1;
                while(c != a[0]){
                    c++;
                    cont2++;
                }   
                cont2 *= 26*26;

                cout << cont + cont1 + cont2 << '\n';
            }
        }
    }

    return 0;
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
A D Z ZZZ AB

Output

x
+
cmd
1 4 26 Essa coluna nao existe Tobias! 28

#3 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const lines = readFileSync("/dev/stdin", "utf8").split("\n")

function getEXCELColumnPositionFromName(name = "") {
	return name
		.split("")
		.reduce(
			(sum, char, index) => sum + (char.toUpperCase().charCodeAt(0) - 64) * Math.pow(26, name.length - 1 - index), 0
		)
}

const MAX_COLUMN_POSITION = getEXCELColumnPositionFromName("XFD")

function main() {
	const responses = []

	for (const colName of lines) {
		if (colName == "") break // EOF

		if (colName.length > 3)
			responses.push("Essa coluna nao existe Tobias!")
		else {
			const columnPosition = getEXCELColumnPositionFromName(colName)

			if (columnPosition <= MAX_COLUMN_POSITION) responses.push(columnPosition)
			else responses.push("Essa coluna nao existe Tobias!")
		}
	}

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
A D Z ZZZ AB

Output

x
+
cmd
1 4 26 Essa coluna nao existe Tobias! 28
Advertisements

Demonstration


Previous
#3140 Beecrowd Online Judge Solution 3140 Copying and Pasting Code Solution in C, C++, Java, Js and Python
Next
#3145 Beecrowd Online Judge Solution 3145 An unexpected Journey Solution in C, C++, Java, Js and Python