-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4558 from Sal4md3r/solucion-eg-excel
Reto #[32] - [Dart]
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Retos/Reto #32 - LA COLUMNA DE EXCEL [Media]/dart/Sal4md3r.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import 'dart:math'; | ||
|
||
void main() { | ||
const letters = [ | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I', | ||
'J', | ||
'K', | ||
'L', | ||
'M', | ||
'N', | ||
'O', | ||
'P', | ||
'Q', | ||
'R', | ||
'S', | ||
'T', | ||
'U', | ||
'V', | ||
'W', | ||
'X', | ||
'Y', | ||
'Z' | ||
]; | ||
|
||
int excelColumn(String column) { | ||
int result = 0; | ||
|
||
// Si la columna es unica devuelve la letra en la posicion indicada | ||
if (column.split('').length == 1 && | ||
letters.contains(column.toUpperCase())) { | ||
return letters.indexOf(column.toUpperCase()) + 1; | ||
} | ||
|
||
// Si son varias letras divide el string para obtener los caracteres | ||
List arrayResult = column.split(''); | ||
|
||
// Itera el arreglo para obtener la suma de las posiciones | ||
for (int i = 0; i < arrayResult.length; i++) { | ||
// Obtiene el indice de la letra dentro del arreglo de letras | ||
int indx = letters.indexOf(arrayResult[i]); | ||
|
||
// Obtiene el exponente de acuerdo a la posicion del elemento dentro del arreglo | ||
// Formula: (26 ^ (array.length - [indice + 1 ]); | ||
num indexExp = pow(26, arrayResult.length - (i + 1)); | ||
|
||
//Acumula el resultado del indice de la letra por el exponente correspondiente | ||
//en la variable result | ||
result += (indx + 1) * indexExp.toInt(); | ||
} | ||
|
||
// Devuelve el resultado de la suma de las letras de acuerdo a su posicion para | ||
// dar el valor de la columna | ||
return result; | ||
} | ||
|
||
print(excelColumn('A')); | ||
print(excelColumn('Z')); | ||
print(excelColumn('AA')); | ||
print(excelColumn('CA')); | ||
} |