Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
What do you mean by iterating input? You need to read all input to memory to produce all substrings.
-
@AmbientTea Ah, sorry, I need to learn to communicate to the voices outside of my head better.
I mean, in the case of needing to find all the possible substrings of a string, you can't just loop over the input in one pass because you need at least two pointers at any one time (for the start and end of the substring) and both need to move.
So for getting all of the anagrams of a string, you might have an outer loop starting at 0 and ending before the end of the array and an inner loop from the index of the outer loop + 1 to the end of the array.
But sometimes you're in a situation where it calls for the inner loop of a nested loop function to end at the index of the outer loop, or to begin at the opposite end of the input than the outer loop.
Was just wondering if any of you just have a cool way of remembering how to structure nested loops and how to determine if you even need a nested loop in the first place. -
Well you determine it the way you did. You need two pointers so there's gonna be two loops.
for tips, a common approach is to always have the left/lower bound inclusive and the right/upper bound exclusive, so you loop through [x, y). If you don't need to it's best to have all loops go in the same direction too and use the same comparator, so no mixing < > <= etc. Best if your language supports ranges instead of C for ofc.
Dunno if that's helpful, i don't have many opportunities to write loops. -
iiii92194yIsn't it, like, a trivial task?
First iterator goes from start to end one time. Second iterator goes from current position of the first to end after each step of the first iterator.
Not a very efficient stuff as it is quadratic relative to the length of the array. -
Voxera115854y@AmyShackles with anagrams I assume you mean permutations?
I would use a recursive method with a loop.
The method loops over all chars in the current input and for each makes a call to it self with the rest of the chars.
And for each char you then prepend it to every result from the call.
Aggregate all results from all calls in the loop and return a list (possibly removing any duplicates to lessen memory consumption)
When the last method returns you have all.
Might not be the fastest or most efficient but it should work. -
@Voxera Sorry, what I meant was... I spent a good deal of time trying to figure out how to solve this problem and went through a lot of trial and error before I figured it out: https://hackerrank.com/challenges/...
And I’ve realized that I generally have trouble working out the solutions to problems that require iterating through input through multiple passes in general. 2D matrices are the subject of many a nightmare.
So I was really more wondering if there’s some stupid trick to it that I’ve somehow missed.
And yes, @iiii, you’re right. It _is_ probably a trivial task. Believe me when I say I felt like an idiot asking in the first place. -
Voxera115854y@AmyShackles ok:)
I usually try to solve a very simple version using pen and paper to get an understanding of the problem.
Then use code to scale up to whatever I need.
But mostly I think it is a matter of doing it often enough to learn a number of methods that you can easily use to break a problem down.
I have used codewars to get fun challenges to train on. -
iiii92194y@AmyShackles that's a curious task. Overall it is not as trivial as just taking all the substrings.
-
iiii92194yI see at least one trick: if there are two short anagrammatic strings then substrings which include them and a substring between those two are anagrammatic as well.
For example "ifai". "i" and "i" are anagrammatic, so if you include the part "fa" in-between, you'll get another pair "ifa" and "fai"
Related Rants
Y’all wouldn’t happen to have some handy mental model for remembering how to iterate through input without being an idiot about it, would you?
Referring to problems like having to get all possible substrings from a given string, etc.
Wishful thinking on my part, probably, but I figure it doesn’t hurt to ask. <3
question
matrices
nested arrays
arrays
help me devrant homies you’re my only hope