Solved

string challenge first and n times last

Posted on 2014-11-14
11
186 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get weblogic logged in user in java 2 58
session migration servlets 2 26
Problem to error 4 46
javap not working 8 38
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.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

896 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

11 Experts available now in Live!

Get 1:1 Help Now