Need logic for number pattern | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Need logic for number pattern

12345 23451 24513 24135 24153

15th Nov 2017, 3:01 PM
Kiran Raju Unikili
Kiran Raju Unikili - avatar
7 Answers
+ 7
Basically, you'll just need some sort of list/array/etc... and you'll create a loop for it. For each iteration of the loop (i), we'll shift the number in that position to the end. This will prevent us from touching the previous positions on each loop through, and it'll stop after we've dealt with the entire list/array. JAVA EXAMPLE: https://code.sololearn.com/c13eIMGwuAsw/#java import java.util.*; public class Program { public static void main(String[] args) { List numberList = new ArrayList(Arrays.asList(1,2,3,4,5)); for(int i = 0; i < numberList.size(); ++i){ numberList.add(numberList.remove(i)); System.out.print(numberList.get(i)); } } }
15th Nov 2017, 4:12 PM
AgentSmith
+ 7
@Daniel, Thanks for pointing that out, I possibly misunderstood the question, it's been a while, and the TS didn't say anything, so I didn't notice, so sorry... Lemme take that bad egg out... Big Thanks :)
27th Nov 2017, 2:39 PM
Ipang
+ 5
Okay, after thinking on it for a moment, this is my take on it. Take your first state: >> 12345 We're going to take the first number, push it to the back, and the new number at the beginning will be "locked in" and no longer able to change. >> 23451 We'll repeat the same process until all numbers are locked in. 2 is locked in, so go to the second number which is 3. Move it to the end and now the 4 is locked in as the second number. >> 24513 As before, move the 5 to the end, and now 1 is locked in as the third number. >> 24135 As before, move the 3 to the end, and 5 is locked in as the 4th number. No room to move now, so 3 is locked in as the last number. >> 24153
15th Nov 2017, 3:16 PM
AgentSmith
+ 3
Ipang your code not work like example, check it ;-)
24th Nov 2017, 2:08 PM
Daniel
Daniel - avatar
+ 3
I'm late to the game...?😅 Here's mine in JS [ES6], works with more characters too: https://code.sololearn.com/WMWf1Q2UWWo8/?ref=app
27th Nov 2017, 5:12 PM
Navardium
Navardium - avatar
+ 2
i know it theoretically. but i need code.. in any programming language.
15th Nov 2017, 3:19 PM
Kiran Raju Unikili
Kiran Raju Unikili - avatar
+ 1
https://code.sololearn.com/cXbASVlw1J67/?ref=app This is the final code from Ipang code ;-)
27th Nov 2017, 1:59 PM
Daniel
Daniel - avatar