Solved

tenRun challenge

Posted on 2016-08-08
28
81 Views
Last Modified: 2016-08-16
Hi,

I am working on below challenge

http://codingbat.com/prob/p199484

Psedo code description of approach :
1. create new array of given array size
2. loop given array in for loop
3. check each element value not greater than 10
4. if yes multiply by 10
5 if no give same value from given array to new array

I wrote my code as below

public int[] tenRun(int[] nums) {
  
  
  int len =nums.length;
  int[] arr=new int[len];
  for(int i=0;i<len;i++){
    if(nums[i]<10){
    arr[i]=nums[i]*10;
    }
    else{
        arr[i]=nums[i];
    }
  }
  return arr;
}

Open in new window





I am  not passing all tests
Expected      Run            
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]      [20, 10, 30, 40, 20, 50]      X      
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20]      [10, 10, 20, 20]      OK      
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20]      [10, 10, 90, 20]      X      
tenRun([1, 2, 50, 1]) → [1, 2, 50, 50]      [10, 20, 50, 10]      X      
tenRun([1, 20, 50, 1]) → [1, 20, 50, 50]      [10, 20, 50, 10]      X      
tenRun([10, 10]) → [10, 10]      [10, 10]      OK      
tenRun([10, 2]) → [10, 10]      [10, 20]      X      
tenRun([0, 2]) → [0, 0]      [0, 20]      X      
tenRun([1, 2]) → [1, 2]      [10, 20]      X      
tenRun([1]) → [1]      [10]      X      
tenRun([]) → []      []      OK      
other tests
X      
How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 12
  • 11
  • 5
28 Comments
 
LVL 27

Expert Comment

by:d-glitch
ID: 41748648
CREATE, GREATER THAN, and MULTIPLY  are not mentioned in the Challenge.
Work on the pseudo code, not the code.

How important is the pseudo code?
Do you realize you are actually failing all of the tests?
The only cases that pass are those where the input string is already in final form.

You can not improve your English comprehension by studying Java.
You might be able to improve your Java skills by slowing down and working on one question at a time.
But I've said that several times already and you don't seem to agree.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749569
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]
i thought above should be as below according to challenge description?
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 10]
please advise
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41749827
Are you familiar with the remainder operator? It is also called modulo or modulus.
You can use it in this challenge to determine whether a number is a multiple of ten .  
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html    
http://www.javaranch.com/drive/modulo.html    
https://www.google.com/?gws_rd=ssl#q=modulus+operator+java
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41750337
>>  I thought above should be as below according to challenge description?
      tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 10]

Why do you think that?  What does the challenge say?

What do you think the results of the following should be:
      tenRun([2, 20, 3, 4, 10, 5]) → ???
0
 
LVL 7

Author Comment

by:gudii9
ID: 41750521
For each multiple of 10 in the given array, change all the values following it to be that multiple of 10, until encountering another multiple of 10. So {2, 10, 3, 4, 20, 5} yields {2, 10, 10, 10, 20, 20}.//however 3,4 became 10.10 i thought 5 should become 10
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20]
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20]
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]
i thought above should be as below according to challenge description?
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 10]


What do you think the results of the following should be:
      tenRun([2, 20, 3, 4, 10, 5]) → ???tenRun([2, 20, 10, 10, 10, 10])
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41750541
>>  However 3,4 became 10.10 i thought 5 should become 10
      What multiple of 10 does the 5 follow???


What do you think the results of the following should be:
      tenRun([2, 20, 3, 4, 10, 5]) → ???tenRun([2, 20, 10, 10, 10, 10])

No, that is not correct

For each multiple of 10 in the given array, change all the values following it to be that multiple of 10

