Swap Elements in Lisp list - list

I'm trying to implement a function that returns a list of LIST (each list in LIST is the result of two elements swapped in list). It's supposed to do a search based on the list formed from each swap. It is part of my program to solve the 8 puzzle problem. Here's what i have so far
(setq *LIST* nil)
(defun swapped_list(lst)
(loop for j in (positions_to_swap) do
(setq *LIST* (rotatef (nth pos lst) (nth j lst))
*LIST*)
(swapped_list '(11 12 13 14 15 16 17 18 19))
If positions_to_swap is (0 2 5) and pos is 4, this should return
((15 12 13 14 11 16 17 18 19) (11 12 15 14 13 16 17 18 19) (11 12 13 14 16 15 17 18 19))
I've been spending countless hours trying to debug with no progress. I've tried many variants but none of them work.

If positions_to_swap is (0 2 5) and pos is 4, this should return ((15
12 13 14 11 16 17 18 19) (11 12 15 14 13 16 17 18 19) (11 12 13 14 16
15 17 18 19))
(defun swap (list position positions-to-swap)
(loop for position-to-swap in positions-to-swap
for rotated-list = (copy-list list)
do (rotatef (nth position rotated-list)
(nth position-to-swap rotated-list))
collect rotated-list))
Does the trick:
CL-USER> (swap '(11 12 13 14 15 16 17 18 19) 4 '(0 2 5))
((15 12 13 14 11 16 17 18 19)
(11 12 15 14 13 16 17 18 19)
(11 12 13 14 16 15 17 18 19))

Related

3v3 round robin schedule generator

I have been asked to create round robin schedules for a 3v3 volleyball tournament where the following criteria must be met: (from most important to least important)
Each player plays with another no more than once
Each player gets to play at least 4 times
Each player cannot sit out for more than one round
Each player does not play against another player more
than twice
The question I am struggling to brute force is the case when I have:
40 players
4 courts
I’ve been able to brute force a semi functional solution for 15 teams but it’s become too unwieldy to do it by hand so I was thinking of trying a program in java. I am not entirely sure how I can go about making a brute force program to do this. I am currently attempting to use a list of every possible team of 3 and inserting them into the schedule. Then iterating over them replacing until the game works.
Here is another version of the same question asked by someone else
https://eso-community.net/viewtopic.php?t=9816
The top answer here has provided perfect solutions to teams of 8-13
The 40 player problem has a huge search space, and I don't believe that any brute force solution will work. Just think about selecting the 24 players who participate in one round, there are 6x10^10 ways of doing just that, and many many more ways of assigning those 24 players to 4 courts of 3 vs 3.
It's not clear exactly how may rounds you are looking for. With 4 courts of 6 players each, this leaves 16 byes per round. Therefore any multiple (i) of 5 rounds is very desirable, since 16 x 5i = 80i, so it is possible to arrange that for every 5i rounds played, each of the 40 teams has exactly 3i games and 2i byes. You have asked for "each player gets to play at least 4 times", so the best option is to look for a solution with 10 rounds, where each player has 6 games. Here is one possible option where all partners are different, and all opponents are different.
(38 15 11 v 7 25 23) (33 14 9 v 20 29 19) (40 16 27 v 37 28 21) (34 17 3 v 10 24 32)
(39 11 12 v 8 21 24) (34 15 10 v 16 30 20) (36 17 28 v 38 29 22) (35 18 4 v 6 25 33)
(40 12 13 v 9 22 25) (35 11 6 v 17 26 16) (37 18 29 v 39 30 23) (31 19 5 v 7 21 34)
(36 13 14 v 10 23 21) (31 12 7 v 18 27 17) (38 19 30 v 40 26 24) (32 20 1 v 8 22 35)
(37 14 15 v 6 24 22) (32 13 8 v 19 28 18) (39 20 26 v 36 27 25) (33 16 2 v 9 23 31)
(36 5 33 v 12 32 15) (13 7 24 v 20 4 2) (25 1 28 v 30 10 31) (37 26 38 v 3 9 18)
(37 1 34 v 13 33 11) (14 8 25 v 16 5 3) (21 2 29 v 26 6 32) (38 27 39 v 4 10 19)
(38 2 35 v 14 34 12) (15 9 21 v 17 1 4) (22 3 30 v 27 7 33) (39 28 40 v 5 6 20)
(39 3 31 v 15 35 13) (11 10 22 v 18 2 5) (23 4 26 v 28 8 34) (40 29 36 v 1 7 16)
(40 4 32 v 11 31 14) (12 6 23 v 19 3 1) (24 5 27 v 29 9 35) (36 30 37 v 2 8 17)

Printing a table using nested while loops only

I'm trying to print the following table in C++:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Using nested while loops only.
I have two main problems here:
I don't understand how to do it without using a non-nested while at the beginning (to print the first line), or an if statement for the first line.
Using setw I have problems trying to align the numbers with two digits.
Here is what I tried
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k=0;
while(k<=10)
{cout << k << setw(5);
k++;
};
cout << "\n";
int i=1;
while(i<=10){
cout << i << setw(5);
int j=1;
while(j<=10){
cout<< i*j << setw(5);
j++;
}
cout << "\n";
i++;
}
return 0;
}
But, as said, I used a non-nested while at the beginning, and also the output is:
0 1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Where the two-digits numbers are not aligned in the proper way. On the other hand I cannot think a way to modify the loop in order to increase the space only for two digits number, without using an if statement.
So am I missing something or it is not possible to print the table above without using if or non-nested while?
I think you want to hide first 0 value. To hide it, I have used some bit manipulations. There is no if statement and all of the printing in nested while.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i = 0, k = 0;
while (i < 10){
int j = 0;
while (j <= 10){
cout << left << setw(5);
(i || j || k) && cout << j + i * j + !j * (i + 1);
!(i || j || k) && cout << "";
j++;
}
cout << "\n";
i += k++ > 0;
}
return 0;
}
The output is
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 2 4 6 8 10 12 14 16 18 20
3 3 6 9 12 15 18 21 24 27 30
4 4 8 12 16 20 24 28 32 36 40
5 5 10 15 20 25 30 35 40 45 50
6 6 12 18 24 30 36 42 48 54 60
7 7 14 21 28 35 42 49 56 63 70
8 8 16 24 32 40 48 56 64 72 80
9 9 18 27 36 45 54 63 72 81 90
10 10 20 30 40 50 60 70 80 90 100
Explanation:
I have used j for seed and i * j for new value for (j + 1)-th column.
It works perfectly for from first row second column to last row last column.
For first column, j is always 0. To make it work, I used !j * (i + 1) which prints 0 to 10 for first column.
To hide 0 for first row and first column, all of i, j and k are 0. For all other cells, at least one of them has value other than 0.
If still unclear, I'll explain with example.
Things to consider:
Outputting tabs instead of trying to set the width is much easier,
though you'll have less control over how your output looks (it will
depend in the tab stop width of your console). But your standard tab stops
will handle your range of values.
your problem statement doesn't say how many while loops you can use
You can use a while loop to emulate an if statement like this:
while(j == 0){
cout << i << '\t';
break;
}
I'll leave the rest as an exercise for you.

