Skip to content

Commit 6731005

Browse files
committed
Solution to tasks
1 parent ec15e6b commit 6731005

File tree

12 files changed

+320
-0
lines changed

12 files changed

+320
-0
lines changed

0x08-recursion/100-is_palindrome.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "main.h"
2+
3+
/**
4+
* _strlen_recursion - returns the length of a string
5+
* @s: string to check
6+
* Return: length of string
7+
*/
8+
int _strlen_recursion(char *s)
9+
{
10+
if (*s == '\0')
11+
return (0);
12+
return (1 + _strlen_recursion(s + 1));
13+
}
14+
15+
/**
16+
* check_palindrome - checks if a string is a palindrome
17+
* @s: string to check
18+
* @len: length of string
19+
* Return: 1 if palindrome, 0 if not
20+
*/
21+
int check_palindrome(char *s, int len)
22+
{
23+
if (len <= 1)
24+
return (1);
25+
if (*s != *(s + len - 1))
26+
return (0);
27+
return (check_palindrome(s + 1, len - 2));
28+
}
29+
30+
/**
31+
* is_palindrome - checks if a string is a palindrome
32+
* @s: string to check
33+
* Return: 1 if palindrome, 0 if not
34+
*/
35+
int is_palindrome(char *s)
36+
{
37+
int len = 0;
38+
39+
if (*s == '\0')
40+
return (1);
41+
len = _strlen_recursion(s);
42+
return (check_palindrome(s, len));
43+
}

0x08-recursion/100-main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
int r;
12+
13+
r = is_palindrome("level");
14+
printf("%d\n", r);
15+
r = is_palindrome("redder");
16+
printf("%d\n", r);
17+
r = is_palindrome("test");
18+
printf("%d\n", r);
19+
r = is_palindrome("step on no pets");
20+
printf("%d\n", r);
21+
return (0);
22+
}

0x08-recursion/101-main.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
int r;
12+
13+
r = wildcmp("main.c", "*.c");
14+
printf("%d\n", r);
15+
r = wildcmp("main.c", "m*a*i*n*.*c*");
16+
printf("%d\n", r);
17+
r = wildcmp("main.c", "main.c");
18+
printf("%d\n", r);
19+
r = wildcmp("main.c", "m*c");
20+
printf("%d\n", r);
21+
r = wildcmp("main.c", "ma********************************c");
22+
printf("%d\n", r);
23+
r = wildcmp("main.c", "*");
24+
printf("%d\n", r);
25+
r = wildcmp("main.c", "***");
26+
printf("%d\n", r);
27+
r = wildcmp("main.c", "m.*c");
28+
printf("%d\n", r);
29+
r = wildcmp("main.c", "**.*c");
30+
printf("%d\n", r);
31+
r = wildcmp("main-main.c", "ma*in.c");
32+
printf("%d\n", r);
33+
r = wildcmp("main", "main*d");
34+
printf("%d\n", r);
35+
r = wildcmp("abc", "*b");
36+
printf("%d\n", r);
37+
return (0);
38+
}

0x08-recursion/101-wildcmp.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "main.h"
2+
3+
/**
4+
* wildcmp - compares two strings and returns 1 if the strings can be
5+
* considered identical, otherwise return 0
6+
* @s1: string to compare
7+
* @s2: string to compare
8+
* Return: 1 if identical, 0 if not
9+
*/
10+
int wildcmp(char *s1, char *s2)
11+
{
12+
if (!*s1 && !*s2)
13+
return (1);
14+
if (*s2 == '*')
15+
return (wildcmp(s1, s2 + 1) || (*s1 != '\0' && wildcmp(s1 + 1, s2)));
16+
if (*s1 == *s2)
17+
return (wildcmp(s1 + 1, s2 + 1));
18+
return (0);
19+
}

0x08-recursion/3-factorial.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "main.h"
2+
3+
/**
4+
* factorial - returns the factorial of a given number
5+
* @n: number to be factored
6+
* Return: factorial of n
7+
*/
8+
int factorial(int n)
9+
{
10+
if (n < 0)
11+
return (-1);
12+
if (n == 0)
13+
return (1);
14+
return (n * factorial(n - 1));
15+
}

0x08-recursion/3-main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
int r;
12+
13+
r = factorial(1);
14+
printf("%d\n", r);
15+
r = factorial(5);
16+
printf("%d\n", r);
17+
r = factorial(10);
18+
printf("%d\n", r);
19+
r = factorial(-1024);
20+
printf("%d\n", r);
21+
return (0);
22+
}

