TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

CheckDigit

Problem statement, definition, constraints, and public examples.

Problem Statement

When transmitting data over a network, particularly a wireless network, it is important to verify the integrity of the received data in some way. One common way to do that is to use a check bit or check digit of some sort. In this problem we will use the following algorithm to evaluate a check digit for a String which is known to contain only uppercase letters:
set runningSum = 0
for each character ch in the string{
    convert the character into a number, where A = 1, B = 2, C = 3, etc.;
    add that number times the one based index of the character to runningSum;
}
return the rightmost digit of runningSum;
Your task is to write a method that implements the above algorithm and returns the check digit for a given String, s.

Definition

Class:
CheckDigit
Method:
getCheckDigit
Parameters:
String
Returns:
int
Method signature:
int getCheckDigit(String s)
(be sure your method is public)

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character of s will be an uppercase letter ('A'-'Z').

Examples

  1. "JAVAONE"
    Returns: 6
    Character | Value | Value * Index | Running Sum ----------+-------+---------------+------------ J | 10 | 10 * 1 = 10 | 10 A | 1 | 1 * 2 = 2 | 12 V | 22 | 22 * 3 = 66 | 78 A | 1 | 1 * 4 = 4 | 82 O | 15 | 15 * 5 = 75 | 157 N | 14 | 14 * 6 = 84 | 241 E | 5 | 5 * 7 = 35 | 276 The rightmost digit of 276 is of course 6.
  2. "SANFRANCISCO"
    Returns: 9
  3. "THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG"
    Returns: 8
Back to all problems