Solved

string challenge first and n times last

Posted on 2014-11-14
11
197 Views
Last Modified: 2014-11-21
Hi,

I am working on below coding challenge
http://codingbat.com/prob/p174148
i wrote as below
public String nTwice(String str, int n) {
int i=0;
int len=str.length();
String last=str.substring(len-1);
if(len>n){
for(int j=0;j<n;j++)
{
len=len+len;

}
return str.substring(0,1)+len;
}
  return "";
}

Open in new window


My test cases are failing as below
Expected	Run		
nTwice("Hello", 2) → "Helo"	"H20"	X	    
nTwice("Chocolate", 3) → "Choate"	"C72"	X	    
nTwice("Chocolate", 1) → "Ce"	"C18"	X	    
nTwice("Chocolate", 0) → ""	"C9"	X	    
nTwice("Hello", 4) → "Hellello"	"H80"	X	    
nTwice("Code", 4) → "CodeCode"	""	X	    
nTwice("Code", 2) → "Code"	"C16"	X	    
other tests

Open in new window


i would like to know how can improve on my above code.. Not sure if that is what challenge is expecting me to do when it says

Given a string and an int n, return a string made of the first and last n chars from the string. The string length will be at least n.

nTwice("Hello", 2) → "Helo"
nTwice("Chocolate", 3) → "Choate"
nTwice("Chocolate", 1) → "Ce
.Please advise.Thanks in advance
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
  • 6
  • 3
  • 2
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40443691
I'm not sure why you are doing len=len+len;
or str.substring(0,1)+len
but I think you earlier did a similar challenge for n=1 (or was it n=2)
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40443730
nTwice("Hello", 2) → "Helo"
first 2 "He", last 2 "lo"
nTwice("Chocolate", 3) → "Choate"
first 3 "Cho", last 3 "ate"
nTwice("Chocolate", 1) → "Ce"
first 1 "C", last 1 "e"
0
 
LVL 20

Accepted Solution

by:
n2fc earned 250 total points
ID: 40443809
Correct answer below:

public String nTwice(String str, int n) {
int len=str.length();

String first=str.substring(0,n);
String last=str.substring(len-n,len);
return first+last;
}

Open in new window

0
Industry Leaders: 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 20

Expert Comment

by:n2fc
ID: 40443819
Another solution:


public String nTwice(String str, int n) {
   return str.substring(0,n)+str.substring(str.length()-n,str.length());
}

Open in new window

0
 
LVL 20

Expert Comment

by:n2fc
ID: 40443824
Both of the above solutions do the same thing...
The second combines into a single statement, with no intermediate results...


You concatenate the 1st n characters with the last n characters of the supplied string.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40443862
I understand the challenge correctly now. I thought I should display n times from first followed by n times last. If that is challenge how to do it.  Please advise
0
 
LVL 20

Expert Comment

by:n2fc
ID: 40443896
Challenge is to display the 1st & last "n" characters in the given string...

See if you can follow the code I supplied above to accomplish this...

Both check out OK on your "codingbat.com" page...
0
 
LVL 7

Author Comment

by:gudii9
ID: 40444055
public String nTwice(String str, int n) {
int i=0;
int len=str.length();
String last=str.substring(len-1);
if(len>=n){
return str.substring(0,n)+str.substring(len-n);

}
  return "";
}

i modified as above. Does that looks fine. Please advise
0
 
LVL 20

Expert Comment

by:n2fc
ID: 40444131
It works...
BUT...

1) "int i=0"   is unnecessary... Why bother? It is NEVER used!
SAME goes for:
     "String last=str.substring(len-1);"

2) You do NOT have to test for GIVEN conditions!  You test for "if (len>=n)"...
UNNECESSARY... See the problem statement...  
"The string length will be at least n. "   Therefore you do NOT need to test this in your program!


In coding, the rule is always:   KISS= "Keep it SIMPLE, S..."

I would SIMPLIFY as follows:

public String nTwice(String str, int n) {
  int len=str.length();
  return str.substring(0,n)+str.substring(len-n);
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40444193
Since len is only used once, I might further simplify to
public String nTwice(String str, int n) {
  return str.substring(0,n)+str.substring(str.length()-n);
}

Open in new window

0
 
LVL 20

Expert Comment

by:n2fc
ID: 40444215
ozo...
Thanks for your input...

I already mentioned that in my first few posts...
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

717 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