Checksum: The international standard book number (ISBN) is a 10-digit code to sp
ID: 3791332 • Letter: C
Question
Checksum: The international standard book number (ISBN) is a 10-digit code to specifie a book. Among these 10 digits, the right most one is a checksum digit, which is uniquely determined from the other 9 digits, from the condition that d1 + 2d2 + 3d3 + … + 10d10 must be a multiple of 11 (di is the ith digit form the right). The checksum digit should be any value between 0 and 10. For example, suppose a ISBN is 020131452x (x is the checksum), then x must be 5 because 5 is the only value between 0 and 10 which satisfy: 10 * 0 + 9 * 2 + 8 *0 + 7 * 1+ 6 * 3 + 5 * 1 + 4 * 4 + 3 * 5 + 2 * 2 + 1 * x is a multiple of 11.
Given the left most 9 digits of a ISBN, please write a function checkSum(int[] input) to return its checkSum. (Note: input is the given left most 9 digits of a ISBN)
Exercise 2:Sumof Digits: Write a function sumOfDigits(int n) to sum all the digits of a given number
Exercise 3: Check Duplicate: Please fill the function checkDup(int[] arr) to check if the there are any duplicate within the given array.
Exercise 4: Longest Equal Sequence: Please write the function longestSequence(int[] arr): Given an array of integers, return the length of the longest contiguous sequences of equal values.
Exercise 5: Random Array: Please write the function int[] getRandom(int n, int a, int b) to generate an integer array of size n with value between a and b (included).
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
static int checkSum(int[] input)
{
int sum = 0,j = 10;
//digits in array are 10
for(int i = 0; i < input.length ; i++,j--)
{
sum+=j*input[i];
}
j =0;
// System.out.printf("sum = %d ", sum);
while((sum%11) >0)
{
j++;
sum +=1;
}
return j;
}
static int SumofDigits(int number)
{
int sum = 0,rem;
do
{
rem = number%10;
sum+=rem;
number = number/10;
}while(number>0);
return sum;
}
//check if array has duplicate value and return true if yes otherwiese false
static boolean CheckDuplicate(int [] input)
{
int length = input.length;
//for(int i : input)
for(int i = 0; i < length; i++)
{
for(int j = i + 1; j < length-1; j++)
{
if(input[i] == input[j])
{
System.out.printf("Inside true ");
//System.out.printf("%d%d",input[i], input[j]);
return true;
}
}
}
return false;
}
static int longestSequence(int [] num)
{
int longest = 0;
int length = 1;
int i;
int n = num.length;
for (i = 1; i < num.length; i++)
if (num[i] == num[i - 1])
length++;
else
{
if (length > longest)
longest = length;
if (longest > n - 1 - i)
return longest;
length = 1;
}
return (length > longest) ? length : longest;
}
static void getRandom(int []array,int a , int b)
{
Random r = new Random();
int num;
for(int i = 0; i < array.length; i++)
{
num = r.nextInt();
if( num > a && num <b)
array[i] = num ;
else
i = i -1;
}
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int []array = new int[10];
//getRandom(array,0,9);
String s = new String("020131452") ;
// String s = new String("111145557");
for(int i = 0; i< 9; i++)
{
array[i] = s.charAt(i)-48;
}
//print array
System.out.printf("Array contains: ");
for(int i = 0; i< 9; i++)
{
System.out.printf("%d ",array[i]);
}
//testing checksum
System.out.printf(" checksum of number %s = %d ", s,checkSum(array));
//check for sumof digits in a number
int number = 1256;
System.out.printf("sum of digits in number %d = %d ",number,SumofDigits(number));
//check duplicate
if(CheckDuplicate(array) == true)
{
System.out.printf("Array has duplicates ");
}
else
{
System.out.printf("Array has no duplicates ");
}
//check for longest sequesnce
System.out.printf("longestSequence = %d ",longestSequence(array));
}
}
-------------------------------------------------------------------------------------------------------------------------------------
//output
Array contains: 0 2 0 1 3 1 4 5 2
checksum of number 020131452 = 5
sum of digits in number 1256 = 14
Inside true
Array has duplicates
longestSequence = 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.