#Decrypting numbers to letters

1 messages · Page 1 of 1 (latest)

strong trench
#

This CS assignment is just deciphering numbers associated with characters and printing it out. You are given an input such as:

22-9-22-1-14 12-15-19 16-1-20-15-19 4-5 12-1 16-9-19-3-9-14-1

and you have to convert it for example to the sentence:

VIVAN LOS PATOS DE LA PISCINA

Basically I was able to figure out that by adding 96 to the number Im able to get the unicode which I can the do chr() on to find its corresponding letter. The problem im facing is that in the users input I have to split the whitespace delimiters and the "-" delimiter but this makes it so the spaces arent present and it just prints one big word:

VIVANLOSPATOSDELAPISCINA

Ive tried to figure out how to maintain the spaces but cant seem to find a solution.

#

also this is just me trying to figure out what im going to put in a UDF. This is just a small piece of a bigger problem.

agile crow
#

I could help you in C/C++/Java/PHP but not python

strong trench
#

unfortunately this is a python assignment :/

desert ibex
#

The given input just seems to represent an index for a letter in the alphabet

#

So I would probably take the raw input, split(" ") the. You have a list of numbers for each word, then id split that string by "-" then you have a list of words which are represented by a list of numbers. Then you could replace the numbers by their represented letter and finally stick the word back together.... that's what id do

#

@strong trench

#

Oh...I just saw that you wrote that in your issue lol

#

If you split by whitespaces, it should return a list something like ["22-9-22-1-14", "12-15-...", ...]

#

My idea would be to get an end result like the following

[[22, 9, 22, 1, 14], [12, 15, ...], ...]

agile crow
#
#

;compile java

import java.util.*;
import java.util.regex.*;

public class Main
{
    public static final char[] alphabet_upper = new char[] { '\0', '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', '\0' };
    public static final char[] alphabet_lower = new char[] { '\0', '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', '\0' };

    public static void main (String[] args)
    {
        final String message = "22-9-22-1-14 12-15-19 16-1-20-15-19 4-5 12-1 16-9-19-3-9-14-1";
        final String[] words = message.split("\\s+");

        String decrypted = "";
        
        for(final String word : words)
        {
            if (word.length() > 0)
            {
                final String[] letters = word.split("\\D+");
                int i = 0;
                for(final String letter : letters)
                {
                    final int n = Integer.parseInt(letter);
                    // System.out.println(n);
                    if (n > 0 && n < 27)
                    {
                        if (i < 1)
                        {
                            decrypted += alphabet_upper[n];
                        }
                        else
                        {
                            decrypted += alphabet_lower[n];
                        }
                    }

                    ++i;
                }
            }
            decrypted += " ";
        }
    
        System.out.println(decrypted);
    }
}
silver horizonBOT
#
Program Output
Vivan Los Patos De La Piscina
agile crow
#

The same algorithm should work in other programming languages

#

basically you split by /\s+/ spaces
then split words by /\D+/ non-digits
then convert each digit into integer,
then array bound check and decode
then append decoded char into the decrypted string
then return the decrypted string

#

;compile js

var alphabet_upper = [ '\0', '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', '\0' ];
var alphabet_lower = [ '\0', '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', '\0' ];

var message = "22-9-22-1-14 12-15-19 16-1-20-15-19 4-5 12-1 16-9-19-3-9-14-1";
var words = message.split(/\s+/gm);
var decrypted = "";

var words_length = words && +words.length || 0;

for (var w = 0; w < words_length; ++w)
{
    var word = words[w] || "";
    if (word.length > 0)
    {
//console.log(word); // works
        var letters = word.split(/\D+/gm);
//console.log(letters);
        var letter_length = letters && +letters.length || 0;

        for(var i = 0; i < letter_length; ++i)
        {
            var letter = letters[i] || "";
            var n = parseInt(letter);
//console.log(letter,n);
            if (n > 0 && n < 27)
            {
                if (i < 1)
                {
                    decrypted += alphabet_upper[n];
                }
                else
                {
                    decrypted += alphabet_lower[n];
                }
            }
        }
    }
    decrypted += " ";
}

console.log(decrypted);
silver horizonBOT
#
Program Output
Vivan Los Patos De La Piscina 

agile crow
#

samething in JavaScript 🙂

#
#

most array index starts at 0, so I put an null character padding at 0 and 27 as safe-guards

#

in C/C++ without regexp, I would do lookup table conversion and a big for..loop if/else instead