Would an alphabetical sequence counter be possible?

3

I'm doing a pascal code where one of my arrays has alphabet values (A to Z) for a personal project of type Questions and Answers / Meanings from A to Z :

>
aux[1]:= 'a';
aux[2]:= 'b';
aux[3]:= 'c';
...
aux[24]:= 'x';
aux[25]:= 'y';
aux[26]:= 'z';

It was fast, but for a moment I found it annoying to have to type them, so I thought:

"" "" "" p>

Something like:

Alfa:Array [ a..z] of 'variavel alfabetica'

Maybe some programs might include some kind of library or function, I do not know, and I could use a counter with Alphabetical sequence instead of numeric     

asked by anonymous 18.02.2016 / 05:04

1 answer

2

Yes , it is possible. Assuming 26 letters in the alphabet, you can create a base-26 system to express numeric values as text. This format is called hexavigesimal bijective .

In this format, 65535 is expressed via string CRXO .

This done, you can now implement an array where keys are hexavigesimal rather than numeric values.

An example implementation in javascript follows:

var app = angular.module('sampleApp', []);

app.controller('SampleController', function($scope, $http) {

  function bijectiveBase26(value) {

    var sequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var length = sequence.length;

    if (value <= 0) return value;
    if (value <= length) return sequence[value - 1];


    var index = (value % length) || length;
    var result = [sequence[index - 1]];

    while ((value = Math.floor((value - 1) / length)) > 0) {
      index = (value % length) || length;
      result.push(sequence[index - 1]);
    }
    return result.reverse().join("");
  }

  $scope.b2Array = {};

  
  //Populando o array com chaves hexavigesimais
  for (i = 0; i < 30; i++) {
    $scope.b2Array[bijectiveBase26(2000 + i)] = String.fromCharCode(65 + i);
  }

  $scope.valor = 65535;

  $scope.$watch("valor",
    function(newValue) {
      $scope.codigo = bijectiveBase26(newValue);
    }
  );
});
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular.js"></script>

<div ng-app="sampleApp">

  <div ng-controller="SampleController">

    Valor:
    <input type='text' ng-model='valor' />
    <br/>Código: {{codigo}}
    <br/>
    <br/>Array: 
    <br/>{{b2Array | json}}


  </div>
</div>
    
18.02.2016 / 07:52