Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

middle three challenge

Posted on 2014-12-04
17
Medium Priority
?
118 Views
Last Modified: 2014-12-18
Hi,

I am working on below challege
http://codingbat.com/prob/p115863

I wrote as below
public String middleThree(String str) {
int len=str.length();
int len2=str.length()/2;

return str.substring(len2-1,len2)+str.substring(len2,len2+1)+str.substring(len2+1,len2+2);
  
}

Open in new window

I passed all tests.

I wonder how to improve my code. Please advise
0
Comment
Question by:gudii9
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 19

Assisted Solution

by:Montoya
Montoya earned 668 total points
ID: 40481406
try this:

public String middleThree(String str) {
  int half = str.length()/2;
  return str.substring(half-1, half+2);
 
}
0
 
LVL 4

Assisted Solution

by:Zsolt Pribusz
Zsolt Pribusz earned 664 total points
ID: 40481998
I prefer one liners :)

public String middleThree(String str) {
  return (str.substring((str.length()-3)/2)).substring(0,3);
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40482815
 return (str.substring((str.length()-3)/2)).substring(0,3);

what is the meaning of above code.  we are doing substring(0,3) on the resultant string of  (str.substring((str.length()-3)/2))?
Please advise
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Montoya
ID: 40482831
str.length is the total length of your string, he is simply subtracting 3 and dividing by 2 (cutting it in half)
0
 
LVL 85

Expert Comment

by:ozo
ID: 40482836
What do you need advice on?
Do you understand (str.length()-3)/2 ?
Do you understand str.substring((str.length()-3)/2) ?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40482847
he is simply subtracting 3 and dividing by 2

string length is varying right. why subtracting 3 why not 5 or 7. i am not clear on that. please advise
0
 
LVL 85

Expert Comment

by:ozo
ID: 40482868
Think about the value of  (str.length()-5)/2 or  (str.length()-7)/2 for varying string lengths.
Do any of them give you the value you want?

Or consider the relationship between (str.length()-3)/2 and (str.length()-1)/2-1
or to str.length()/2-1
0
 
LVL 7

Author Comment

by:gudii9
ID: 40496363
str.length is the total length of your string, he is simply subtracting 3 and dividing by 2 (cutting it in half)

i am confused using inner substring. Substring within other substring.

I see requirent deals only with odd number and atleast of 3 digit so taking -3(not -5, -7 or -1 or -9 etc) makes more sense as below

str.length()-3)/2

can you please break down the inner substring to more understandable steps. please advise
0
 
LVL 85

Expert Comment

by:ozo
ID: 40496388
Maybe it would help if you wrote down a list of different string lengths, and the corresponding substring position you would want in each case.
A relationship should become apparent.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40496520
Think about the value of  (str.length()-5)/2 or  (str.length()-7)/2 for varying string lengths.
Do any of them give you the value you want?

if string length is 3 above results -2/2=1 or -4/2=-2 again which is not what we want


Or consider the relationship between (str.length()-3)/2 and (str.length()-1)/2-1
or to str.length()/2-1

(str.length()-3)/2 ----0/2 which yields  0
Maybe it would help if you wrote down a list of different string lengths, and the corresponding substring position you would want in each case.
if length is 3 i want character at index 1 which is middle and one before and after so xyz(xyz)
if length is 5 i want character at index 2and one before and after so yza (xyzab
if length is 7 i want character at index 3and one before and after so zab(xyzabcd)
if length is 9i want character at index 4and one before and after so  (xyzabcdef
0
 
LVL 85

Expert Comment

by:ozo
ID: 40496649
Do you see any relationship connecting
3 -> 1 and one before and after
5 -> 2 and one before and after
7 -> 3 and one before and after
9 -> 4 and one before and after
to
(str.length()-3)/2
?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40496672
str.length()-3 when you subract by three always get even number like 0,2,4,6 respectively for string lenghts 3,5,7,9

when you devide even number by 2 then reminder always 0 and the value that is yielded is 0,1,2,3
otherthank that i do not see any other particular relationship. please advise
0
 
LVL 85

Expert Comment

by:ozo
ID: 40496687
Can you think of a way to use the values 0,1,2,3 ?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40496879
so on resultant 0,1,2,3 the are doing other substring(0,3)

like
substring(0).substring(0,3)
substring(1).substring(0,3)
substring(2).substring(0,3)
substring(3).substring(0,3)

please advise
0
 
LVL 85

Accepted Solution

by:
ozo earned 668 total points
ID: 40497135
So if (str.length()-3)/2 for lenghts 3,5,7,9 yields the values 0,1,2,3
you seem to be saying that str.substring((str.length()-3)/2).substring(0,3)
could do something desireable.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40497461
yes
0
 
LVL 7

Author Comment

by:gudii9
ID: 40497462
i think now i understand it.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

772 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