Recursion


I was trying to do some simple exercises on recursive functions:



1. Write a function using Recursion to print numbers from n to 0.



My solution:



```
int integer(int n)
{
      if(n>=0)
      {
           printf("\n%d",n);
           integer(--n);
       }
}
```

The next exercise was



2. Write a function using Recursion to print numbers from 0 to n. (You just need to shift one line in the program of problem 1.)



Well i dont know how to do it using recursion; at least not by shifting just one line of the previous code. And also did not want to use any other variables.  

Is there any simple solution to the 2nd problem. Help me out?
```
[b]#include[/b] <stdio.h> 

[b]void[/b] [b]foo[/b]([b]int[/b] n)
{
   [b]if[/b] ( n >= 0 )
   {
	  [b]printf[/b]("%d ", n);
	  [b]foo[/b](n - 1);
   }
}

[b]void bar[/b]([b]int[/b] n)
{
   [b]if[/b] ( n >= 0 )
   {
	  [b]bar[/b](n - 1);
	  [b]printf[/b]("%d ", n);
   }
}

[b]int main[/b]() 
{
   [b]int[/b] value = 5;
   [b]foo[/b](value); [b]putchar[/b]('\n');
   [b]bar[/b](value); [b]putchar[/b]('\n');
   [b]return[/b] 0; 
}

/* my output
5 4 3 2 1 0 
0 1 2 3 4 5 
*/
```
you can use the repmat function here
if you have to print the patter n (here 10) times, use the following code:
a = [5 4 3 2 1 0]';
n = 10;
b =repmat(a, n,1) c = input()  

t = len(c)  

temp = ""  

for i in range(0,len(c)):  

    if t<3:  

        break  

    else:  

        if i in (0,1,t-2,t-1):  

            temp = temp+c[i]  

        else:  

            continue  

print(temp) applying and factorization, we get, also changing by reversing n and dnote: - try to factorize that term which help in reducing expression. Here's an example of one possibility. Just season to taste... 



```
extends Node2D
var x_delta = 0

func _physics_process(delta):
    var input_velocity = Vector2.ZERO

    if Input.is_action_just_pressed("ui_right"):
        x_delta = 1

    if Input.is_action_just_pressed("ui_left"):
        x_delta = -1

    if Input.is_action_just_released("ui_right") && x_delta > 0:
        x_delta = 0

    if Input.is_action_just_released("ui_left") && x_delta < 0:
        x_delta = 0

    input_velocity.x = x_delta

    print(input_velocity.x)

``` not [full disclosure] a linux person per se.

something similar to the following should work:

https://www.unix.com/shell-programming-and-scripting/68166-converting-integer-string.html

start simple with just enough code to test the concept.

see if it works and modify/correct as necessary.

then if it still does not work, then post your documented code accordingly along an explanation of what is or is not happening as you expect or desire. direction: a word and number arrangement machine when given an input line of word and numbers rearranges them following a particular rule in each step. the following is an illustration of input and rearrangement. input: politics 61 policy 116 partner 93 role 45 clear 27 38 point step i: role politics policy 116 partner 93 45 clear 27 38 point 61 step ii: role clear politics policy partner 93 45 27 38 point 116 61 step iii: role clear partner politics policy 93 45 38 point 27 116 61 step iv: role clear partner politics policy 93 38 point 45 27 116 61 step v: role clear partner politics point policy 93 38 45 27 116 61 step v, is the last step of the above arrangement as the intended arrangement is obtained. as per the rule followed in the given steps find out the appropriate steps for the given input. input: energy 54 78 place 23 issue 211 145 flat rent taxi 17in step iii, what is the difference between fifth element from left and second element from right? The existing solutions based on `findall` are fine for non-overlapping matches (and no doubt optimal except maybe for HUGE number of matches), although alternatives such as `sum(1 for m in re.finditer(thepattern, thestring))` (to avoid ever materializing the list when all you care about is the count) are also quite possible. Somewhat idiosyncratic would be using `subn` and ignoring the resulting string...:



```
def countnonoverlappingrematches(pattern, thestring):
  return re.subn(pattern, '', thestring)[1]

```

the only real advantage of this latter idea would come if you only cared to count (say) up to 100 matches; then, `re.subn(pattern, '', thestring, 100)[1]` might be practical (returning 100 whether there are 100 matches, or 1000, or even larger numbers).


Counting **overlapping** matches requires you to write more code, because the built-in functions in question are all focused on NON-overlapping matches. There's also a problem of definition, e.g, with pattern being `'a+'` and thestring being `'aa'`, would you consider this to be just one match, or three (the first `a`, the second one, both of them), or...?


Assuming for example that you want possibly-overlapping matches *starting at distinct spots in the string* (which then would give TWO matches for the example in the previous paragraph):



```
def countoverlappingdistinct(pattern, thestring):
  total = 0
  start = 0
  there = re.compile(pattern)
  while True:
    mo = there.search(thestring, start)
    if mo is None: return total
    total += 1
    start = 1 + mo.start()

```

Note that you do have to compile the pattern into a RE object in this case: function `re.search` does not accept a `start` argument (starting position for the search) the way *method* `search` does, so you'd have to be slicing thestring as you go -- definitely more effort than just having the next search start at the next possible distinct starting point, which is what I'm doing in this function. matlab does not need a |return| statement to return a value. to return a value you simply need to list it as an output argument of the function:

 function [output1,output2,...] = fun\_name(input1,input2,...)

you have specified an output argument |rad|, but then have totally ignored this inside your code an never allocated any value to it. you should try something like this:

 function rad = radial(n,m);
 rad = ... whatever rad is;
 end

of course this is explained quite clearly in the introductory tutorials:

<http://www.mathworks.com/help/matlab/getting-started-with-matlab.html>

and also the <http://www.mathworks.com/help/matlab/ref/function.html |function|> documentation:

<http://www.mathworks.com/help/matlab/ref/function.html>

read the documentation. it tells you how matlab works.

if you want to store all of the values of |rad| in a loop then you can use indexing. (a) for digit



expression : 5n + 1

where n = no of digits

for 5 digits

no of segments required = 5(5) + 1 = 25 + 1 = 26

for 10 digits

no of segments required = 5(10) + 1 = 50 + 1 = 51

for 100 digits

no of segments required = 5(100) + 1 = 500 + 1 = 501

(b) for digit



expression : 3n + 1

where n = no of digits

for 5 digits

no of segments required = 3(5) + 1 = 15 + 1 = 16

for 10 digits

no of segments required = 3(10) + 1 = 30 + 1 = 31

for 100 digits

no of segments required = 3(100) + 1 = 300 + 1 = 301

(c) for digit



expression : 5n + 2

where n = no of digits

for 5 digits

no of segments required = 5(5) + 2 = 25 + 2 = 27

for 10 digits

no of segments required = 5(10) + 2 = 50 + 2 = 52

for 100 digits

no of segments required = 5(100) + 2 = 500 + 2 = 502 as long as you write down the hierarchy manually, there’s certainly the possibility to simplify such transitive from implementations with a macro. [i’ve written one here, feel free to take a look.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=337cce675aa28e99ac030ca783c1c155) going even further one could try to use procedural macros with global state that uses annotations on the…