Question
Must be done in Scala The Luhn algorithm is used to check bank card numbers for simple errors such as mistyping a digit, and proceeds
Must be done in Scala
The Luhn algorithm is used to check bank card numbers for simple errors such as mistyping a digit, and proceeds as follows:
- Consider each digit as a separate number
- Moving right-to-left and beginning at the second last digit, double every other number
- Subtract 9 from each number that is now greater than 9
- Add all the resulting numbers together
- If the total is divisible by 10, the card number is valid
a) Define a function luhnDouble that doubles a digit and subtracts 9 if the result is greater than 9. For example:
scala> luhnDouble(3)
res0: Int = 6
scala> luhnDouble(6)
res1: Int = 3
b) Define a function altMap, which takes as parameter a list of functions (instead of the one function for map) to apply to a list. altMap applies the functions in the functions list to the elements of the list in order. In other words, it applies the first function to the first element, the second function to the second, and so on, until it is time to go back to applying the first function to the next element. For example, altMap (List(_ + 10, _ + 100), List(0, 1, 2, 3, 4)) evaluates to List(10, 101, 12, 103, 14).
Do not use built-in higher-order functions to implement altMap.
c) Define function luhn for checking the validity of card numbers of arbitrary lengths using altMap. luhn should accept a list of Int values to represent the digits of the card.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started