Replace middle row elements of nested list with new list elements Q kdb

Hi so I have created the nested list/matrix:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m
1 2 3
4 5 6
7 8 9
I have also identified the middle column in the list:
q)a:m[0;1],m[1;1],m[2;1]
I now want to replace the middle row (4 5 6) with a to finish with m looking like:
q)m
1 2 3
2 5 8
7 8 9
You've already seen you can index into the matrix with syntax like m[0;1], where 0 refers to the first level of nesting and 1 refers to the second level
KDB also allows you to assign to an index of a list in a similar way e.g.
q)l:1 2 3 4
q)l[1]:20
q)l
1 20 3 4
So you can use something similar in this example:
q)m[1]:a
q)m
1 2 3
2 5 8
7 8 9
As an aside, KDB also allows you to leave out an index, in which case it will take all items from the corresponding level of nesting, e.g.
q)m[0] /first level of nesting i.e. first row
1 2 3
q)m[;0] /second level of nesting i.e. first column
1 4 7
Hope that helps
Jonathon McMurray
AquaQ Analytics
You want to generalise for larger matrices (which must also be square) so your answer needs two parts:
how to construct a
how to insert it
for row/col x where x<count m.
The general expression you want is simply m[x;]:m[;x], because m[x;] denotes row x and m[;x] denotes column x.
See Q for Mortals 3.11.3 Two- and Three-Dimensional Matrices
You can make this a function of the index and the matrix:
q)show m:5 5#1_til 26
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
q){y[x;]:y[;x];:y}[3;m]
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
4 9 14 19 24
21 22 23 24 25
Just adding another approach for you.
q)m:8 cut til 64
q)0 0+\:til 8
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
q)(m)./:flip 0 0+\:til 8
0 9 18 27 36 45 54 63
q)#[m;4;:;(m)./:flip 0 0+\:til 8]
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q)
For fun, here it is in a function which takes the length&width of the matrix and replaces the 'middle' row with the diagonal values
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}8
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
0 9 18 27 36 45 54 63
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}5
0 1 2 3 4
5 6 7 8 9
0 6 12 18 24
15 16 17 18 19
20 21 22 23 24
q){n:x*x;m:x cut til n;#[m;x div 2;:;](m)./:flip 0 0+\:til x}4
0 1 2 3
4 5 6 7
0 5 10 15
12 13 14 15
q)
q)#[((1 2 3);(4 5 6);(7 8 9));1;:;(2;5;8)]
1 2 3
2 5 8
7 8 9
Indexing in q can be straight forward and I believe a intermediate can be omitted:
q)m:((1 2 3);(4 5 6);(7 8 9))
q)m[1]:m[;1]
q)m
1 2 3
2 5 8
7 8 9

