Would an alphabetical sequence counter be possible?

Asked

Viewed 64 times

3

I’m making a code in Pascal where one of my arrays has alphabet values (from A to Z) for a personal project of the type Questions and Answers/ Meaning A to Z:

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

It was quick, but for a second I thought it was boring to have to type them, so I thought:

"I know text is text and number is number. But could the program (Pascal or any other) recognize the alphabet as a kind of textual sequence or alphabetic variable ?"

Something like:

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

Maybe some programs could include some sort of library or function, I don’t know, and I could use a sequence counter Alphabetical instead of numerical

1 answer

2


Yes, 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 bijective hexavigesimal.

In this format, the value 65535 is expressed via string SKUNK.

This done, you can now implement a array where keys are hexavigesimais values instead of numerical values.

An example implementation, in javascript, follows below:

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.