Doing it by hand:
tenRun([2, 20, 3, 4, 10, 5]
     2  - no change
    20  - multiple of 10 - no change
     3 - change it 20 since it follows that multiple of 10
     4 - change it 20 since it follows that multiple of 10
    10  - multiple of 10 - no change
     5 - change it 10 since it follows that multiple of 10

Open in new window

 You don't change the values that are multiples of 10.
And you don't change any values before the first multiple of 10.
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41750593
What do you think the results of the following should be:
      tenRun([2, 10, 3, 20, 5, 1001, 10, 2001]) → ???
0
 
LVL 7

Author Comment

by:gudii9
ID: 41750633
>>  However 3,4 became 10.10 i thought 5 should become 10
      What multiple of 10 does the 5 follow???//[b]not sure what it means..10*0 is 0 10*1 is 10 so 5 falls in between 0 and 5..when you say What multiple of 10 does the 5 follow i am not following it?[/b]


What do you think the results of the following should be:
      tenRun([2, 20, 3, 4, 10, 5]) → ???tenRun([2, 20, 10, 10, 10, 10]) 

No, that is not correct

For each multiple of 10 in the given array, change all the values following it to be that multiple of 10

Doing it by hand:
tenRun([2, 20, 3, 4, 10, 5]
     2  - no change
    20  - multiple of 10 - no change
     3 - change it 20 since it follows that multiple of 10
     4 - change it 20 since it follows that multiple of 10
    10  - multiple of 10 - no change
     5 - change it 10 since it follows that multiple of 10//[b][i]but challenge expecting to change it to 20 not 10[/i][/b]

Open in new window

0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41750693
This is the case we are talking about now.  It is one of the original test cases that you did not understand.
      tenRun([2, 10, 3, 4, 20, 5]

>>  However 3,4 became 10.10 i thought 5 should become 10
      What multiple of 10 does the 5 follow???

>> not sure what it means..10*0 is 0 10*1 is 10 so 5 falls in between 0 and 5..when you say
      What multiple of 10 does the 5 follow i am not following it?[/b]

falls between has nothing to do with following

multiple of  does not mean  multiply by

What multiple of 10 does the 5 follow in the array?
                                    is the same as
What multiple of 10 comes before 5 in the array?

The answer in this case is 20.  -- So change the 5 to 20.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41750815

What multiple of 10 does the 5 follow in the array?
                                    is the same as
What multiple of 10 comes before 5 in the array?

The answer in this case is 20.  -- So change the 5 to 20.
still thinking above
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41750973
This is the text of the Challenge, in case you have forgotten:
     For each multiple of 10 (like 0, 10, 20, 30, ...) in the given array
     change all the values following it (in the array) to be that multiple of 10
     until encountering another multiple of 10.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41751159
tenRun([2, 10, 3, 20, 5, 1001, 10, 2001]) → ???    ([2, 10, 10, 20, 20, 20, 10, 10]) → is this answer correct?
1
 
LVL 7

Author Comment

by:gudii9
ID: 41751166
public int[] tenRun(int[] nums) {
  
  
  int len =nums.length;
  int[] arr=new int[len];
  for(int i=0;i<len;i++){
    if((nums[i]/10)==0){
    arr[i]=10*(nums[i]%10);
    }
    else{
        arr[i]=nums[i];
    }
  }
  return arr;
}

Open in new window



Expected      Run            
tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]      [20, 10, 30, 40, 20, 50]      X      
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20]      [10, 10, 20, 20]      OK      
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20]      [10, 10, 90, 20]      X      
tenRun([1, 2, 50, 1]) → [1, 2, 50, 50]      [10, 20, 50, 10]      X      
tenRun([1, 20, 50, 1]) → [1, 20, 50, 50]      [10, 20, 50, 10]      X      
tenRun([10, 10]) → [10, 10]      [10, 10]      OK      
tenRun([10, 2]) → [10, 10]      [10, 20]      X      
tenRun([0, 2]) → [0, 0]      [0, 20]      X      
tenRun([1, 2]) → [1, 2]      [10, 20]      X      
tenRun([1]) → [1]      [10]      X      
tenRun([]) → []      []      OK      
other tests
X      
Your progress graph for this problem


i think now i understood challene trying to fix it and failing due to not able to start from first occurence of 10 multiple but starting from index 0
0
 
LVL 7

Author Comment

by:gudii9
ID: 41751169
how to start for loop from first occurrence of 10 multiple not from i=0 which resolve the issues here
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 27

Expert Comment

by:d-glitch
ID: 41751244
You are still multiplying by 10.
That is wrong.

You have to start at 0 to look for the first multiple of 10.

What is your pseudo code?
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41751256
tenRun([2, 10, 3, 20, 5, 1001, 10, 2001]) → ???    ([2, 10, 10, 20, 20, 20, 10, 10]) → is this answer correct?

Yes, but your code is not even close.

Where is your pseudo code?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41752857
You should use the remainder operator to make your search. See my first comment above here.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41753323
how to use moduls operator for this challenge?