How to do _mm256_maskstore_epi8() in C/C++?

The problem
What I am trying to do is, if I have a vector of 27 (not 32!) int8_t:
x = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}
I want to first cyclic-shift it to the right by n (not a constant), e.g. if n=1:
x2 = {26,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}
Then this vector is used to perform some very complex calculation, but for simplicity, let's assume that the next step is just to cyclic-shift it back to the left by n, and store it to the memory. So I should have a new vector of 27 int8_t:
y = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}
So there're thousands of such vectors and performance is very critical here. The CPU that we're using has AVX2 support so we want to use it to speed things up.
My current solution
To get x2, I use two _mm256_loadu_si256() with a _mm256_blendv_epi8():
int8_t x[31+27+31];
for(int i=0; i<27; i++){
x[31+i] = i;
}
__m256i mask = _mm256_set_epi32 (0x0, 0x00800000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0);
__m256i x_second_part = _mm256_loadu_si256((__m256i*)(x+31+1)); //{1,2,...,26}
__m256i x_first_part = _mm256_loadu_si256((__m256i*)(x+31-26)); //{0}
__m256i x2 = _mm256_blendv_epi8(x_second_part, x_first_part, mask); //{1,2,...,26, 0}
int8_t y[31+27+31];
_mm256_storeu_si256((__m256i*)(y+31-26), x2);
_mm256_storeu_si256((__m256i*)(y+31+1), x2);
The reason why the x and y are declared to be of size [31+27+31] is that in this case _mm256_loadu_si256() and _mm256_storeu_si256() won't cause segfault.
And I can get the value of y by:
for(int i=0; i<27; i++){
cout << (int)y[31+i] << ' ';
}
The new problem
Unfortunately all the vectors must be continuous in memory, for example, if there are totally two vectors that need to be processed:
x = {[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];
[27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53]};
Then I cannot just use _mm256_storeu_si256() to put the value of y back to memory because when the value of the second vector is written to memory it will overwrite some values of the first vector:
int8_t x[31+27+27+31];
int8_t y[31+27+27+31];
for(int i=0; i<27*2; i++){
x[31+i] = i;
}
for(int i=0; i<2; i++){
__m256i mask = _mm256_set_epi32 (0x0, 0x00800000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0);
__m256i x_second_part = _mm256_loadu_si256((__m256i*)(x+31+27*i+1)); //{1,2,...,26}
__m256i x_first_part = _mm256_loadu_si256((__m256i*)(x+31+27*i-26)); //{0}
__m256i x2 = _mm256_blendv_epi8(x_second_part, x_first_part, mask); //{1,2,...,26, 0}
_mm256_storeu_si256((__m256i*)(y+31+27*i-26), x2);
_mm256_storeu_si256((__m256i*)(y+31+27*i+1), x2);
}
for(int i=0; i<27; i++){
cout << (int)y[31+i] << ' ';
}cout << endl;
for(int i=0; i<27; i++){
cout << (int)y[31+27+i] << ' ';
}cout << endl;
will output
0 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
instead of
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
So I was thinking of using maskstore. But in the Intel Intrinsic Guide I couldn't find _mm256_maskstore_epi8. This leads me back to the topic:
How to do _mm256_maskstore_epi8() in C/C++?
There is another implementation of cyclic-shift inside 27-bytes vector with using AVX2:
#include <iostream>
#include <immintrin.h>
const __m256i K0 = _mm256_setr_epi8(
0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0);
const __m256i K1 = _mm256_setr_epi8(
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70);
inline const __m256i Shuffle(const __m256i & value, const __m256i & shuffle)
{
return _mm256_or_si256(_mm256_shuffle_epi8(value, _mm256_add_epi8(shuffle, K0)),
_mm256_shuffle_epi8(_mm256_permute4x64_epi64(value, 0x4E), _mm256_add_epi8(shuffle, K1)));
}
__m256i shuffles[27];
void Init()
{
uint8_t * p = (uint8_t *)shuffles;
for (int s = 0; s < 27; ++s)
for (int i = 0; i < 32; ++i)
p[s*32 + i] = i < 27 ? (27 + i - s)%27 : i;
}
void CyclicShift27(const uint8_t * src, size_t shift, uint8_t * dst)
{
_mm256_storeu_si256((__m256i*)dst, Shuffle(_mm256_loadu_si256((__m256i*)src), shuffles[shift]));
}
int main()
{
Init();
uint8_t src[32] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 }, dst[32];
for (int j = 0; j < 27; ++j)
{
CyclicShift27(src, j, dst);
std::cout << "\t";
for (int i = 0; i < 32; i++)
std::cout << (int)dst[i] << ' ';
std::cout << std::endl;
}
return 0;
}
Output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27 28 29 30 31
25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 27 28 29 30 31
24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 27 28 29 30 31
23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 27 28 29 30 31
22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 27 28 29 30 31
21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 27 28 29 30 31
20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 27 28 29 30 31
19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 27 28 29 30 31
18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 27 28 29 30 31
17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 27 28 29 30 31
16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 27 28 29 30 31
15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 27 28 29 30 31
14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 27 28 29 30 31
13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 27 28 29 30 31
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 27 28 29 30 31
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 27 28 29 30 31
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 27 28 29 30 31
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 27 28 29 30 31
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 27 28 29 30 31
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 27 28 29 30 31
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 27 28 29 30 31
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 27 28 29 30 31
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 27 28 29 30 31
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 27 28 29 30 31
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 27 28 29 30 31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 27 28 29 30 31
It looks more simple than my previous answer.
I have made an implementation of cyclic-shift inside 27-bytes vector with using of SSSE3:
#include <iostream>
#include <tmmintrin.h>
union Shuffle
{
uint8_t s[64];
__m128i v[4];
};
Shuffle shuffles[27];
int Shift(int value)
{
return (value >= 0 && value < 16) ? value : -1;
}
void Init()
{
for (int s = 0; s < 27; ++s)
{
for (int i = 0; i < 16; ++i)
{
shuffles[s].s[0 + i] = s < 16 ? Shift(i - s) : Shift(i - s + 27);
shuffles[s].s[16 + i] = Shift(16 + i - s);
shuffles[s].s[32 + i] = Shift(11 + i - s);
shuffles[s].s[48 + i] = s < 11 ? Shift(i - s) : Shift(i - s + 27);
}
}
}
void CyclicShift27(const uint8_t * src, size_t shift, uint8_t * dst)
{
__m128i srcLo = _mm_loadu_si128((__m128i*)(src + 0));
__m128i srcHi = _mm_loadu_si128((__m128i*)(src + 11));
__m128i dstLo = _mm_or_si128(_mm_shuffle_epi8(srcLo, shuffles[shift].v[0]), _mm_shuffle_epi8(srcHi, shuffles[shift].v[1]));
__m128i dstHi = _mm_or_si128(_mm_shuffle_epi8(srcLo, shuffles[shift].v[2]), _mm_shuffle_epi8(srcHi, shuffles[shift].v[3]));
_mm_storeu_si128((__m128i*)(dst + 0), dstLo);
_mm_storeu_si128((__m128i*)(dst + 11), dstHi);
}
int main()
{
Init();
uint8_t src[27] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26 }, dst[27];
for (int j = 0; j < 27; ++j)
{
CyclicShift27(src, j, dst);
for (int i = 0; i < 27; i++)
std::cout << (int)dst[i] << ' ';
std::cout << std::endl;
}
return 0;
}
It output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12 13
13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11 12
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10 11
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8 9
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7 8
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6 7
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5 6
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4 5
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3 4
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2 3
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1 2
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 0
I hope it will be useful.

How to group ages in specific range using clojure?

I have a list which has the age of persons like (12,23,34,33,34,45,56...) almost 200 numbers. I want to group them (10-20)(21-30)(31-40)....(91-100)age groups seperately.
How do I do it in clojure.
Thank you
Here's an implementation, the key functions are group-by and quot:
(defn group-by-tens [numbers]
(->> numbers (group-by #(quot % 10))
(sort-by first)
(map second)))
Example:
(group-by-tens [15 28 35 6 9 37 33 47 11 38 4 27 49 47 38 20 36 49 27 30])
=> ([6 9 4] [15 11] [28 27 20 27] [35 37 33 38 38 36 30] [47 49 47 49])
also, if your age values are sorted (like in the example from your question), you can just partition them (or otherwise sort and partition):
user> (partition-by #(quot % 10)
[1 2 3 4 10 12 16 23 27 29 33 34 45 59 71 72])
;;=> ((1 2 3 4) (10 12 16) (23 27 29) (33 34) (45) (59) (71 72))