Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Coding Challenges: Code tips or tricks  RSS feed

 
Sheriff
Posts: 5446
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Barbara Fischer wrote:Thank you Knute that you put in the work - really!


You're very welcome!

This is something what I really appreaciate and I hope that later when I am able to achieve a good command of Java coding - I give it back to the society or our community.

I solved the problem - there were an input and it was needed a Scanner. I created one inside the contstructor a (System.in) method(? - do I tell it correctly? ) and it worked.


System.in is a valid argument for the Scanner constructor, yes.

I have a bunch of different task whic should be somehow solved - and also I need to put and store all knowledge from it. Could you help in that ?

Similiar like this - actually HackerRank challenges.


For every separate task, create a thread and I or someone will help.  If the question is related to the original question, just post it in the same thread -- like you did here.

Glad I could be of service!
 
Marshal
Posts: 61741
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You usually need a Scanner reading System.in, but avoid having more than one. Use one such Scanner per application if possible, and don't close it. Remember Scanners aren't thread‑safe.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently I solved two coding problems. The answears are right - however I would post it here to check: if there is any easier or better way of thnking which I can pick up.


Task Given two strings, a and b, create a bigger string made of the first char of b, the first char of a, the second char of b, the second char of a, and so on. Any leftover chars go to the end of the result.  HINT: Watch out for spaces!  Input Format There are two lines of input, both contain a String.  

Sample input
Hi there  
Sample output
 tHhiere


I created a the code below for this
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward." (Wikipedia)  Task Given a string A, print 'Yes' if it is a palindrome, print 'No' otherwise.  Constraints      A will consist at most lower case english letters.      
Sample input  
anna  
Sample output
 Yes
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Task Given a string, s, and two indices, start and end, print a substring consisting of all characters in the inclusive range from start to end - 1. You'll find the String class's substring method helpful in completing this challenge.  Input Format The first line contains a single string denoting s. The second line contains two space-separated integers denoting the respective values of start and end.
Constraints      1 <= |s| <= 100     0 <= start <= end <= n    
String s consists of letters of the Hungarian alphabet and punctuation marks  
Output Format Print the substring in the inclusive range from start to end - 1.
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Task Given a string and an int n, print a string made of n+1 repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive.  HINT: Watch out for spaces!  
Input Format
There are two lines of input:      
The first line contains a string.    
The second line contains an integer.  
Output Format A single String.
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Barbara Fischer wrote:. . . if there is any easier or better way of thnking which I can pick up. . . . .

Yes.

Don't put all the code into your main() method. Don't call a class Main (only NetBeans does that).
Don't use if‑else if you can use ?: instead. Or, better still, find a ready‑made method.
Don't use += repeatedly on Strings. The + operator is probably the best way to concatenate Strings if you are working in a single statement, but in a loop it leads to slow performance.
You don't need the lengths, believe it or not. It is easier if you know the two lengths, but not necessary. Strings are immutable, but if you go through the String documentation, you will find another class used to create mutable counterparts of a String. That sort of object has methods which you can use to interleave characters in Strings.
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell!

I saved for me later. Also I have some question. Regarding BufferRead class vs. Scanner. But first I discover, so may I find my answers.

I also stumbled upon a question about 2D Array or Hourglass array. Is there any different to read this kind of array or it is just 4x4/8x8 and the same data structure?
But what to do when there is a missing space like:
1 1 1     1 1 0     1 0 0
   1        0          0
1 1 1     1 1 0     1 0 0

Like these.
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a 2D array in Java®. Only arrays of arrays. Please explain more about your query about arrays. I presume that is a separate question from interleaving words. Please explain what you mean about missing spaces. I have never seen the term hourglass array; I think it is misleading.
You know that the standard form of a for loop always iterates exactly the right number of times for the array, even if you have something unusual like a 0‑length array?If the elements in arrays are themselves arrays, it is possible to iterate those arrays with nested for loops, or you can use for‑each loops for iterating, too.
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Barbara Fischer wrote:

You can improve that a lot. I suggest you perfect each exercise before trying the next, rather than submitting code which “works”. You will learn much more like that. Perfect your interleaving program, and you can use similar techniques for substrings.
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I know that a code is enoug good?

I think this can be measured the less code writing ( Now I come up in my mind Robert Green : Clean code maybe back here )
- use less from the computer resources ( for it I need to know things like what you mentioned like String in recursion methods. Could you please suggest webpages ? )
- anything else?
*With all respect I asked this question
 
Barbara Fischer
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean " anything else " ?
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Barbara Fischer wrote:How do I know that a code is enoug good?  . . .

We'll tell you.

Go back to what I said about interleaving Strings. See if we can't improve the code. But I might not reply again tonight.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!