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

Write a program to print all permutations of a string.

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 35
Thread images: 4

File: se_then_se_now.png (171KB, 800x600px) Image search: [Google]
se_then_se_now.png
171KB, 800x600px
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 (622KB, 900x900px) Image search: [Google]
Dean (4).png
622KB, 900x900px
>>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 (242KB, 591x283px) Image search: [Google]
Dean (5).png
242KB, 591x283px
>>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 (62KB, 1256x364px) Image search: [Google]
perms.png
62KB, 1256x364px
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 posts: 35
Thread images: 4


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.