[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Write a program to print all permutations of a string.
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 35
Thread images: 4
File: se_then_se_now.png (171 KB, 800x600) Image search: [Google]
se_then_se_now.png
171 KB, 800x600
So I found out that my GF cheated on me right before my final interview.
I was pretty fucking distraught and couldn't answer the question. I'm certain I failed the interview and won't get the job.

And now, I'm trying to get my mind off of life, so...
>Write a program to print all permutations of a string.
I don't care what language it is.
[spoiler]I'm so fucking out of it atm I can't to do it in either C, JS, or Python.[/spoiler]
>>
>>55263758
take homework threads to >>>/wsr/ you stupid nigger.
>>
File: Dean (4).png (622 KB, 900x900) Image search: [Google]
Dean (4).png
622 KB, 900x900
>>55263778
Fuck off, nigger. I just want some /g/ interaction. This shit is easily Googlable.
>>
>>55263796
>>55263758
no, you fuck off cuck
>>
File: Dean (5).png (242 KB, 591x283) Image search: [Google]
Dean (5).png
242 KB, 591x283
>>55263816
Let's both fuck off together, then.
I've got, what, like, a third of Cpt Morgan left?
How about you, anon, what's your drink of choice?
>>
Why the fuck did I even do this?

#include <string>
#include <algorithm>

auto main() -> int{
std::string str{"cuckold"};
std::sort(begin(str), end(str));
do{
std::cout << str << '\n';
} while(std::next_permutation(begin(str), end(str)));
}
>>
File: perms.png (62 KB, 1256x364) Image search: [Google]
perms.png
62 KB, 1256x364
beat this
>>
>>55264509
it wont run faggot
>>
>>55264567
> write a program
> writes a script
:^)
>>
>>55264636
Well, it's not like I tested it. but whats wrong with it?
>>
>>55264567
there is a permutations library you melt
>>
>>55264642
OP implicitly specified python as a valid language to use, also

>>55264676
the whole point of these exercises are to write algorithms that actually do the heavy footwork, not to look up libraries
>>
>>55264660
Your begin(str), end(str) probably should be str.begin(), str.end()
>>
>>55264567

from math import factorial

def perm(s, i):
if len(s) <= 1:
return s
x, y = divmod(i, factorial(len(s)-1))
return s[x] + perm(s[:x] + s[x+1:], y)

s1 = 'abcdef'
for i in range(factorial(len(s1))):
print(perm(s1, i))
>>
the divmod line can be just
y, x = divmod(i, len(s))
but doing it this way preserves lexicographic order for sorted inputs.
>>
Some langues have built in permutation functions
>>
>>55263758
>gf
>my
>>
>>55264642
>using the smiley with a carat nose
>>
>>55264509
Is this C++? What happened?
>>
>>55264841
What even is ADL?
>>
>>55264709
pythons never a valid language
>>
>>55263758
pic was funny and accurate... till the linux is good for le programming meme. maybe i'll edit it later
>>
main = fmap permutations getLine
>>
>>55266281
Sure but the other guy probably wasn't compiling with c++11, hence the wouldn't run.
>>
Its Sunday and my brain is turned off on weekends but I gave it a shot

private static List<string> Permutations(string arg) {
char[] arr = arg.ToCharArray();

int distinctChars = arr.AsEnumerable().Distinct();
int length = arr.Length;
int verifyCount = distinctChars * length;

List<string> permutations = new List<string>();

for (int i = 0; i < length; i++) {
for (int x = 0; x < distinctChars; x < distinctChars) {

permutations.Add(Swap(arr, i, x));

}
}
return permutations;
}
private static string Swap(char[] toSwap, int indexFirst, int indexSecond) {
char first = toSwap[indexFirst];

toSwap[indexFirst] = toSwap[indexSecond];
toSwap[indexSecond] = first;

return toSwap;
}
>>
>>55263758
Holy shit

I literally don't know how to do this.
>>
>>55271275
Just use Visual C#'s String.MapPermutations() method.

Programming is so easy.
>>
>>55263758
>>55263796
>>55263849


>gf cheated on me
>im drunk on 4chan
>using these gay "cool guy" reactions

i hope your girlfriend is getting the man she deserves right now
>>
>>55263758
>Write a program to print all permutations of a string.
λ permutations "gtfo"
["gtfo","tgfo","ftgo","tfgo","fgto","gfto","oftg","fotg","ftog","otfg","tofg","tfog","ogtf","gotf","gtof","otgf","togf","tgof","ogft","goft","gfot","ofgt","fogt","fgot"]

done.

If you ban the use of that library function:
λ let perms [] = [[]]; perms s = do x <- s; fmap (x:) $ perms (s \\ [x])
λ perms "gtfo"
["gtfo","gtof","gfto","gfot","gotf","goft","tgfo","tgof","tfgo","tfog","togf","tofg","fgto","fgot","ftgo","ftog","fogt","fotg","ogtf","ogft","otgf","otfg","ofgt","oftg"]


took me a minute max, where's my job?
>>
>>55263758
>that picture
The top image sounds like my day job.

So, so glad I got out of webdev...
>>
>>55272311
I posted >>55272237 as a joke because I'm pretty sure OP intended for us to figure out the logic ourselves.
>>
>>55264567
This will print the same permutation multiple times. For example perms("aab") will print:
aab
aba
aab
aba
baa
baa
>>
>>55272608
That's how most “permutations” functions I know work, including the one from the Haskell stdlib.

Besides, the goal was not to print all *unique* permutations, just to print all of them. Technically, duplicates would have been allowed. (And technically, you could even just submit a program that prints all strings)
>>
>>55270747
So is this the most efficient one? It looks like a lot of the other ones in this thread have repetitions
>>
>>55272383
it's sad because it's true

i hpoe that vulkan and HSA will slightly change this
Thread replies: 35
Thread images: 4

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.