Solved

string challenge first and n times last

Posted on 2014-11-14
11
189 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
Eclipse with gitlab 1 37
Can anyone please tell me why i can not find ID  investorCoolGrid inside the jsp page? 1 19
by zero exception 10 39
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 …
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 goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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