Calculate number of possible messages from a coded string











up vote
-1
down vote

favorite












Question
Alphabets are assigned numbers like -



a = 1
b = 2
c = 3
.
.
z = 26


Given a number like 1234 as input calculate count of possible different combination of character out of this string.



Example



input "123" 
output = 3 {(1,2,3), (12, 3),(1,23) }

input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1

input = ""
output = 1


Please review this scala code for this problem -



import scala.language.postfixOps
import scala.collection.mutable.HashMap

object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}









share|improve this question




















  • 2




    Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
    – 200_success
    2 days ago












  • fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
    – vikrant
    2 days ago






  • 1




    I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
    – 200_success
    2 days ago










  • This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
    – vikrant
    2 days ago















up vote
-1
down vote

favorite












Question
Alphabets are assigned numbers like -



a = 1
b = 2
c = 3
.
.
z = 26


Given a number like 1234 as input calculate count of possible different combination of character out of this string.



Example



input "123" 
output = 3 {(1,2,3), (12, 3),(1,23) }

input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1

input = ""
output = 1


Please review this scala code for this problem -



import scala.language.postfixOps
import scala.collection.mutable.HashMap

object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}









share|improve this question




















  • 2




    Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
    – 200_success
    2 days ago












  • fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
    – vikrant
    2 days ago






  • 1




    I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
    – 200_success
    2 days ago










  • This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
    – vikrant
    2 days ago













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Question
Alphabets are assigned numbers like -



a = 1
b = 2
c = 3
.
.
z = 26


Given a number like 1234 as input calculate count of possible different combination of character out of this string.



Example



input "123" 
output = 3 {(1,2,3), (12, 3),(1,23) }

input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1

input = ""
output = 1


Please review this scala code for this problem -



import scala.language.postfixOps
import scala.collection.mutable.HashMap

object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}









share|improve this question















Question
Alphabets are assigned numbers like -



a = 1
b = 2
c = 3
.
.
z = 26


Given a number like 1234 as input calculate count of possible different combination of character out of this string.



Example



input "123" 
output = 3 {(1,2,3), (12, 3),(1,23) }

input = "0123"
output = 0 // as 0 is not a valid number for this case a = 1

input = ""
output = 1


Please review this scala code for this problem -



import scala.language.postfixOps
import scala.collection.mutable.HashMap

object Decode extends App {
def validTwoDigit(number: Int) : Boolean = (9 < number) && (27 > number)
def countMessage(message: String, lookup: HashMap[String, Int]): Int = {
if (message.isEmpty) 1
else if (message.head == '0') 0
else if (lookup contains message) lookup(message)
else {
val count = countMessage(message.drop(1), lookup) +
( if (validTwoDigit(message.take(2).toInt)) countMessage(message.drop(2), lookup) else 0 )
lookup += (message -> count)
count
}
}
val message = args(0)
println(message)
println(countMessage(message, HashMap[String, Int]()))
}






programming-challenge interview-questions scala combinatorics dynamic-programming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









vikrant

214




214








  • 2




    Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
    – 200_success
    2 days ago












  • fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
    – vikrant
    2 days ago






  • 1




    I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
    – 200_success
    2 days ago










  • This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
    – vikrant
    2 days ago














  • 2




    Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
    – 200_success
    2 days ago












  • fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
    – vikrant
    2 days ago






  • 1




    I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
    – 200_success
    2 days ago










  • This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
    – vikrant
    2 days ago








2




2




Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago






Is this using a non-English alphabet? What's the 27th letter? For that matter, what do the letters have to do with anything, when the inputs and outputs are both numbers?
– 200_success
2 days ago














fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago




fixed the code. For this problem letter does not bring any value but a variant of this problem asks for printing all possible combinations (not just count), so we have to do that mapping.
– vikrant
2 days ago




1




1




I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago




I suggest that you edit this question to do a better job of describing what the task is that you are solving, and to remove any irrelevant complications.
– 200_success
2 days ago












This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago




This is the way it was asked to me. Please suggest a better alternative. Also isnt excluding redundant details from question is part of problem solving?
– vikrant
2 days ago















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208556%2fcalculate-number-of-possible-messages-from-a-coded-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208556%2fcalculate-number-of-possible-messages-from-a-coded-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei