Answer the question
In order to leave comments, you need to log in
How to "feel" recursion?
Hello. I'm self-taught programming, but I've noticed huge problems with recursion tasks that are slightly heavier than the basic ones.
For example, the second section with recursion tasks on this site:
codingbat.com/java/Recursion-2
The entire section is based on one method ( codingbat.com/prob/p145416 - show solution), the perception of which is not going well for me. I understood it - I drew the principle of its work, but it’s still difficult, I don’t have a sense of its perception, logic; and let's say if we change the condition a little (the next task), I can't understand what needs to be changed in order to apply this method...
I decided to postpone it until later, and solve easier tasks, one of which I came up with for myself during watching this video (but not the formula):
https://youtu.be/UfEiJJGv4CE?t=168
Task: given a natural number N. Find the number of numbers in the interval [0-N] that contain the number "3" in themselves without cycles - using recursion.
And I solved it... but I don't like my solution. I pass two parameters - Number and Current which are exactly the same, and are just buffers for the values used in the part of the code that looks for the number "3" in the current number and the part of the code that steps back from the value N to 0.
public class ThreeCounter {
public static int ThreeCounter(int number, int current) {
//Base case -- алгоритм прошел по всем числам
if(current == 0) {
return 0;
}
//Base case? второй рекурсии -- алгоритм прошел по всем цифрам текущего числа
if (number == 0) {
return ThreeCounter(current-1, current - 1);
}
if(number % 10 == 3) {
return 1 + ThreeCounter(current-1, current - 1);
}
return ThreeCounter(number/10, current);
}
public static void main(String[] args) {
int n = 100;
System.out.println(ThreeCounter(n, n));
}
}
Answer the question
In order to leave comments, you need to log in
How to make this code "correct" because I don't think it would be wise to constantly write: "How many Threes (same number, same number)"?
private static int ThreeCounterImpl(int number, int current) { ... }
public static int ThreeCounter(int number) { return ThreeCounterImpl(number, number); }
I had exactly the same problem at one time, I also did not fully understand, as a result, the concept completely came when I made a simple method of recursive yaw through files.
Algorithm example:
функция_поиска( url ){
если (url.файл()) тогда *делаем нужное к примеру смотрим расширение или имя*
иначе цикл_перебора_содержимого_каталога вызов функция_поиска( url )
Try to write programs with a more visual result, preferably graphical. Tree traversal, tree generation, Peano curve generation, Koch snowflake generation, Sierpinski triangle generation. Play with the options. These are very simple algorithms in which, apart from recursion, there is nothing at all.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question