Post History
C (gcc), 152 bytes r;e(c){r=c=='j'?5:strchr("bdfghklpqy",c)?4:c=='i'?3:c=='t'?2:1;}c;s;f(char*i,char*o){s?(e(*i),c=r,e(*o)):(qsort(i,strlen(i),s=1,f),puts(i));return r-c;} Try it online! Out...
Answer
#2: Post edited
- # [C (gcc)], 152 bytes
- <!-- language-all: lang-c -->
- r;e(c){r=c=='j'?5:strchr("bdfghklpqy",c)?4:c=='i'?3:c=='t'?2:1;}c;s;f(char*i,char*o){s?(e(*i),c=r,e(*o)):(qsort(i,strlen(i),s=1,f),puts(i));return r-c;}
- [Try it online!][TIO-l9zmnipy]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-l9zmnipy]: https://tio.run/##Vcy9boMwEAfwnac40QE7ciTox4JlMfUJmi3KQA4TTFKb2KYlRTw7NclQOt397uOP2xPiPFsuCdLRChQiaZPiLXfeYmNJfKzqU3O@dNdbzJAWr/lyoZLi5d74pHjOMz4hd7wm2JR2o9i9GDq6gkiyUZShsCx0htKcXJ2xnigW8i9Sk7B1ImM1ZV3vXSDlVvrearBb5NP8VMlaaQm7948dGSiMsKRD@N4fQMDAHza932dpeuBQk7BjwZSDEymHKYqU9vBZKg3ky6iKRmMEj8C4jCn/w395dWzXA6/wvLY232u2vfNrd@2Pv6lhGU3zLw "C (gcc) – Try It Online"
- Output:
```- a
- aa
- jbita
- kitc
- now
- jtus
- jpyitzx
- ```
- I didn't fine tune it much, but it's delightfully crazy :)
- Explanation:
- - The function `f` takes one input string and one output string as parameter. It returns `int` as per gcc's old C90 default behavior.
- - Since in-place modification of the input should be fine according to Codidacts input/output rules, I actually never use the output but modify the input. (Would crash & burn in case of string literals though, but the challenge only said strings, so I skipped the copy from input to output buffer.)
- - The global variable `s` gets zero-initialized and is used to mark the function's state. If not set, then it's the first call. If set, then the function has been called before. (This will have to be reset to zero externally, in order to execute multiple test cases in a row from main())
- - `s?` checks if `s` is zero, if so execute `qsort` and then print.
- - `qsort` uses the very same function `f` as its own callback to save a function. This abuses the fact that `const void*` and `char*` likely have the same representation. And the function returns `int` so it fulfils the requirements of a qsort callback. Curiously, `qsort` didn't raise a compiler error when passed the wrong type of function pointer (this doesn't sound conforming at all, gcc...).
- - When `qsort` is called, the size of one item parameter also sets the state `s` to 1.
- - `f` gets called by `qsort` many times and now the `s?` evaluates to 1. It calls the helper function `e` to evaluate both parameters (`i` and `o` are now the qsort callback parameters).
- - `e` sets a global variable `r` to value 5,4,3,2 or 1 depending on how which set of characters the passed character belongs to.
- - No benefit from typing out decimal values of these characters since they are all above ASCII value 100, might as well use `'a'` character constants then.
- - The result `r` from the first call to `e` is stored in `c`. Then after the next call `r-c` is returned as the result of qsort callback. This weighs `'j'` as lowest, swap to `c-r` to get the other way around.
- - The function actually always returns `r-c` but when `s==0` nobody cares since the result is placed in the `i` buffer anyway.
- - When `qsort` is done, we are back to the first execution of the function, so `puts` is called once before finishing.
- # [C (gcc)], 152 bytes
- <!-- language-all: lang-c -->
- r;e(c){r=c=='j'?5:strchr("bdfghklpqy",c)?4:c=='i'?3:c=='t'?2:1;}c;s;f(char*i,char*o){s?(e(*i),c=r,e(*o)):(qsort(i,strlen(i),s=1,f),puts(i));return r-c;}
- [Try it online!][TIO-l9zmnipy]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-l9zmnipy]: https://tio.run/##Vcy9boMwEAfwnac40QE7ciTox4JlMfUJmi3KQA4TTFKb2KYlRTw7NclQOt397uOP2xPiPFsuCdLRChQiaZPiLXfeYmNJfKzqU3O@dNdbzJAWr/lyoZLi5d74pHjOMz4hd7wm2JR2o9i9GDq6gkiyUZShsCx0htKcXJ2xnigW8i9Sk7B1ImM1ZV3vXSDlVvrearBb5NP8VMlaaQm7948dGSiMsKRD@N4fQMDAHza932dpeuBQk7BjwZSDEymHKYqU9vBZKg3ky6iKRmMEj8C4jCn/w395dWzXA6/wvLY232u2vfNrd@2Pv6lhGU3zLw "C (gcc) – Try It Online"
- Output:
- ```text
- a
- aa
- jbita
- kitc
- now
- jtus
- jpyitzx
- ```
- I didn't fine tune it much, but it's delightfully crazy :)
- Explanation:
- - The function `f` takes one input string and one output string as parameter. It returns `int` as per gcc's old C90 default behavior.
- - Since in-place modification of the input should be fine according to Codidacts input/output rules, I actually never use the output but modify the input. (Would crash & burn in case of string literals though, but the challenge only said strings, so I skipped the copy from input to output buffer.)
- - The global variable `s` gets zero-initialized and is used to mark the function's state. If not set, then it's the first call. If set, then the function has been called before. (This will have to be reset to zero externally, in order to execute multiple test cases in a row from main())
- - `s?` checks if `s` is zero, if so execute `qsort` and then print.
- - `qsort` uses the very same function `f` as its own callback to save a function. This abuses the fact that `const void*` and `char*` likely have the same representation. And the function returns `int` so it fulfils the requirements of a qsort callback. Curiously, `qsort` didn't raise a compiler error when passed the wrong type of function pointer (this doesn't sound conforming at all, gcc...).
- - When `qsort` is called, the size of one item parameter also sets the state `s` to 1.
- - `f` gets called by `qsort` many times and now the `s?` evaluates to 1. It calls the helper function `e` to evaluate both parameters (`i` and `o` are now the qsort callback parameters).
- - `e` sets a global variable `r` to value 5,4,3,2 or 1 depending on how which set of characters the passed character belongs to.
- - No benefit from typing out decimal values of these characters since they are all above ASCII value 100, might as well use `'a'` character constants then.
- - The result `r` from the first call to `e` is stored in `c`. Then after the next call `r-c` is returned as the result of qsort callback. This weighs `'j'` as lowest, swap to `c-r` to get the other way around.
- - The function actually always returns `r-c` but when `s==0` nobody cares since the result is placed in the `i` buffer anyway.
- - When `qsort` is done, we are back to the first execution of the function, so `puts` is called once before finishing.
#1: Initial revision
# [C (gcc)], 152 bytes <!-- language-all: lang-c --> r;e(c){r=c=='j'?5:strchr("bdfghklpqy",c)?4:c=='i'?3:c=='t'?2:1;}c;s;f(char*i,char*o){s?(e(*i),c=r,e(*o)):(qsort(i,strlen(i),s=1,f),puts(i));return r-c;} [Try it online!][TIO-l9zmnipy] [C (gcc)]: https://gcc.gnu.org/ [TIO-l9zmnipy]: https://tio.run/##Vcy9boMwEAfwnac40QE7ciTox4JlMfUJmi3KQA4TTFKb2KYlRTw7NclQOt397uOP2xPiPFsuCdLRChQiaZPiLXfeYmNJfKzqU3O@dNdbzJAWr/lyoZLi5d74pHjOMz4hd7wm2JR2o9i9GDq6gkiyUZShsCx0htKcXJ2xnigW8i9Sk7B1ImM1ZV3vXSDlVvrearBb5NP8VMlaaQm7948dGSiMsKRD@N4fQMDAHza932dpeuBQk7BjwZSDEymHKYqU9vBZKg3ky6iKRmMEj8C4jCn/w395dWzXA6/wvLY232u2vfNrd@2Pv6lhGU3zLw "C (gcc) – Try It Online" Output: ``` a aa jbita kitc now jtus jpyitzx ``` I didn't fine tune it much, but it's delightfully crazy :) Explanation: - The function `f` takes one input string and one output string as parameter. It returns `int` as per gcc's old C90 default behavior. - Since in-place modification of the input should be fine according to Codidacts input/output rules, I actually never use the output but modify the input. (Would crash & burn in case of string literals though, but the challenge only said strings, so I skipped the copy from input to output buffer.) - The global variable `s` gets zero-initialized and is used to mark the function's state. If not set, then it's the first call. If set, then the function has been called before. (This will have to be reset to zero externally, in order to execute multiple test cases in a row from main()) - `s?` checks if `s` is zero, if so execute `qsort` and then print. - `qsort` uses the very same function `f` as its own callback to save a function. This abuses the fact that `const void*` and `char*` likely have the same representation. And the function returns `int` so it fulfils the requirements of a qsort callback. Curiously, `qsort` didn't raise a compiler error when passed the wrong type of function pointer (this doesn't sound conforming at all, gcc...). - When `qsort` is called, the size of one item parameter also sets the state `s` to 1. - `f` gets called by `qsort` many times and now the `s?` evaluates to 1. It calls the helper function `e` to evaluate both parameters (`i` and `o` are now the qsort callback parameters). - `e` sets a global variable `r` to value 5,4,3,2 or 1 depending on how which set of characters the passed character belongs to. - No benefit from typing out decimal values of these characters since they are all above ASCII value 100, might as well use `'a'` character constants then. - The result `r` from the first call to `e` is stored in `c`. Then after the next call `r-c` is returned as the result of qsort callback. This weighs `'j'` as lowest, swap to `c-r` to get the other way around. - The function actually always returns `r-c` but when `s==0` nobody cares since the result is placed in the `i` buffer anyway. - When `qsort` is done, we are back to the first execution of the function, so `puts` is called once before finishing.