• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion Problem  RSS feed

 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
I need some suggestion or help. I have completed my BSc in CS. Still looking around for Job. But the biggest challenge I am facing is recursion. I can do simple recursion with no problem but when it comes recursion with while or for loop, I get completely lost. Two of my interview I faced with tracing recursion problem. I was able to answer all problems on time but I was so disappoint with recursion problem. I need some suggestion what to do or which direction can help me to get more comfortable with recursion problems.

Thanks.
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hasan,
Recursion and the loops are 2 different things,you can use both one for some repititive work though.if you are not stable in parsing the recursion based problems it simply means you have avoid to use it or say you didn't practice it a lot.
the first thing i will recommend you to have a look at the →description of recursion by Wikipedia.
you can search about →*recursion vs nested loops* in the ranch forum.
I would not call like recursion is slower or takes much memory as it depends on the algorithm and the actual context.but personally i avoid using it if i can achieve it with loop,not every time though as sometime recursion let us to solve the problems in a efficient way along with rendering the readability to the code.
try to find the problems based on recursion on ranch(search) or ask for it to the google and practice and practice a lot.their are some classical problems which are solved using recursion like terms in a fibonacci sequence,evaluating factorial...etc,you can find all this on google.

Hope it helps!

Kind Regards,
Praveen.
 
Paul Clapham
Sheriff
Posts: 22212
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to discuss possible solutions, perhaps you could give us an example where you need to use "recursion with a while or for loop"? On first looking at your post I'm not exactly sure what that means, but an example, I find, tends to clarify things. It's often hard to think abstractly about things which you have no experience of, and that's when talking about concrete examples can be helpful.
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Praveen and Paul. Sorry praveen you understand me wrong. I certainly understand the different between Recursion and Nested Loop. After all it is not possible to finish degree in CS without knowing the main difference between them. But Paul, I appreciate your answer too as you asked me to specify what kind of problem I face little hard. As an example :



I believe I have to practice more on these kind of problem. But any kind of direction from you guys can boost my skill may be.

Thanks
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are struggling with these kind of code...
IMO the cause for it is the style of your coding.you are not following the principles of Object Oriented Principles(java is Object oriented... based).although java is OOPS based it doesn't means it's going to stop you from coding in a procedural style.
look in your code-
the indent style,
how many things does the permute Helper is doing.in fact your method has some redundant code in it for the swapping part.
may be these things are puzzling your mind and making it hard for you to parse the logic...
permuteHelper is doing:
1.)printing the array.
2.)swapping.
3.)calculating the different permutation of an array.
Try this may be it can help:
we can refactor it,see the code below
1.) #printHelper

2.) #swapHelper.

3.)#arrayPermutations

4.) #main

method names can be more better and may be,it can be refactored more better if tried but i just meant
to let you understand the effect of it.
i am still learning these things(i have not enough experience with java or any programming language).
but plenty of members like *junilu* always recommend to avoid the procedural style if you are dealing
with object oriented paradigm,if you didn't then you will get habitual of it.and IMO procedural way of
coding jumbles the code and thus puzzling the ones mind during parsing it.the principles of the object
oriented paradigm helps to get rid of the same problems.

practice with keeping the principles(of OOPs) in mind then you will of course gain the comfortable level.

HIH

Kind regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think their can be one more method:

#print


modified#printHelper
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Praveen for your help. I will try this later today.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!