I am stuck at a task , does it mean i am not logically good?

Hi,

I feel really disappointed today, i was stuck at task https://www.freecodecamp.org/challenges/no-repeats-please

So i went through tutorials and videos for the permutation …

I came across this solution that if i have say abcdefa , i can take 7! and subtract it with possible combination of “aa” together if they are unique … that is 6! + 6! , i subtract this with …So if i take 7! its 5040 , i then subtract it with 6!X2! = 5040- 1440 = 3600 , which came as correct so i started writing down coding algo for this …

But soon i found it worked only when i had one alphabet appear only twice then i tried with aaabb, here i started getting totally different results - as total is 120 , i subtract 4! + 4! for taking b as one so i subtract 120- 48 = 72 now i have the 3 a , so i take them as one “aaa” , now i have 3 unique character left considering “aaa” is one, this in itself can have 3! combinations so i do3! X 3! so i have 36 here …

Now i have 120 - 48 -36 = 36 but the FCC answer is 12 …

I went through videos and links like here - https://stackoverflow.com/questions/32282607/permutations-excluding-repeated-characters/36178855#36178855 but could not understand much

Gone through several youtube videos still i can understand the repeat and non repeat formulae like n!/n! - r!

or n!/n1!.n2! etc , but not what to do with this problem, what can i do next, it seems i am always stuck when some thing difficult comes up …Kindly guide …

Thanks

Yeah, that one was probably the one I had the most difficulty with. It’s some difficult math and a complex algorithm. I’m pretty good with both of those, but that challenge was especially tough. It’s time for bed now but if no one else has jemped in, maybe I can give some guidance tomorrow.

But don’t feel like this means you can’t code or anything. That is a really tough challenge. It took me several tries over the span of a week, doing a lot of research. I seem to remember that as the challenge that challenged me the most.

1 Like

@ksjazzguitar - Thanks , its not like other challenges were a breeze for me , i struggled but here i have more struggle then ever, also i am now really worried that if i am so much struggling with tasks how will i do the 4 mega projects, i was really excited a few days back but now scared …

Like @ksjazzguitar, I personally find that challenge to be rather tough, too and I don’t think you should feel disappointed because you haven’t worked it out yet.

I also attempted to solve it numerically like you did with effectively the same approach, but only got as far as having a general solution for when letters are repeated no more than once. Assuming you want to keep trying, here are a couple of hints/comments about the problem that you specifically pointed out:

Return the number of total permutations of the provided string that don’t have repeated consecutive letters.

  • Note again that in the instructions that letters cannot be repeated. Using the aaabb example, you haven’t actually accounted for the permutations where only two a’s are next to each other, such as (aa)bab
  • It get tricky here because some of all of the possible permutations for (aa)bab (4! x 2 = 48) are already part of (aaa)bb and aaa(bb). I the context of (aa)abb, There are 6 x 2! = 12 permutations where a is next to (aa)—2 cases when (aa) is at the ends (the remaining a can only be on one side) and 2 x 2 cases when (aa) is anywhere, the 2! accounts for the interchangeable positions of the unique a’s inside (aa). Similarly, there are 3! x 2! = 12 of such cases for (aa)a(bb). That is, you haven’t accounted for 48 - 12 - 12 = 24 cases. You probably can see at this point that subtracting these 24 cases lead you to the answer of 12

I personally liked the idea of solving it numerically so I started doing that to being with (even though I failed)—but you don’t necessarily have to solve it numerically. In fact, if all fails, you can always simply compute all possible permutations and filter out the ones with the same letters next to each other (for example, try Googling for “algorithm for generating permutations”—I would advise not to include JavaScript as your search terms, though, because being able to understand an algorithm and implement it properly is still a skill, otherwise you would just be effectively copying). Brute-forcing it is not as elegant, but it gets the job done.

I’m not suggesting that you should always find the easy way out, but don’t forget that your goal (I assume) is to learn to code well enough to solve problems. So try not to distract yourself and risk losing momentum and/or driving yourself into a corner by trying to come up with the greatest, most-elegant algorithm now when there is a safe, obvious way to get the job done—I’m not discouraging you from coming up with a great solution, it’s just that you can always revisit this and it’s probably shouldn’t be your priority right now.

I hope I don’t come across as abrasive, I’m just speaking from personal experience and I hope it helps. :slight_smile:

1 Like

Hi again.

Yes, a simple mathematical formula to solve this does have a certain appeal. But this is a class in programming, not in advanced mathematics. In the real world, the mathematical solution would be superior, especially if you were dealing with super large data sets. But this is not the real world - it is a class in javascript coding.

I think the intent of the problem is that you generate all the possible permutations and then filter out the ones that have repeats. So, can you find a permutation function? If you’ve tried, then it’s OK to look around and see if you can find one to adapt. Programmers do that all the time. Just make sure you understand what is happening. Never use borrowed code unless you understand exactly what it is doing. Most permutation solutions involve recursion, so this is a great chance to learn about that. Try first, but don’t be afraid to look for help.

Once you have your array of permutations, then you need to filter out the ones with repeats. Of course, you could right your own function that checks for repeats. Or you could just use RegEx. Using either of those with a filter() function will give you an array of acceptable permutations, and then you can just return the length of the resultatant array - an array of permutations without repeats.

So see if you can generate the permutations - that is the hardest part of the whole thing. If you run into trouble, post some code and we’ll see if we can help.

1 Like

I’m having trouble with that one as well. My first try was dodgy because I converted numbers to letters too early in the process and ended up with an array of permutations that had *2 extra combinations per repeated letter. I could have cleaned it up with maths I suppose, but I wanted something neater. So I wrote another solution that filtered out all the rubbish earlier on. Codepen didn’t like that because it said my loop was taking too long, so I switched to Brackets and used the Chrome console. It worked fine and came up with all the correct answers for the test strings. So I went to the challenge page and pasted my solution into the editor, but it failed on the two longest strings. I assume the editor didn’t like my loops either and timed out on them. So I’m back to the start again. This time I’ll try to generate less permutations so the filtering loops take less time. Yeah this is a difficult challenge. That cash register challenge was a pain as well thanks to the weird way computers handle decimal fractions.

1 Like

@honmanyau - thanks, i am going through some algorithm chapters right now online, will work on that and then come back, thanks for the very detailed explanation …

@ksjazzguitar - i will post some code soon …

@CodeNooooob - do let us know your solution, i will post mine too …

Regards

My solution was to get the string length, let’s say that was 7 characters, and use that to set up a loop to get all the decimals between the bottom end of that and the top. So the loop went from 1234567 to 7654321. I took the decimal value of the loop and spilt it into an array of strings. Then I filtered out any arrays that had 0, 8 or 9 in them, and any arrays that had repeated numbers. Next I swapped the array decimal values for their corresponding letters from the string, so “aaabdcf” would be: a 1, a 2, a 3, b 4, etc. Then all I had to do was scan the arrays again and filter out any that had two letters next to each other that were the same and return the length of the array of arrays. Worked fine in Chrome, but the Freecodecamp editor doesn’t like it. My code is primitive and ugly, but there it is if you really want to look at it: https://codepen.io/Code_Noob_/pen/MEevgo?editors=0010. It won’t run on Codepen either though.

But anyway, I have to start again. I was thinking of maybe cooking up some sort of custom number base, but I can’t get that working yet. Not sure how to approach this now as it seems a major concern is speed.

I was looking forward to the advanced front end projects as well like you were, but it looks like a week of misery lies ahead instead. Good luck anyway.