Solved

middle three challenge

Posted on 2014-12-04
17
100 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 2
  • +1
17 Comments
 
LVL 19

Assisted Solution

by:Montoya
Montoya earned 167 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 166 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 84

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 84

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 84

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 84

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 84

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 84

Accepted Solution

by:
ozo earned 167 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Part One of the two-part Q&A series with MalwareTech.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 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