+ 42
[ASSIGNMENT] Lets generate this pattern!!
var array=[1,2,3,4,5,6,7,8, 9]; //pattern 123 234 345 456 567 678 789 891 912 123 ....to infinity the pattern itself is easy,the problem is the pattern restarting after its reached the end.i need this for a web code,thanks Note::it should work with an array of any length or elements.Thanks!!😄 non web codes might help too
53 Answers
+ 47
Just do this :
""+arr[i%length]+arr[(i+1)%length]+arr[(i+2)%length]
https://code.sololearn.com/WFU5e5iaEynx/?ref=app
+ 20
https://code.sololearn.com/cNvftY4nN1Ej/?ref=app
https://code.sololearn.com/c4r3Q50jQU32/?ref=app
+ 16
to get the next value you can use :
( current modulo number ) + offset
in your case it is : ( current % 9 ) + 1
to reach the correct next value
+ 16
first part of problem as David told with modulo then
to solve the situation when reached at end of array, concat the ending array part and starting array part in a single arrray , it ll happen when
when( end index < start index)
https://code.sololearn.com/Wj8XH9T2AJFZ/?ref=app
+ 16
আমি বাংলাদেশি
+ 15
☺️☺️☺️👇My try in php
https://code.sololearn.com/wzaUmhkJ9mR8/?ref=app
+ 12
My solution is surprisingly easy.
But up to now all other solutions are mote complicated since we KNOW how to solve this kind of probs.
https://code.sololearn.com/cxnmuCGvoshv/?ref=app
+ 8
https://code.sololearn.com/cm1bWStrfqMK/?ref=app
+ 7
https://code.sololearn.com/cN0TvhZYUnZy/?ref=app
This works until time out.
+ 7
In only 5 lines!
https://code.sololearn.com/cYjWP3Nlf5rC/?ref=app
+ 6
https://code.sololearn.com/Wg6ImxLVHTdj/#js
+ 6
confusing right
+ 6
another(easy) solution
https://code.sololearn.com/cxYneGqWoMjU/?ref=app
+ 5
#include<iostream>
using namespace std;
int main()
{
int arr[]={1,2,3,4,5,6,7,8,9};
int i,j=0,k=0;
while(k<20)
{
for(i=1;i<=3;i++)
{
if(j==10)
{
j==1;
}
cout<<arr[j];
j++;
}
if(j==10)
{
j%9+1;
}
j=j-2;
cout<<"\n";
k++;
}
}
+ 5
My Creation.
https://code.sololearn.com/W0MYNHy5cT9t/?ref=app
+ 5
Nothing is wrong with modulo..😉😉
https://code.sololearn.com/c750gLB3VPF0/?ref=app
https://code.sololearn.com/ch10YF659O2o/?ref=app
+ 4
https://code.sololearn.com/w6v4bH35NH6X/?ref=app
https://code.sololearn.com/wxnXDmklU0vy/?ref=app
+ 4
https://code.sololearn.com/cALSQ6QJLu2Y/?ref=app