Decoding Encoded Numbers using Symbolic Representation

MD Jamil Kashem Porosh
3 min readSep 23, 2023

--

In certain scenarios, numbers are represented using a set of symbols instead of the standard decimal system. Each symbol corresponds to a specific value, and numbers are constructed by combining these symbols. The symbols used in this system are: @, #, $, %, &, +, and ~, with each symbol having a unique value. In this blog post, we will explore the problem of decoding numbers encoded in this symbolic representation. We will provide JavaScript and Python solutions to decode the numbers and explain the approach behind them.

Decoding Encoded Numbers using Symbolic Representation

Problem Statement:

Given an encoded string representing a symbolic number, our task is to decode it and convert it into its numerical representation. The encoding follows the following rules:

Symbols and their corresponding values:

  • @ represents 1
  • # represents 5
  • $ represents 10
  • % represents 50
  • & represents 100
  • + represents 500
  • ~ represents 1000
  • Numbers are constructed by combining symbols from largest to smallest, left to right.

Subtraction is used in some cases:

  1. @ can be placed before # and $ to make 4 and 9, respectively.
  2. $ can be placed before % and & to make 40 and 90, respectively.
  3. & can be placed before + and ~ to make 400 and 900, respectively.

Example 1:

Input: s = “$@@@”
Output: 13
Explanation: $ = 10, @@@ = 3. Thus, the decoded number is 10 + 3 = 13.

Example 2:

Input: s = “&$&”
Output: 190

JavaScript Solution:
Here’s an implementation of the problem in JavaScript:

function decodeNumber(s) {
const symbolValues = {
"@": 1,
"#": 5,
"$": 10,
"%": 50,
"&": 100,
"+": 500,
"~": 1000
};

let result = 0;

for (let i = 0; i < s.length; i++) {
if (i < s.length - 1 && symbolValues[s[i]] < symbolValues[s[i + 1]]) {
result += symbolValues[s[i + 1]] - symbolValues[s[i]];
i++;
} else {
result += symbolValues[s[i]];
}
}

return result;
}

// Example usage
const number = decodeNumber("$@@@");
console.log(number); // Output: 13

Python Solution:
And here’s the equivalent implementation in Python:

def decode_number(s):
symbol_values = {
"@": 1,
"#": 5,
"$": 10,
"%": 50,
"&": 100,
"+": 500,
"~": 1000
}

result = 0
i = 0

while i < len(s):
if i < len(s) - 1 and symbol_values[s[i]] < symbol_values[s[i + 1]]:
result += symbol_values[s[i + 1]] - symbol_values[s[i]]
i += 2
else:
result += symbol_values[s[i]]
i += 1

return result

# Example usage
number = decode_number("$@@@")
print(number) # Output: 13

Conclusion:

In this blog post, we addressed the problem of decoding numbers represented in a symbolic form. We provided JavaScript and Python solutions to convert the encoded strings into their numerical representations. By following the rules outlined for encoding and making use of subtraction when necessary, we can accurately decode the numbers. The provided code examples illustrate the step-by-step approach to decoding the encoded strings. By applying these solutions, you can easily decode numbers represented using this symbolic system and perform numerical operations on them.

Note::

👋 Hey there! If you have any burning questions or just want to say hi, don’t be shy — I’m only a message away. 💬 You can reach me at jamilkashem@zoho.com.

🤝 By the way, I think we share the same interest in software engineering — let’s connect on LinkedIn! 💻

--

--

MD Jamil Kashem Porosh

Software Engineer | 📝 Tech Blogger | React.js, React Native, JavaScript, Go, Python. (✉️ jamilkashem@zoho.com)