Solved

middle three challenge

Posted on 2014-12-04
17
86 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 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Re-position the objects 7 96
diffSum example 4 23
How to increase Spring boot/Tomcat max file upload size 2 17
MySQL  on Tomcat 8 26
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

911 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

18 Experts available now in Live!

Get 1:1 Help Now