Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

middle three challenge

Posted on 2014-12-04
Medium Priority
109 Views
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);

}
``````
I passed all tests.

0
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
• 8
• 6
• 2
• +1

LVL 19

Assisted Solution

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

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);
}
``````
0

LVL 7

Author Comment

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))?
0

LVL 19

Expert Comment

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

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

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

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

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

0

LVL 84

Expert Comment

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

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

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

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

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

LVL 7

Author Comment

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)

0

LVL 84

Accepted Solution

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

ID: 40497461
yes
0

LVL 7

Author Comment

ID: 40497462
i think now i understand it.
0

Featured Post

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether youâ€™re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and â€¦
Suggested Courses
Course of the Month9 days, 7 hours left to enroll