Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

From the book Think Like a Programmer (emphasis mine)

The Luhn formula is a widely used system for validating identification numbers. Using the original number, double the value of every other digit. Then add the values of the individual digits together (if a doubled value now has two digits, add the digits individually). The identification number is valid if the sum is divisible by 10. Write a program that takes an identification number of arbitrary length and determines whether the number is valid under the Luhn formula. The program must process each character before reading the next one.

In a broader sense, what does it mean to process each character before reading the next one? What does this look like syntactically? What does the opposite mean and what does it look like syntactically?

Update:

For some background, here's my attempt at the problem (in JavaScript):

Array.prototype.sum = function() {
    var sum = 0,
        itemCount = this.length,
        i;

    for(i = 0; i <= itemCount - 1; i++) {
        sum += this[i];
    }

    return sum;
}

function validateNumberWithLuhnForumla(number) {
    var numbersArray = number.toString().split(''),
        numberCount = numbersArray.length,
        i = 0,
        isValid = false,
        doubledDigit,
        checksum,
        numbersToSum = [];


    for(i = 0; i <= numberCount - 1; i++) {
        if(i % 2 > 0 && i > 0) {
            doubledDigit = numbersArray[i] * 2;
            if(doubledDigit > 9) {
                numbersToSum.push(1, (doubledDigit - 10));
            } else {
                numbersToSum.push(doubledDigit);
            }
        }
    }

    checkSum = numbersToSum.sum();

    if(checkSum % 10 === 0) {
        isValid = true;
    }

    return isValid;
}
share|improve this question
2  
foreach(c in input)state=calcSomething(state, c); –  ratchet freak May 18 '13 at 19:34
add comment

2 Answers

up vote 3 down vote accepted

It means that you're allowed to retain the value of one character in memory only until the next input operation happens. This is an idealization of the common practical constraint that an algorithm must be able to process arbitrary input without requiring arbitrary amounts of RAM. A simple practical test is: does your solution work with only a single char variable?

share|improve this answer
add comment

The opposite would be for example reading the whole identification number into a string, and then making multiple passes over the digits of the identification number to calculate the Luhn checksum. The question is asking you to calculate the checksum without looking back to previous digits.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.