0x08-recursion/4-main.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
int r;
12+
13+
r = _pow_recursion(1, 10);
14+
printf("%d\n", r);
15+
r = _pow_recursion(1024, 0);
16+
printf("%d\n", r);
17+
r = _pow_recursion(2, 16);
18+
printf("%d\n", r);
19+
r = _pow_recursion(5, 2);
20+
printf("%d\n", r);
21+
r = _pow_recursion(5, -2);
22+
printf("%d\n", r);
23+
r = _pow_recursion(-5, 3);
24+
printf("%d\n", r);
25+
return (0);
26+
}

0x08-recursion/4-pow_recursion.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "main.h"
2+
3+
/**
4+
* _pow_recursion - returns the value of x raised to the power of y
5+
* @x: base
6+
* @y: exponent
7+
*
8+
* Return: x raised to the power of y
9+
*/
10+
int _pow_recursion(int x, int y)
11+
{
12+
if (y < 0)
13+
return (-1);
14+
if (y == 0)
15+
return (1);
16+
return (x * _pow_recursion(x, y - 1));
17+
}

0x08-recursion/5-main.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
int r;
12+
13+
r = _sqrt_recursion(1);
14+
printf("%d\n", r);
15+
r = _sqrt_recursion(1024);
16+
printf("%d\n", r);
17+
r = _sqrt_recursion(16);
18+
printf("%d\n", r);
19+
r = _sqrt_recursion(17);
20+
printf("%d\n", r);
21+
r = _sqrt_recursion(25);
22+
printf("%d\n", r);
23+
r = _sqrt_recursion(-1);
24+
printf("%d\n", r);
25+
return (0);
26+
}

0x08-recursion/5-sqrt_recursion.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "main.h"
2+
3+
/**
4+
* _sqrt_helper - helper function for _sqrt_recursion
5+
* @n: number
6+
* @i: incrementer
7+
*
8+
* Return: natural square root of n
9+
*/
10+
int _sqrt_helper(int n, int i)
11+
{
12+
if (i * i == n)
13+
return (i);
14+
if (i * i > n)
15+
return (-1);
16+
return (_sqrt_helper(n, i + 1));
17+
}
18+
19+
/**
20+
* _sqrt_recursion - returns the natural square root of a number
21+
* @n: number
22+
*
23+
* Return: natural square root of n
24+
*/
25+
int _sqrt_recursion(int n)
26+
{
27+
if (n < 0)
28+
return (-1);
29+
return (_sqrt_helper(n, 0));
30+
}

0x08-recursion/6-is_prime_number.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "main.h"
2+
3+
/**
4+
* is_prime_helper - helper function for is_prime_number
5+
* @n: number
6+
* @i: incrementer
7+
*
8+
* Return: 1 if n is a prime number, otherwise 0
9+
*/
10+
int is_prime_helper(int n, int i)
11+
{
12+
if (i == n)
13+
return (1);
14+
if (n % i == 0)
15+
return (0);
16+
return (is_prime_helper(n, i + 1));
17+
}
18+
19+
/**
20+
* is_prime_number - returns 1 if the input integer is a prime number,
21+
* otherwise return 0
22+
* @n: number
23+
*
24+
* Return: 1 if n is a prime number, otherwise 0
25+
*/
26+
27+
int is_prime_number(int n)
28+
{
29+
if (n <= 1)
30+
return (0);
31+
return (is_prime_helper(n, 2));
32+
}

0x08-recursion/6-main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
4+
/**
5+
* main - check the code
6+
*
7+
* Return: Always 0.
8+
*/
9+
int main(void)
10+
{
11+
int r;
12+
13+
r = is_prime_number(1);
14+
printf("%d\n", r);
15+
r = is_prime_number(1024);
16+
printf("%d\n", r);
17+
r = is_prime_number(16);
18+
printf("%d\n", r);
19+
r = is_prime_number(17);
20+
printf("%d\n", r);
21+
r = is_prime_number(25);
22+
printf("%d\n", r);
23+
r = is_prime_number(-1);
24+
printf("%d\n", r);
25+
r = is_prime_number(113);
26+
printf("%d\n", r);
27+
r = is_prime_number(7919);
28+
printf("%d\n", r);
29+
return (0);
30+
}

0 commit comments

Comments
 (0)