Introduction:
During a class assessment, students are often presented with intriguing problems to test their problem-solving skills. In this blog post, we will tackle one such problem known as the "Sum of Letters." The problem statement goes like this:
Problem Statement:
Given the following rules:
A = 0
B = 1
C = B + A
D = C + B
And so on, we are tasked with finding the sum of the alphabet characters in a given word using a recursive approach.
Sum of Letters
During a class assessment, a teacher asks students to solve the following problem: If A=0, B = 1, C=B+A, D= C+B, and so on, find the sum of the alphabets of a given word.
Input Specification:
input1: String representing any word
Output Specification: Return an integer value which represents the sum of all the alphabets in input1, as per the above-given scenario.
Example 1:
input1: MAN
Output: 377
Explanation:
Given the above scenario, the values of M. A and N are 144, 0 and 233 respectively. Hence, the sum returned is 144+0+233-377
Example 2:
input1: MORE
Output: 2121
Explanation:
Given the above scenario, the values of M. O. R and E are 144, 377,1597 and 3 respectively. Hence, the sum returned is 144-377-1597+3-2121
Recursive Approach:
To solve this problem, we will use a recursive function. Here's a step-by-step explanation of the approach:
Define an array to store the values of each alphabet character based on the given rules. Initialize values for 'A' and 'B'.
Create a recursive function that takes the input word and an index as parameters. The index keeps track of the current character being processed in the word.
In the recursive function, check if the current character is 'A' or 'B'. If so, add the corresponding value to the sum.
If the current character is any other alphabet character, calculate its value using the sum of the two preceding characters, according to the rules, and add it to the sum.
Recursively call the function with the next index until the entire word is processed.
C Code:
// Function to calculate the sum of alphabets in the input word
int letter(char* input1) {
static int alphabetValues[26] = {0}; // Initialize an array to store alphabet values
static int initialized = 0;
if (!initialized) {
// Initialize values for 'A' and 'B'
alphabetValues[0] = 0; // A=0
alphabetValues[1] = 1; // B=1
// Calculate values for other characters based on the rules
for (int i = 2; i < 26; i++) {
alphabetValues[i] = alphabetValues[i - 2] + alphabetValues[i - 1];
}
initialized = 1;
}
int sum = 0;
char currentChar = *input1;
if (currentChar == '\0') {
return 0; // Base case: If we've reached the end of the string, return 0
}
if (currentChar == 'A' || currentChar == 'B') {
sum += alphabetValues[currentChar - 'A'];
} else {
sum += alphabetValues[currentChar - 'C' + 2];
}
return sum + letter(input1 + 1); // Recursively call the function with the next character
}
int main() {
char input1[] = "MAN";
int result = letter(input1);
printf("Output: %d\n", result); // Output: 377
char input2[] = "MORE";
int result2 = letter(input2);
printf("Output: %d\n", result2); // Output: 2121
return 0;
}
C++ Code:
#include <string>
#include <unordered_map>
std::unordered_map<char, int> alphabetValues; // A map to store alphabet values
// Function to initialize alphabet values based on the provided rules
void initializeAlphabetValues() {
alphabetValues['A'] = 0;
alphabetValues['B'] = 1;
char currentChar = 'C';
int currentValue = 1;
int prevValue = 0;
while (currentChar <= 'Z') {
alphabetValues[currentChar] = currentValue + prevValue;
prevValue = currentValue;
currentValue = alphabetValues[currentChar];
currentChar++;
}
}
// Recursive function to calculate the sum of alphabets in the input word
int letter(const std::string& input1, int index) {
if (index >= input1.length()) {
return 0; // Base case: If we've processed the entire string, return 0
}
char currentChar = input1[index];
int sum = 0;
if (alphabetValues.find(currentChar) != alphabetValues.end()) {
sum += alphabetValues[currentChar];
}
return sum + letter(input1, index + 1); // Recursively call the function with the next character
}
int main() {
initializeAlphabetValues(); // Initialize alphabet values
std::string input1 = "MAN";
int result = letter(input1, 0);
std::cout << "Output: " << result << std::endl; // Output: 377
std::string input2 = "MORE";
int result2 = letter(input2, 0);
std::cout << "Output: " << result2 << std::endl; // Output: 2121
return 0;
}
Java Code:
public static int letter(String input1) {
int[] alphabetValues = new int[26];
alphabetValues[0] = 0; // A=0
alphabetValues[1] = 1; // B=1
for (int i = 2; i < 26; i++) {
alphabetValues[i] = alphabetValues[i - 2] + alphabetValues[i - 1];
}
return calculateSum(input1, 0, alphabetValues);
}
private static int calculateSum(String word, int index, int[] alphabetValues) {
if (index >= word.length()) {
return 0; // Base case: If we've processed the entire word, return 0
}
char currentChar = word.charAt(index);
if (currentChar == 'A' || currentChar == 'B') {
return alphabetValues[currentChar - 'A'] + calculateSum(word, index + 1, alphabetValues);
} else {
int newValue = alphabetValues[currentChar - 'C' + 2]; // Calculate value for characters other than 'A' and 'B'
return newValue + calculateSum(word, index + 1, alphabetValues);
}
}
public static void main(String[] args) {
String input1 = "MAN";
int result = letter(input1);
System.out.println("Output: " + result); // Output: 377
String input2 = "MORE";
int result2 = letter(input2);
System.out.println("Output: " + result2); // Output: 2121
}
}
JavaScript Code:
function letter(input1) {
// Object to store alphabet values based on the provided rules
const alphabetValues = {
'A': 0,
'B': 1
};
// Function to initialize alphabet values for other characters
function initializeAlphabetValues(char) {
if (char >= 'A' && char <= 'Z' && !alphabetValues[char]) {
alphabetValues[char] = alphabetValues[String.fromCharCode(char.charCodeAt(0) - 2)] +
alphabetValues[String.fromCharCode(char.charCodeAt(0) - 1)];
}
}
// Recursive function to calculate the sum
function calculateSum(input, index) {
if (index >= input.length) {
return 0; // Base case: If we've processed the entire string, return 0
}
const currentChar = input.charAt(index).toUpperCase();
// Initialize values for the current character (if not already initialized)
initializeAlphabetValues(currentChar);
let sum = 0;
if (alphabetValues[currentChar] !== undefined) {
sum += alphabetValues[currentChar];
}
return sum + calculateSum(input, index + 1); // Recursively call the function with the next character
}
return calculateSum(input1, 0);
}
// Test cases
const input1 = 'MAN';
const result = letter(input1);
console.log(`Output: ${result}`); // Output: 377
const input2 = 'MORE';
const result2 = letter(input2);
console.log(`Output: ${result2}`); // Output: 2121