Question :
Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions
1. 5 if a perfect square
2. 4 if multiple of 4 and divisible by 6
3. 3 if even number
And sort the numbers based on the weight and print it as follows
<10,its_weight>,<36,its weight><89,its weight>.
Solution :
To solve this program we need some logic to find the give conditions,
Program solution:
Condition 1 : Weight is 5 if given number is perfect square of any number for example : 4,9,16,36
/**
* Check squere.
*
* @param number the number
* @return the int
*/
private static int checkSquere(int number) {
int l_ireturn = 0;
for (int i = 2; i < number; i++) {
int j = number / i;
if (i == j && i*j == number) {
l_ireturn = i;
}
}
return l_ireturn;
}
Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions
1. 5 if a perfect square
2. 4 if multiple of 4 and divisible by 6
3. 3 if even number
And sort the numbers based on the weight and print it as follows
<10,its_weight>,<36,its weight><89,its weight>.
Solution :
To solve this program we need some logic to find the give conditions,
- Weight is 5 if given number is perfect square of any number for example : 4,9,16,36
- Weight is 4 if given number multiple of 4 AND divisible by 6
- Weight is 3 if given number is even number.
Program solution:
Condition 1 : Weight is 5 if given number is perfect square of any number for example : 4,9,16,36
/**
* Check squere.
*
* @param number the number
* @return the int
*/
private static int checkSquere(int number) {
int l_ireturn = 0;
for (int i = 2; i < number; i++) {
int j = number / i;
if (i == j && i*j == number) {
l_ireturn = i;
}
}
return l_ireturn;
}
Condition 2 : Weight is 4 if given number multiple of 4 AND divisible by 6
/**
* Logic multiple divison.
*
* @param number the number
* @return the int
*/
private static int logicMultipleDivison(int number){
int l_ireturn = 0;
int l_iMulti = number/4;
if(l_iMulti*4 == number)
{
l_iMulti = number/6;
if(l_iMulti*6 == number)
{
l_ireturn = 4;
}
}
return l_ireturn;
}
Condition 3 : Weight is 3 if given number is even number.
/**
* Logic even number.
*
* @param number the number
* @return the int
*/
private static int logicEvenNumber(int number)
{
int l_ireturn = 0;
if(number%2 == 1)
{
l_ireturn = 3;
}
return l_ireturn;
}
Do you think i have missed something from the question? . yes we missed it but we have the coding solution below?
In the question we have set of numbers are given as input so we need to iterate all the numbers from given set and check the above condition
Do Iterate the set of given number and find the weight of each number by calling the logic what we have done before..!
private static Map<Integer,Integer> doIterateSet(Set<Integer> l_setInteger)
{
Iterator<Integer> itr = l_setInteger.iterator();
Map<Integer,Integer> l_mapWeightMap = new HashMap<Integer,Integer>();
while (itr.hasNext()) {
int number = itr.next();
int l_iSquere = checkSquere(number);
int l_iMulDiv = logicMultipleDivison(number);
int l_iEven = logicEvenNumber(number);
l_mapWeightMap.put(number, l_iSquere+l_iMulDiv+l_iEven);
}
return l_mapWeightMap;
}
Yes, We are iterate the set but where is the set input, Cool we have the main method, which have the set of integer input...
public static void main(String[] args) {
Set<Integer> l_setNumberSet = new HashSet<Integer>();
l_setNumberSet.add(10);
l_setNumberSet.add(36);
l_setNumberSet.add(54);
l_setNumberSet.add(89);
l_setNumberSet.add(12);
Map<Integer,Integer> l_mapWeightMap = doIterateSet(l_setNumberSet);
System.out.println(l_mapWeightMap);
}
Finally the println method prints the given number and there weights for given condition.
Output :
{54=0, 36=10, 10=0, 89=3, 12=4}
Here i just print the map as output if you want some readable format please do the the change on the main method and iterate the map and print the output whatever you want.
NOTE : Please give your comment to improve my coding standard as well as my functional logics.
/**
* Logic multiple divison.
*
* @param number the number
* @return the int
*/
private static int logicMultipleDivison(int number){
int l_ireturn = 0;
int l_iMulti = number/4;
if(l_iMulti*4 == number)
{
l_iMulti = number/6;
if(l_iMulti*6 == number)
{
l_ireturn = 4;
}
}
return l_ireturn;
}
Condition 3 : Weight is 3 if given number is even number.
/**
* Logic even number.
*
* @param number the number
* @return the int
*/
private static int logicEvenNumber(int number)
{
int l_ireturn = 0;
if(number%2 == 1)
{
l_ireturn = 3;
}
return l_ireturn;
}
Do you think i have missed something from the question? . yes we missed it but we have the coding solution below?
In the question we have set of numbers are given as input so we need to iterate all the numbers from given set and check the above condition
Do Iterate the set of given number and find the weight of each number by calling the logic what we have done before..!
private static Map<Integer,Integer> doIterateSet(Set<Integer> l_setInteger)
{
Iterator<Integer> itr = l_setInteger.iterator();
Map<Integer,Integer> l_mapWeightMap = new HashMap<Integer,Integer>();
while (itr.hasNext()) {
int number = itr.next();
int l_iSquere = checkSquere(number);
int l_iMulDiv = logicMultipleDivison(number);
int l_iEven = logicEvenNumber(number);
l_mapWeightMap.put(number, l_iSquere+l_iMulDiv+l_iEven);
}
return l_mapWeightMap;
}
Yes, We are iterate the set but where is the set input, Cool we have the main method, which have the set of integer input...
public static void main(String[] args) {
Set<Integer> l_setNumberSet = new HashSet<Integer>();
l_setNumberSet.add(10);
l_setNumberSet.add(36);
l_setNumberSet.add(54);
l_setNumberSet.add(89);
l_setNumberSet.add(12);
Map<Integer,Integer> l_mapWeightMap = doIterateSet(l_setNumberSet);
System.out.println(l_mapWeightMap);
}
Finally the println method prints the given number and there weights for given condition.
Output :
{54=0, 36=10, 10=0, 89=3, 12=4}
Here i just print the map as output if you want some readable format please do the the change on the main method and iterate the map and print the output whatever you want.
NOTE : Please give your comment to improve my coding standard as well as my functional logics.
i can't understand the question
ReplyDelete