Monday, 17 August 2015

What is the string made of?

Standard
You are given a string, which contains entirely of decimal digits (0-9). Each digit is made of a certain number of dashes, as shown in the image below. For instance 1 is made of 2 dashes, 8 is made of 7 dashes and so on.
digits made of dashes
You have to write a function that takes this string message as an input and returns a corresponding value in terms of a number. This number is the count of dashes in the string message.
Note:
0 consists of 6 dashes, 1 consists of 2 dashes, 2 consists of 5 dashes, 3 consists of 5 dashes, 4 consists of 4 dashes, 5 consists of 5 dashes, 6 consists of 6 dashes, 7 consists of 3 dashes [though the figure shows that 7 consists of 4 dashes but due to minor mistake in the problem please write your solution assuming 7 consists of 3 dashes], 8 consists of 7 dashes, 9 consists of 6 dashes.
Constraints
  • String message will contain at least one digit, but not more than 100
  • Each character in code will be a digit ('0'-'9').

Sample Input
12134
Sample Output
18

Time Limit: 2 sec(s) for all input files combined.
Memory Limit: 256 MB
Source Limit: 1024 KB 
 
Code:
 
class TestClass {
    public static void main(String args[])throws Exception{
      Scanner s=new Scanner(System.in);
      int d[]={6,2,5,5,4,5,6,3,7,6};
      String str;
      int digit,sum=0;
      str=s.next();
      char ch;
      for(int i=0;i<str.length();i++)
      {
          ch=str.charAt(i);
          digit=d[Character.getNumericValue(ch)];
          sum+=digit;
      }
      System.out.println(sum);
    }
}
 

Related Posts:

  • Birthday Party My flatmate, Sayan, once invited all his relatives and friends from all over the city to join him on his birthday party. He is also famous for boast… Read More
  • Minimal Combinatorial Given two integers - n and r, your task is to calculate the combinatorial nCr. nCr = n! / r! (n-r)! The caveat is that you have to writ… Read More
  • Magnificent Fountains I and my flatmate ,Sayan, went to see the magnificient fountains in the Jubilee park on 3rd March.It was the eve of the 184rd Bithday of the late M… Read More
  • Small Factorials Problem You are asked to calculate factorials of some small positive integers. Input An integer T, denoting the number of testcases, followed by T lines, e… Read More
  • Rasta and Tavas Rasta calls a number like a Tavas if and only if 1 ≤ a ≤ n and the sum of all primes (like p) that p | a is exactly equal to k. He asks you to find … Read More

0 comments:

Post a Comment