Solved

string challenge first and n times last

Posted on 2014-11-14
11
183 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 19

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
 
LVL 19

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 19

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 19

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 19

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 19

Expert Comment

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

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
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…
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.

706 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

17 Experts available now in Live!

Get 1:1 Help Now