+ 1
Pig Latin
Can anyone explain to me the pig Latin question from the coad coach
20 Respostas
+ 2
Arnav,
This part of the code locates a WHITESPACE character and then performs logic. At this point you have found your word plus a whitespace. So when you do your substr command, you have to back it up one spot to eliminate the whitespace.
ch = s.charAt(i);
if(Character.isWhitespace(ch))
Hint: The solution is on this line. How can you change this line to copy one less character. :)
word = s.substring(start,i+1);
+ 5
Wong Hei Ming yeah i get it now Thanks
+ 4
Arnav
yes, thinking about the problem and trying your own methods, especially if they don't work, is where most of the learning happens.
you can post your failed attempt and members here will guide you through a solution, or point out where you are wrong.
+ 3
All the questions in the code coach have been explained in very simple manner along the inputs and outputs. Kindly read it carefully. Try to attempt and incase you get stuck feel free to post of questions along the copy of code pasted in the code playground so we can guide you
+ 3
You try to think of a solution by yourself, from what you have learned.
Coding is problem solving. Nobody is hiring a programmer who can't solve problems.
To solve a problem, you have to first be able to understand it.
Now, are you stuck in the 'understanding the problem' part or in the 'coming up with the solution' part?
I'm not trying to be mean or harsh, but in taking the easy path, you can finish all the courses by searching for readymade solutions, but you will only end up learning nothing and wasting your time.
+ 3
Bob_Li I did try it twice but yeah you too are right problem solving is the most essential skill for a coder š
+ 2
Link to code coach
https://www.sololearn.com/coach/16?ref=app
The quiz already provide a sample input and output.
In case you don't want to leave this page, I give you another example (to simplify things, all in lowercase).
Input -> boldly to code where no man has coded before
Output -> oldlybay otay odecay hereway onay anmay ashay odedcay eforebay
Explanation
For each word in the sentence, move the first character to the end of the word, then append 'ay' to it.
+ 2
Jerry Hobby Yess did it thanks.A trim func was all I needed š
+ 2
Bob_Li Oh but from where do we get this whitespace in our word.I dont get it
+ 2
Bob_Li aight gotchu thanks
+ 2
Dammnn thats cool ngl Bob_Li
+ 1
import java.util.*;
public class Program
{
public static void main(String[] args) {
String s = "";
String word = "";
int start = 0;
char first = '\u0000';
char ch= '\u0000';
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
s = s+ " ";
String snew = "";
for(int i = 0;i<s.length();i++)
{
ch = s.charAt(i);
if(Character.isWhitespace(ch))
{
word = s.substring(start,i+1);
start = i+1;
first = word.charAt(0);
word = word.substring(1);
word = word+first+"ay"+" ";
snew +=word;
}
}
System.out.println(snew);
}
}
Here arises the problem I am done with everything expect it leaves a whitespace between the last three words for eg - if the word was Hello it outputs ello hay instead of ellohay.How do i fix it now Bob_Li Wong Hei Ming
+ 1
you can also shorten it a bit by not reassigning word and just directly concatenating to snew. "ay"+" " can also be combined into "ay "
if(Character.isWhitespace(ch))
{
word = s.substring(start,i+1);
start = i+1;
first = word.charAt(0);
// to this:
snew += word.substring(1).trim()+first+"ay ";
}
+ 1
Bob_Li Yes that too can be done
+ 1
Bob_Li s.substring(start,i) will not take the last alphabet of word so we gotta do (start,i+1) everything's else is right.
+ 1
it's the character between the words. Your code was including it with i+1. not adding the 1 trims it off.
Java is really verbose. my Python solution was one line.
print(*(w[1:]+w[:1]+'ay' for w in input().split()))
anyway, here is what Jerry Hobby means by not using i+1
https://sololearn.com/compiler-playground/c5yRxhqXSqSr/?ref=app
+ 1
Arnav here is the shortest solution I was able to come up with. It was a good chance to brush up on my rusty Java...
methods used:
For-Each Loop, split(), substring(), charAt()
also directly printed each word so no string concatenation was required.
import java.util.*;
public class Program{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
for(String w: sc.nextLine().split(" "))
System.out.print(w.substring(1) +
w.charAt(0) + "ay ");
}
}
0
Arnav
what Jerry Hobby suggested is even simpler, no need for trim.
if(Character.isWhitespace(ch))
{
word =
s.substring(start,i);
start = i+1;
first = word.charAt(0);
snew +=
word.substring(1) +
first + "ay ";
}
0
Arnav, that's the idea. The last character is the whitespace. We don't want it. So we trim it at this step.
This code passes the test:
import java.util.*;
public class Program
{
public static void main(String[] args) {
String s = "";
String word = "";
int start = 0;
char first;
char ch;
Scanner sc = new Scanner(System.in);
s = sc.nextLine();
s = s+ " ";
String snew = "";
for(int i = 0;i<s.length();i++)
{
ch = s.charAt(i);
if(Character.isWhitespace(ch))
{
word =
s.substring(start,i);
start = i+1;
first = word.charAt(0);
snew +=
word.substring(1) +
first + "ay ";
}
}
System.out.println(snew);
}
}
0
K