+ 4

[CHALLENGE] Remap elements array elements to a new start element

Consider the array: [a, b, c, d, e] The user will choose a new index to array begins, like 3, and needs receive the result: [d, e, a, b, c] ------------------------------------- Another example: Array: ["python", "java", "ruby", "swift"] Remapped to index 1: ["java", "ruby", "swift", "python"] >#GIVE PREFERENCE TO JAVASCRIPT#<

1st Feb 2018, 6:35 PM
Lucas Sousa
Lucas Sousa - avatar
5 Antworten
+ 4
that is called circular shifting edit: upon reading the challenge again, it's not quite circular shifting (reversed circular shifting with an index limitation, but that does not roll well on the tongue 😅) updated code as well ;)
1st Feb 2018, 7:17 PM
Burey
Burey - avatar
+ 3
Here I post my Python code with 3 ways of doing It, they are quite easy to understand and to do thanks to Python language, but Im sure you can translate that into java script. https://code.sololearn.com/c1U8n7ojEnnY/?ref=app
2nd Feb 2018, 9:58 PM
Lucas Pardo
Lucas Pardo - avatar
+ 2
My approach: We have to take our sequence, keeping the original order, starting in another index. When we start in another index we can look that we got a "sub-sequence" from new index to last index. Taking question example you can look: s = [a, b, c, d, e] / targetIndex = 2 if we want to start this sequence in index 2, we will got a new sub-sequence from 2 to s.length: [c, d, e]. Once we "sliced" our main sequence, we just need append the firsts elements to end of our first sliced sequence (elements from 0 to targetIndex): [a, b] Once we have to sub-sequences from main, we just need concat second in the first, then we got the remapped array: first: [c, d, e] second: [a, b] result: [c, d, e, a, b] Then this solves the challenge! Code: https://code.sololearn.com/WC06dtjgKN7q/#js --------------------------------------- Do you have a another cool approach? :)
1st Feb 2018, 10:07 PM
Lucas Sousa
Lucas Sousa - avatar
+ 1
Cool! I love Python as well. Thanks for coding!
3rd Feb 2018, 2:44 AM
Lucas Sousa
Lucas Sousa - avatar