|
| 1 | +--- |
| 2 | +language: c |
| 3 | +exerciseType: 1 |
| 4 | +difficulty: 2 |
| 5 | +title: Anagramm |
| 6 | +--- |
| 7 | + |
| 8 | +# --description-- |
| 9 | + |
| 10 | +Zwei Wörter sind Anagramme, wenn eines eine Umordnung des anderen ist: Sie verwenden exakt dieselben Buchstaben, jeden Buchstaben gleich oft, nur in einer anderen Reihenfolge. `listen` und `silent` sind Anagramme, und ebenso `stone` und `tones`. |
| 11 | + |
| 12 | +Ein Wort ist nie ein Anagramm von sich selbst. Wenn die beiden Wörter exakt gleich sind, wurde nichts umgeordnet, daher ist die Antwort `false`. Beide Wörter sind in Kleinbuchstaben gegeben und enthalten nur die Buchstaben von `a` bis `z`. |
| 13 | + |
| 14 | +# --instructions-- |
| 15 | + |
| 16 | +Schreiben Sie eine Funktion `isAnagram`, die zwei Wörter, `first` und `second`, entgegennimmt und `true` zurückgibt, wenn sie Anagramme voneinander sind, und andernfalls `false`. |
| 17 | + |
| 18 | +Beispiele: |
| 19 | +``` |
| 20 | +isAnagram("listen", "silent") ➞ true |
| 21 | +isAnagram("stone", "tones") ➞ true |
| 22 | +isAnagram("stone", "stone") ➞ false |
| 23 | +isAnagram("abc", "abcd") ➞ false |
| 24 | +isAnagram("aab", "abb") ➞ false |
| 25 | +``` |
| 26 | + |
| 27 | +- Zwei identische Wörter sind keine Anagramme. |
| 28 | +- Wörter unterschiedlicher Länge sind nie Anagramme. |
| 29 | +- Jeder Buchstabe muss in beiden Wörtern gleich oft vorkommen. |
| 30 | + |
| 31 | +# --before-seed-- |
| 32 | + |
| 33 | +```c |
| 34 | +// DO NOT EDIT FROM HERE |
| 35 | +#include <setjmp.h> |
| 36 | +#include <stdio.h> |
| 37 | +#include <stdbool.h> |
| 38 | + |
| 39 | +#ifndef _CEXCEPTION_H |
| 40 | +#define _CEXCEPTION_H |
| 41 | + |
| 42 | +#ifdef __cplusplus |
| 43 | +extern "C" |
| 44 | +{ |
| 45 | +#endif |
| 46 | + |
| 47 | +//This is the value to assign when there isn't an exception |
| 48 | +#ifndef CEXCEPTION_NONE |
| 49 | +#define CEXCEPTION_NONE (0x5A5A5A5A) |
| 50 | +#endif |
| 51 | + |
| 52 | +//This is number of exception stacks to keep track of (one per task) |
| 53 | +#ifndef CEXCEPTION_NUM_ID |
| 54 | +#define CEXCEPTION_NUM_ID (1) //there is only the one stack by default |
| 55 | +#endif |
| 56 | + |
| 57 | +//This is the method of getting the current exception stack index (0 if only one stack) |
| 58 | +#ifndef CEXCEPTION_GET_ID |
| 59 | +#define CEXCEPTION_GET_ID (0) //use the first index always because there is only one anyway |
| 60 | +#endif |
| 61 | + |
| 62 | +//The type to use to store the exception values. |
| 63 | +#ifndef CEXCEPTION_T |
| 64 | +#define CEXCEPTION_T unsigned int |
| 65 | +#endif |
| 66 | + |
| 67 | +//This is an optional special handler for when there is no global Catch |
| 68 | +#ifndef CEXCEPTION_NO_CATCH_HANDLER |
| 69 | +#define CEXCEPTION_NO_CATCH_HANDLER(id) |
| 70 | +#endif |
| 71 | + |
| 72 | +//These hooks allow you to inject custom code into places, particularly useful for saving and restoring additional state |
| 73 | +#ifndef CEXCEPTION_HOOK_START_TRY |
| 74 | +#define CEXCEPTION_HOOK_START_TRY |
| 75 | +#endif |
| 76 | +#ifndef CEXCEPTION_HOOK_HAPPY_TRY |
| 77 | +#define CEXCEPTION_HOOK_HAPPY_TRY |
| 78 | +#endif |
| 79 | +#ifndef CEXCEPTION_HOOK_AFTER_TRY |
| 80 | +#define CEXCEPTION_HOOK_AFTER_TRY |
| 81 | +#endif |
| 82 | +#ifndef CEXCEPTION_HOOK_START_CATCH |
| 83 | +#define CEXCEPTION_HOOK_START_CATCH |
| 84 | +#endif |
| 85 | + |
| 86 | +//exception frame structures |
| 87 | +typedef struct { |
| 88 | + jmp_buf* pFrame; |
| 89 | + CEXCEPTION_T volatile Exception; |
| 90 | +} CEXCEPTION_FRAME_T; |
| 91 | + |
| 92 | +//actual root frame storage (only one if single-tasking) |
| 93 | +extern volatile CEXCEPTION_FRAME_T CExceptionFrames[]; |
| 94 | + |
| 95 | +//Try |
| 96 | +#define Try \ |
| 97 | + { \ |
| 98 | + jmp_buf *PrevFrame, NewFrame; \ |
| 99 | + unsigned int MY_ID = CEXCEPTION_GET_ID; \ |
| 100 | + PrevFrame = CExceptionFrames[MY_ID].pFrame; \ |
| 101 | + CExceptionFrames[MY_ID].pFrame = (jmp_buf*)(&NewFrame); \ |
| 102 | + CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \ |
| 103 | + CEXCEPTION_HOOK_START_TRY; \ |
| 104 | + if (setjmp(NewFrame) == 0) { \ |
| 105 | + if (1) |
| 106 | + |
| 107 | +//Catch |
| 108 | +#define Catch(e) \ |
| 109 | + else { } \ |
| 110 | + CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; \ |
| 111 | + CEXCEPTION_HOOK_HAPPY_TRY; \ |
| 112 | + } \ |
| 113 | + else \ |
| 114 | + { \ |
| 115 | + e = CExceptionFrames[MY_ID].Exception; \ |
| 116 | + (void)e; \ |
| 117 | + CEXCEPTION_HOOK_START_CATCH; \ |
| 118 | + } \ |
| 119 | + CExceptionFrames[MY_ID].pFrame = PrevFrame; \ |
| 120 | + CEXCEPTION_HOOK_AFTER_TRY; \ |
| 121 | + } \ |
| 122 | + if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE) |
| 123 | + |
| 124 | +#endif |
| 125 | + |
| 126 | +volatile CEXCEPTION_FRAME_T CExceptionFrames[CEXCEPTION_NUM_ID] = {{ 0 }}; |
| 127 | + |
| 128 | +void Throw(CEXCEPTION_T ExceptionID) |
| 129 | +{ |
| 130 | + unsigned int MY_ID = CEXCEPTION_GET_ID; |
| 131 | + CExceptionFrames[MY_ID].Exception = ExceptionID; |
| 132 | + if (CExceptionFrames[MY_ID].pFrame) |
| 133 | + { |
| 134 | + longjmp(*CExceptionFrames[MY_ID].pFrame, 1); |
| 135 | + } |
| 136 | + CEXCEPTION_NO_CATCH_HANDLER(ExceptionID); |
| 137 | +} |
| 138 | + |
| 139 | +CEXCEPTION_T e; |
| 140 | +int _test_failed_count = 0; |
| 141 | +int _test_count = 0; |
| 142 | + |
| 143 | +void try_catch(bool assertion) { |
| 144 | + _test_count++; |
| 145 | + Try { |
| 146 | + if (!assertion) { |
| 147 | + Throw(_test_count); |
| 148 | + } |
| 149 | + } Catch (e) { |
| 150 | + _test_failed_count += 1; |
| 151 | + printf("Test Case '--err-t%i--' failed\n", e); |
| 152 | + } |
| 153 | +} |
| 154 | +// DO NOT EDIT UNTIL HERE |
| 155 | +#include <stdlib.h> |
| 156 | +#include <string.h> |
| 157 | +``` |
| 158 | + |
| 159 | +# --seed-- |
| 160 | + |
| 161 | +```c |
| 162 | +bool isAnagram(const char* first, const char* second) { |
| 163 | + |
| 164 | +} |
| 165 | +``` |
| 166 | +
|
| 167 | +# --before-asserts-- |
| 168 | +
|
| 169 | +```c |
| 170 | +int main() { |
| 171 | +``` |
| 172 | + |
| 173 | +# --asserts-- |
| 174 | + |
| 175 | +Die Wörter "listen" und "silent" sind Anagramme |
| 176 | + |
| 177 | +```c |
| 178 | + try_catch(isAnagram("listen", "silent") == true); |
| 179 | +``` |
| 180 | +
|
| 181 | +Die Wörter "stone" und "tones" sind Anagramme |
| 182 | +
|
| 183 | +```c |
| 184 | + try_catch(isAnagram("stone", "tones") == true); |
| 185 | +``` |
| 186 | + |
| 187 | +Ein Wort ist kein Anagramm von sich selbst |
| 188 | + |
| 189 | +```c |
| 190 | + try_catch(isAnagram("stone", "stone") == false); |
| 191 | +``` |
| 192 | +
|
| 193 | +Wörter unterschiedlicher Länge sind keine Anagramme |
| 194 | +
|
| 195 | +```c |
| 196 | + try_catch(isAnagram("abc", "abcd") == false); |
| 197 | +``` |
| 198 | + |
| 199 | +Dieselben Buchstaben in unterschiedlicher Anzahl sind kein Anagramm |
| 200 | + |
| 201 | +```c |
| 202 | + try_catch(isAnagram("aab", "abb") == false); |
| 203 | +``` |
| 204 | +
|
| 205 | +Die Wörter "anagram" und "nagaram" sind Anagramme |
| 206 | +
|
| 207 | +```c |
| 208 | + try_catch(isAnagram("anagram", "nagaram") == true); |
| 209 | +``` |
| 210 | + |
| 211 | +Zwei Wörter derselben Länge mit unterschiedlichen Buchstaben sind keine Anagramme |
| 212 | + |
| 213 | +```c |
| 214 | + try_catch(isAnagram("rat", "car") == false); |
| 215 | +``` |
| 216 | +
|
| 217 | +Zwei leere Wörter sind identisch, daher sind sie keine Anagramme |
| 218 | +
|
| 219 | +```c |
| 220 | + try_catch(isAnagram("", "") == false); |
| 221 | +``` |
| 222 | + |
| 223 | +Zwei unterschiedliche einzelne Buchstaben sind keine Anagramme |
| 224 | + |
| 225 | +```c |
| 226 | + try_catch(isAnagram("a", "b") == false); |
| 227 | +``` |
| 228 | +
|
| 229 | +Die Wörter "evil" und "vile" sind Anagramme |
| 230 | +
|
| 231 | +```c |
| 232 | + try_catch(isAnagram("evil", "vile") == true); |
| 233 | +``` |
| 234 | + |
| 235 | +# --after-asserts-- |
| 236 | + |
| 237 | +```c |
| 238 | + printf("Executed %d tests, with %d failures", _test_count, _test_failed_count); |
| 239 | + return 0; |
| 240 | +} |
| 241 | +``` |
| 242 | +
|
| 243 | +# --solutions-- |
| 244 | +
|
| 245 | +```c |
| 246 | +bool isAnagram(const char* first, const char* second) { |
| 247 | + if (strcmp(first, second) == 0) { |
| 248 | + return false; |
| 249 | + } |
| 250 | +
|
| 251 | + int counts[26] = {0}; |
| 252 | +
|
| 253 | + for (int i = 0; first[i] != '\0'; i++) { |
| 254 | + counts[first[i] - 'a']++; |
| 255 | + } |
| 256 | +
|
| 257 | + for (int i = 0; second[i] != '\0'; i++) { |
| 258 | + counts[second[i] - 'a']--; |
| 259 | + } |
| 260 | +
|
| 261 | + for (int i = 0; i < 26; i++) { |
| 262 | + if (counts[i] != 0) { |
| 263 | + return false; |
| 264 | + } |
| 265 | + } |
| 266 | +
|
| 267 | + return true; |
| 268 | +} |
| 269 | +``` |
0 commit comments