Psedo code description of approach :
1. create new array of given array size
2. loop given array in for loop
3. check each element value modulus is 0(ie reminder)
4. if yes multiply subsequent elements by same previous element value?
5 if no give same value from given array to new array
0
 
LVL 27

Expert Comment

by:rrz
ID: 41753364
how to use moduls operator for this challenge?
You can search for the condition  
nums[i]%10) == 0

Open in new window

that will be true for multiples of ten.
4. if yes multiply subsequent elements by same previous element value?
No. As d-glitch has posted
... MULTIPLY  are not mentioned in the Challenge.
Multiply is an arithmetic operation. If we multiply 3 by 4 the result is 12. The numbers that are multiples of ten are 10,  20 ,30, 40, and so on.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41755187
public int[] tenRun(int[] nums) {
  
  
  int totalLen =nums.length;
  //int[] arr=new int[len];
  int firstVal=0;
  for(int i=0;i<totalLen;i++){
    if((nums[i]%10)==0){
    firstVal=i;
    break;
    }
  }
  
  
    for(int k=firstVal;k<totalLen;k++){
    if((nums[k]%10)==0){
    secondVal=k;
    break;
    }
  }
    
    
  return nums;
}

Open in new window


i tried above approach. I have to print  all values between firstval and secondval which i am not able to figure out how to proceed further esp if there is thirdval and fourthvale etc
0
 
LVL 27

Accepted Solution

by:
d-glitch earned 250 total points
ID: 41755196
Go through the array one element at a time.
Do nothing until you find a multiple of 10.  You need a flag.
Once you find one you will use to replace later elements.  Remember it.
The rest of the array elements are either replaced or used to update replacer.
0
 
LVL 27

Expert Comment

by:rrz
ID: 41755206
Hint: instead of using the name firstVal use the name currentVal.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41755543
let me try
0
 
LVL 7

Author Comment

by:gudii9
ID: 41757297
public int[] tenRun(int[] nums) {
  
  int totalLen =nums.length;
  //int[] arr=new int[len];
  int val=0;
  boolean tenFlag=false;
  for(int i=0;i<totalLen;i++){
    if((nums[i]%10)==0){
    val=nums[i];
    tenFlag=true;
    }
    else if((nums[i]%10)!=0&&tenFlag){
    nums[i]=val;
    
    }
  }
  return nums;
}

Open in new window



Expected

Run



tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20] [2, 10, 10, 10, 20, 20] OK  
tenRun([10, 1, 20, 2]) → [10, 10, 20, 20] [10, 10, 20, 20] OK  
tenRun([10, 1, 9, 20]) → [10, 10, 10, 20] [10, 10, 10, 20] OK  
tenRun([1, 2, 50, 1]) → [1, 2, 50, 50] [1, 2, 50, 50] OK  
tenRun([1, 20, 50, 1]) → [1, 20, 50, 50] [1, 20, 50, 50] OK  
tenRun([10, 10]) → [10, 10] [10, 10] OK  
tenRun([10, 2]) → [10, 10] [10, 10] OK  
tenRun([0, 2]) → [0, 0] [0, 0] OK  
tenRun([1, 2]) → [1, 2] [1, 2] OK  
tenRun([1]) → [1] [1] OK  
tenRun([]) → [] [] OK  

other tests
 OK
now I passed all tests
0
 
LVL 27

Expert Comment

by:rrz
ID: 41757303
Here is my code.
public int[] tenRun(int[] nums) {
    int current = 1;
  for(int i=0;i<nums.length; i++){
    if((nums[i]%10) == 0){
        current = nums[i];
    }
    else{
        if(current != 1) nums[i] = current; 
    }
  }
  return nums;
}

Open in new window

1
 
LVL 27

Expert Comment

by:d-glitch
ID: 41757769
Yes, you can combine the flag and the currVal variables for a more elegant solution.

You can come up with this sort of trick if you focus your thinking on algorithms and optimizations.  You will never come up with them if you keep asking for basic help.

You are currently working on a problem that can use this exact trick.  Good luck.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41757787

You are currently working on a problem that can use this exact trick.

how to combine this?
0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41757794
Look at rrz's code until you understand it.  You may actually have to think about it.  
That is the point.

How many problems are you currently working on?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now