Solved

string challenge first and n times last

Posted on 2014-11-14
11
190 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat startup error 5 136
null output 3 43
What browser will run Java? 7 128
Java array 21 87
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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