• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

what is the best way to do shifting the element in an array

 
Ranch Hand
Posts: 814
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am stuck with this popular question on shifting the element(s) in an array like forever....and till now I am still not confident to go for a coding test.

So, there were lots of amazing solutions offered by the most creative people I have seen in Hyperskills.



The original one - I thought the person was damn clever to do it this way:


Now, when I tried to use the function written using Scanner by others to one in the IDE, it won't work if the function is asked to have 2nd last element as the first element.


How do I transform that clever code to a method using the IDE with the same kind of idea behind that clever developer ?


I also tried to study the way this guy did in the stackoverflow : https://stackoverflow.com/questions/40348218/how-to-shift-an-entire-array-one-position-to-the-right

and he uses



But, it doesn't help in terms of transforming the code that can use if that's say the shifting is changed to the last 2nd element or 3rd element etc.



Tks.
 
Marshal
Posts: 73767
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid abbreviations like “Tks”; I know what they mean but people who don't come from an English‑speaking background might have difficulty.

I am afraid I don't I don't like that way to create an array with Scanner. Use its Streams ability, and put that into a separate method.I would prefer to see a separate method to rotate an array, but I think that is as good an algorithm as any. If yu want to work out algorithms like that, don't do it in code. Do it with diagrams on paper (or a WP document) until you understand what you are doing. Decide what to do if either the array's length or the distance to shift is 0, and generalise the method so it can rotate by any number of places.
 
tangara goh
Ranch Hand
Posts: 814
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

How do you write a method that don't use Java 8 to do it and without Scanner ? The using Scanner method I am able to view others answers.  Sorry that I was not clear but I was trying to convert one of the brilliant answers using Scanner to one that that no need to use Scanner because that is what real life programming is about.
 
Bartender
Posts: 1068
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are two things that Campbell said rather passively that can be said more forcefully, slightly more likely to be interpreted as snarky or rude but less likely to be missed.

So this is definitely not intended to be snarky or rude, but to be more clear by shouting two things that he whispered.

Scanner is a very convenient method to read items in from an Input Stream (the IO kind, STDIN or a filestream, etc.).

What we are trying to do should always be called rotating, because we can shift and move in zero values or arbitrary values, rather than actually rotating.
Rotating means you still have the exact same elements, but they move to different positions, nothing goes away and nothing new comes in.

There are plenty of ways to read in an array without a Scanner but most find them harder (there are one or two nasty tricks using Scanner in general, but once you know those they are easy).

So it is good to separate the idea of reading in an Array from some source of elements, for whatever you might want to do with it, sort, search it, just keep it around for later, and of rotating an array you already have.

Few people are writing much new code for anything less than Java 8, and Streams (the type Stream, IntStream, LongStream, DoubleStream) are very much part of basic Java now.

There are lots and lots and lots and lots of Very Old Examples all over the internet and in books.  Oracle's fastidious attention to Compatibility means those Very Old Examples will still work, but are often no longer the best/clearest ways to do things any more.

When you separate obtaining the elements from the act of rotating them, you have a rotate method you can use on any array, and a way to read in an array to do whatever you might want with it.

When you put them together, you have something useful only for doing both at once.

Reading your reply carefully, I think you actually DO want to separate the reading and the rotating, that is good.

I am a little surprised, given how many vary useful utilities they have in the Arrays helper class that they don't have one to rotate left or right an arbitrary number of elements.

A quick search showed many ways of varying efficiency/complexity and different levels of straight-forwardness or readability.

Are you just asking what is the best way to rotate an Array?  Are you trying to have the simplest code, the one that takes the least time, or the one that uses the least extra memory to do it?

There may be three different answers.

Or more.

So saying 'best' is a little like asking which is best, a dog, a slice of pizza or a flashlight.

The one that takes the least time and memory might be hard to write and debug and even to read, but if you write it once and use it a million times I'd say it is 'the best'.

If you are asked to write one for a job interview, the one that is 'best' is the one you are sure you can get right before you run out of time.

Which best one are you looking for?

 
Campbell Ritchie
Marshal
Posts: 73767
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . two things that Campbell said rather passively

I really shouted at my computer, but that doesn't transmit at all well across thousands of miles

. . .the Arrays helper class that they don't have one to rotate left or right . . .

Maybe they thought nobody would need it. There is a Collections#rotate(List, int) method, so you can create a List, rotate it, and use its toArray() method. But I think the idea of this sort of exercise is to write a rotate method by hand. As you said, I would prefer to see it generalised.
 
Saloon Keeper
Posts: 8460
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my C/C++ days I had a method declared like this that would detect which way the overlap (if any) went and would use the appropriate ascending or descending move. A length would be specified which could be more than one.
 
Jesse Silverman
Bartender
Posts: 1068
33
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:From my C/C++ days I had a method declared like this that would detect which way the overlap (if any) went and would use the appropriate ascending or descending move. A length would be specified which could be more than one.



I've done that too.  It was very efficient, but a little hard to read.  When I went away from it for a few days, I had to stare at it to know it was correct.
Except that I didn't, because it was right.
So I'd say the best one is the one that works the most efficiently, even if there are others that are easier to read.

Code that gets read all the time needs to be super-easy to read.
Code that gets called tons and almost nobody looks at it, not so much.
 
Carey Brown
Saloon Keeper
Posts: 8460
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:Code that gets called tons and almost nobody looks at it, not so much.


 
Jesse Silverman
Bartender
Posts: 1068
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought OP's question title said shift, but the code example showed rotate.
When someone asks for a rotate greater than length, does one treat that like the shift operator in Java, i.e. mask it to be modulo the length?
That is more logically justifiable than treating the shift operator in this manner, not that there is anything wrong with THAT.
 
Campbell Ritchie
Marshal
Posts: 73767
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . modulo the length? . . .

That is what I would have thought; it also allows for negative shifts, which means rotation to the left right left right other way.
 
Would you like to try a free sample? Today we are featuring tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic