gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
expanded sample size, first version
[mjc2wsl.git] / samples / alpha-mj-exp / Palindromes.mj
1 program Palindromes {
2 // return a palindrome if it can be generated from given letters
4 char[] load()
5 char[] res;
6 char in;
7 int c, num; {
8 print('l');
9 print('?');
10 read(num);
11 res = new char[num];
12 c = 0;
13 while (c<num) {
14 print(c);
15 print('?');
16 read(in);
17 if (ord(in) == 10) {
18 read(in);
19 }
20 res[c] = in;
21 c++;
22 }
23 return res;
24 }
26 char[] reverse(char[] arr)
27 int i;
28 char[] res;
29 {
30 res = new char[len(arr)];
31 i = 0;
32 while (i < len(arr)) {
33 res[i] = arr[len(arr) - i - 1];
34 i++;
35 }
36 return res;
37 }
39 void printArr(char[] arr)
40 int i;
41 {
42 print('[');
43 if (arr != null) {
44 i = 0;
45 while (i < len(arr)) {
46 print(arr[i], 3);
47 i++;
48 }
49 }
50 print(']', 3);
51 print('\n');
52 }
54 char[] concatA(char[] arr1, char[] arr2)
55 char[] res;
56 int i;
57 {
58 res = new char[len(arr1) + len(arr2)];
59 i = 0;
60 while (i < len(arr1)) {
61 res[i] = arr1[i];
62 i++;
63 }
64 while (i < len(res)) {
65 res[i] = arr2[i - len(arr1)];
66 i++;
67 }
69 return res;
70 }
72 char[] copy(char[] arr, int counter)
73 int i;
74 char[] res;
75 {
76 res = new char[counter];
77 i = 0;
78 while (i < len(res)) {
79 res[i] = arr[i];
80 i++;
81 }
83 return res;
84 }
86 char[] concatC(char[] arr, char character)
87 char[] res;
88 int i;
89 {
90 res = new char[len(arr) + 1];
91 i = 0;
92 while (i < len(arr)) {
93 res[i] = arr[i];
94 i++;
95 }
96 res[i] = character;
98 return res;
99 }
101 char[] findPalindrome(char[] s)
102 int[] letters;
103 int i, j, counter, ret;
104 char ch, oddChar;
105 char[] palindrome, palindromeRes;
107 letters = new int[26];
109 i = 0;
110 while (i < len(letters)) {
111 letters[i] = 0;
112 i++;
115 i = 0;
116 while (i < len(s)) {
117 if (ord(s[i]) >= 97 && ord(s[i]) <= 122) {
118 letters[ord(s[i]) - 97]++;
120 i++;
123 oddChar = ' ';
124 palindrome = new char[len(s)];
125 counter = 0;
127 i = 0;
128 ret = 1;
129 while (i < len(letters) && ret != 0) {
130 if (letters[i] > 0 && letters[i] % 2 == 0) {
131 j = 1;
132 while (j <= letters[i] / 2) {
133 palindrome[counter] = chr(i + 97);
134 counter++;
135 j++;
137 } else if (letters[i] > 0 && oddChar == ' ') {
138 oddChar = chr(i + 97);
139 j = 1;
140 while (j <= letters[i] / 2) {
141 palindrome[counter] = chr(i + 97);
142 counter++;
143 j++;
145 } else if (letters[i] % 2 != 0 && oddChar != ' ') {
146 ret = 0;
149 i++;
152 palindromeRes = copy(palindrome, counter);
154 if (ret == 0) {
155 return null;
156 } else if (oddChar != ' ') {
157 return concatA(concatC(palindromeRes, oddChar), reverse(palindromeRes));
158 } else {
159 return concatA(palindromeRes, reverse(palindromeRes));
163 void main()
164 char[] input;
165 int response;
167 input = load();
168 printArr(input);
169 printArr(findPalindrome(input));
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner