0

Pig Latin

Can anyone explain to me the pig Latin question from the coad coach

23rd Nov 2024, 6:05 AM
Arnav
Arnav - avatar
20 ответов
+ 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);
23rd Nov 2024, 12:51 PM
Jerry Hobby
Jerry Hobby - avatar
+ 5
Wong Hei Ming yeah i get it now Thanks
23rd Nov 2024, 8:59 AM
Arnav
Arnav - avatar
+ 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.
23rd Nov 2024, 9:02 AM
Bob_Li
Bob_Li - avatar
+ 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
23rd Nov 2024, 6:13 AM
Aysha
Aysha - avatar
+ 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.
23rd Nov 2024, 8:38 AM
Bob_Li
Bob_Li - avatar
+ 3
Bob_Li I did try it twice but yeah you too are right problem solving is the most essential skill for a coder 👍
23rd Nov 2024, 8:58 AM
Arnav
Arnav - avatar
+ 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.
23rd Nov 2024, 8:37 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Jerry Hobby Yess did it thanks.A trim func was all I needed 🙏
23rd Nov 2024, 1:07 PM
Arnav
Arnav - avatar
+ 2
Bob_Li Oh but from where do we get this whitespace in our word.I dont get it
23rd Nov 2024, 2:07 PM
Arnav
Arnav - avatar
+ 2
Bob_Li aight gotchu thanks
23rd Nov 2024, 3:53 PM
Arnav
Arnav - avatar
+ 2
Dammnn thats cool ngl Bob_Li
23rd Nov 2024, 4:46 PM
Arnav
Arnav - avatar
+ 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
23rd Nov 2024, 12:32 PM
Arnav
Arnav - avatar
+ 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 "; }
23rd Nov 2024, 1:12 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li Yes that too can be done
23rd Nov 2024, 1:14 PM
Arnav
Arnav - avatar
+ 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.
23rd Nov 2024, 1:47 PM
Arnav
Arnav - avatar
+ 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
23rd Nov 2024, 2:22 PM
Bob_Li
Bob_Li - avatar
+ 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 "); } }
23rd Nov 2024, 4:33 PM
Bob_Li
Bob_Li - avatar
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 "; }
23rd Nov 2024, 1:26 PM
Bob_Li
Bob_Li - avatar
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); } }
23rd Nov 2024, 1:58 PM
Bob_Li
Bob_Li - avatar
0
K
24th Nov 2024, 3:12 PM
atheist RSP
atheist RSP - avatar