Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/leet/ challenge #1: permutation sequence

Name: Anonymous 2021-03-21 19:34

Implement
next permutation
, which rearranges numbers (nums) into the lexicographically next greater permutation of numbers.
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
The replacement must be in place and use only constant extra memory.

Example 1:
Input:[1,2,3]
Output: [1,3,2]

Example 2:
Input: [3,2,1]
Output: [1,2,3]

Example 3:
Input: [1,1,5]
Output: [1,5,1]

Example 4:
Input: [1]
Output: [1]

Constraints:
1 <= nums.length <= 10e6
0 <= nums[i] <= 2147483647



Shortest code wins bonus prize, but the main prize goes to every post with actually functioning code.

The deadline is 691200 seconds from this post's timestamp.

Name: Anonymous 2021-03-21 21:12

>>1
Anus kys

Name: Anonymous 2021-03-22 4:04

Do your own homework.

Name: Anonymous 2021-03-22 6:14

Faggot black progger

Name: Anonymous 2021-03-22 6:56

>>2
Not yet, but I might pretty soon.
>>3
Not my homework. I'm a 41yo admin getting paid for shitposting and jerking off.

Name: Anonymous 2021-03-22 14:58

The replacement must be in place and use only constant extra memory.
Haskell and Bash are excluded.

Name: Anonymous 2021-03-22 17:49

#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int next(int *a, int m, int c) {
int n = 0;
for (int i = 0; i < m; i++)
if (a[i] < a[n] && a[i] > c) n = i;
return n;
}
int main(int argc, char **argv) {
const int s = argc - 1;
int * a = malloc(sizeof(int) * s);
for(int i = 0; i < s; i++) a[i] = atoi(argv[i + 1]);
int i = s - 1;
while (i > 0) {
if (a[i] > a[i - 1]) {
int nxt = next(&a[i], s - i, a[i - 1]);
int tmp = a[i - 1];
a[i - 1] = a[i + nxt];
a[i + nxt] = tmp;
break;
}
i--;
}
qsort(a + i, s - i, sizeof(i), cmp);
for (int i = 0; i < s; i++) printf("%d ", a[i]);
printf("\n");
free(a);
return EXIT_SUCCESS;
}


Tests:
$ ./a.out 1
1
$ ./a.out 1 1 5
1 5 1
$ echo 1 2 3 | tee -a res | xargs ./a.out | tee -a res | xargs ./a.out | tee -a res | xargs ./a.out | tee -a res | xargs ./a.out | tee -a res | xargs ./a.out | tee -a res | xargs ./a.out >> res
$ cat res
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1 2 3

And yes you can have 10e6 arguments on my machine at least
$ getconf ARG_MAX
2097152

Name: Anonymous 2021-03-22 19:35

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main(int argc,char**argv){
vector<string> arguments(argv + 1, argv + argc);
next_permutation(begin(arguments),end(arguments));
for(size_t i;i<arguments.size();i++)cout<<arguments[i];
;return 0;}

Name: Anonymous 2021-03-22 20:45

>>8
You forget include void.h

Name: Anonymous 2021-03-22 22:06

FrozenAnus, challenges are more fun if you make the algorithm yourself instead of googling for a library that already includes it.

Name: Anonymous 2021-03-22 23:57

perhaps its not a "challenge" or "fun" if its reimplementing
standard library function #93535 ? Is it fun to re-implement sprintf?

Name: Anonymous 2021-03-23 0:04

>>10
Anonymous , challenges are more fun if you make the Challenge yourself instead of copypasting from web interview questions or code golfing sites.

Name: Anonymous 2021-03-23 0:12

>>11
Yeah you were totally aware of the existence of the C++ next_permutation function, because you so often use it in your personal C++ projects. You totally didn't google it.
Anyway, it's nice to see some code on /prog/ for a change. Maybe the next challenges will be less trivial?

Name: Anonymous 2021-03-23 0:15

>>12
No Google involved. Found the algorithm myself, which isn't exactly hard.

Name: Anonymous 2021-03-23 6:36

>>12
I made the BBCode golf challenge myself (based on a piece of /prague/ culture). It was original, thematic, simple, and memetic, and yet nobody took part in it.

Name: Anonymous 2021-03-23 7:48

>>15
1.it was too simple and boring code golfin'.
2.some people dislike BBCode posting or find it annoying(spoilers, stacking multiple lines with super/sub text)
3.People don't play this BBCode golf unless they have severe autism.

Name: Anonymous 2021-03-23 8:05

>>16
3.People don't play this BBCode golf unless they have severe autism.
0.85714285714 of this forum has severe autism (the rest has schizophrenia)

Name: Anonymous 2021-03-23 8:36

>>16
Post your own challenge. I’ll take part.

Name: Anonymous 2021-03-23 15:05

>>18
Posting an original challenge is quite a challenge.

Name: Anonymous 2021-03-23 22:00

>>18
Here you go, tough guy. https://textboard.org/prog/100 https://lainchan.org/%CE%BB/res/2036.html#19177
(before that one anon asks again, no, it's not mine)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List