The Alpha Numeric Sum – Java Logical Programs For Practice

The Alpha Numeric Sum – Java Logical Programs For Practice

Question :
The Alpha Numeric Sum You are provided with a string which is a combination of numbers and alphabets.

Based on certain RULES you are supposed to generate a sum for the given string.

Refer to the examples given below for details of the RULES.

Example1:

inputs : 12a23c45z

Solution: Sum should be calculated as follows:

(greater of the digits 1 & 2 )+(value previous to alphabet position of alphabet)+ (greater of the digits 2 & 3 )+(value previous to alphabet position of alphabet)+ (greater of the digits 4 & 5)+(value previous to alphabet * position of alphabet)

= 2+(2*3)+3+(3*6)+5+(5*9)

= 2+6+3+18+5+45

= 79 (This is the final solution which is the sum of the given string)

Note:

1) If the string starts with a alphabet, the previous value will be 0 (see Example2 below)

2) The given string will have only 2 digit numbers. If the string has a number which has greater than 2 digits or less than 2 digits the final sum should be -10 (see Example3 below)

3) The given string will not have alphabets in pairs. There must always be a single alphabet followed by numeric values. If this is not followed in the given input the final sum should be -20.

4) The position of the alphabets are to be calculated starting from the left with 1 being the first position to start.

Example2:

input1: x45z21c87a60e81

Solution

= (0*1)+(5)+(5*4)+(2)+(1*7)+(8)+(7*10)+(6)+(0*13)+(8)

= 0+5+20+2+7+8+70+6+0+8

= 126
Example3:

input1: a345z129c2d

Solution: = -10

Solution of Alpha Numeric Sum

 

2 Comments

  1. Madiha ayub August 28, 2017

Leave a Reply