This commit is contained in:
Willem Haffmans 2024-09-10 00:18:01 +02:00
commit 607ce08c18
141 changed files with 8694 additions and 0 deletions

118
README.md Normal file
View File

@ -0,0 +1,118 @@
# piscine-codam
## c00
ex00: ft_putchar.c
ex01: ft_print_alphabet.c
ex02: ft_print_reverse_alphabet.c
ex03: ft_print_numbers.c
ex04: ft_is_negative.c
ex05: ft_print_comb.c
ex06: ft_print_comb2.c
ex07: ft_putnbr.c
## c01
ex00: ft_ft.c
ex01: ft_ultimate_ft.c
ex02: ft_swap.c
ex03: ft_div_mod.c
ex04: ft_ultimate_div_mod.c
ex05: ft_putstr.c
ex06: ft_strlen.c
ex07: ft_rev_int_tab.c
ex08: ft_sort_int_tab.c
## c02
ex00: ft_strcpy.c
ex01: ft_strncpy.c
ex02: ft_str_is_alpha.c
ex03: ft_str_is_numeric.c
ex04: ft_str_is_lowercase.c
ex05: ft_str_is_uppercase.c
ex06: ft_str_is_printable.c
ex07: ft_strupcase.c
ex08: ft_strlowcase.c
ex09: ft_strcapitalize.c
ex10: ft_strlcpy.c
ex11: ft_putstr_non_printable.c
## c03
ex00: ft_strcmp.c
ex01: ft_strncmp.c
ex02: ft_strcat.c
ex03: ft_strncat.c
ex04: ft_strstr.c
ex05: ft_strlcat.c
## c04
ex00: ft_strlen.c
ex01: ft_putstr.c
ex02: ft_putnbr.c
ex03: ft_atoi.c
ex04: ft_putnbr_base.c
ex05: ft_atoi_base.c
## c05
ex00: ft_iterative_factorial.c
ex01: ft_recursive_factorial.c
ex02: ft_iterative_power.c
ex03: ft_recursive_power.c
ex04: ft_fibonacci.c
ex05: ft_sqrt.c
ex06: ft_is_prime.c
ex07: ft_find_next_prime.c
ex08: ft_ten_queens_puzzle.c
## c06
ex00: ft_print_program_name.c
ex01: ft_print_params.c
ex02: ft_rev_params.c
ex03: ft_sort_params.c
## c07
ex00: ft_strdup.c
ex01: ft_range.c
ex02: ft_ultimate_range.c
ex03: ft_strjoin.c
ex04: ft_convert_base.c
ft_convert_base2.c
ex05: ft_split.c
## c08
ex00: ft.h
ex01: ft_boolean.h
ex02: ft_abs.h
ex03: ft_point.h
ex04: ft_strs_to_tab.c
ex05: ft_show_tab.c
## c11
ex00: ft_foreach.c
ex01: ft_map.c
ex02: ft_any.c
ex03: ft_count_if.c
ex04: ft_is_sort.c
ex05: do-op.c
ops.c
ops.h
## c12
ft_list.h
ex00: ft_create_elem.c
ex01: ft_list_push_front.c
ex02: ft_list_size.c
ex03: ft_list_last.c
ex04: ft_list_push_back.c
ex05: ft_list_push_strs.c
ex06: ft_list_clear.c
ex07: ft_list_at.c
ex08: ft_list_reverse.c
ex09: ft_list_foreach.c
ex10: ft_list_foreach_if.c
ex11: ft_list_find.c
ex12: ft_list_remove_if.c
ex13: ft_list_merge.c
ex14: ft_list_sort.c
ex15: ft_reverse_fun.c
ex16: ft_sorted_list_insert.c
ex17: ft_sorted_list_merge.c

2
bsq/BSQ-tester/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__
*.agree

21
bsq/BSQ-tester/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 E-Berry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

62
bsq/BSQ-tester/README.md Normal file
View File

@ -0,0 +1,62 @@
# BSQ Test Unit
This is a simple automated BSQ test unit for 42 piscine. This test suite contains three main files: `map_generator.py`, `map_solver.py`, and `test_bsq.py`. You can run each of these files separately, excluding the `map_parser.py` file, which is a helper module.
## Disclaimer
Please note that the tests provided in this test suite may not be entirely accurate, as the author cannot guarantee that they align with the expectations of the moulinette. The tests are based on the author's understanding and are intended to help others. The author takes no responsibility for any discrepancies or issues that may arise from using these tests.
You are encouraged to contribute and add your own tests to improve the test suite. If you have any ideas or suggestions, feel free to reach out to the author on Discord: julesreixcharat.
## Overview and Usage
### map\_generator.py
`map_generator.py` is a script that generates a random map based on the given parameters. It can be used to create test cases for the BSQ solver.
To run the `map_generator.py` file, use the following command:
```
python3 map_generator.py <x> <y> [-d <density>] [-s <seed>] [-o <output>]
```
Arguments:
- `<x>`: Width of the map
- `<y>`: Height of the map
- `<density>`: Density of the map (default: 0.4)
- `<seed>`: Random seed
- `<output>`: Output file name
### map\_solver.py
`map_solver.py` is a script that solves the generated map and finds the biggest square. It can be used to test the efficiency and correctness of the BSQ solver.
To run the `map_solver.py` file, use the following command:
```
python3 map_solver.py <x> <y> [-d <density>] [-s <seed>] [-p <process>]
```
Arguments:
- `<x>`: Width of the map
- `<y>`: Height of the map
- `<density>`: Density of the map (default: 0.4)
- `<seed>`: Random seed
- `<process>`: Solved map file name
### test\_bsq.py
`test_bsq.py` is a test suite that runs various test cases on the BSQ solver. It uses the `map_generator.py` and `map_solver.py` scripts to create test cases and compare the results with the expected output.
Before running `test_bsq.py`, make sure to set the relative or absolute path to the `./bsq` binary in the `BSQ_PATH` variable at the beginning of the file.
To run the `test_bsq.py` file, use the following command:
```
python3 test_bsq.py
```
This will run the test suite and display the results.

View File

@ -0,0 +1,59 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# map_generator.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jreix-ch <jreix-ch@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/09/26 21:02:44 by jreix-ch #+# #+# #
# Updated: 2023/09/27 18:02:54 by jreix-ch ### ########.fr #
# #
# **************************************************************************** #
# Internal modules
import sys
import random
import argparse
def generate_map(x, y, density = 0.4, chars = ".ox", seed = random.randint(0, 2**32 - 1)):
random.seed(seed)
map_data = [f"{y}{chars[0]}{chars[1]}{chars[2]}"]
for i in range(y):
row = []
for j in range(x):
if random.random() < density:
row.append(chars[1])
else:
row.append(chars[0])
map_data.append("".join(row))
return map_data
def main():
parser = argparse.ArgumentParser(description="Map generator and solver")
parser.add_argument("x", type=int, help="Width of the map")
parser.add_argument("y", type=int, help="Height of the map")
parser.add_argument("-d", "--density", type=float, default=0.4, help="Density of the map (default: 0.4)")
parser.add_argument("-s", "--seed", type=int, default=None, help="Random seed")
parser.add_argument("-o", "--output", type=str, help="Output file name")
args = parser.parse_args()
if args.seed is None:
args.seed = random.randint(0, 2**32 - 1)
map_data = generate_map(args.x, args.y, density=args.density, seed=args.seed)
print(f"[Map of {args.x} * {args.y} - Density of {args.density}; Random seed is {args.seed}]")
for row in map_data:
print(row)
if args.output:
with open(args.output, "w") as f:
for row in map_data:
f.write(row + "\n")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,26 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# map_parser.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jreix-ch <jreix-ch@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/09/27 17:16:12 by jreix-ch #+# #+# #
# Updated: 2023/09/27 17:46:20 by jreix-ch ### ########.fr #
# #
# **************************************************************************** #
def parse_map(map_file):
with open(map_file, "r") as f:
header = f.readline().strip()
square_char = header[-1]
obstacle_char = header[-2]
empty_char = header[-3]
num_lines = int(header[:-3])
map_data = []
for _ in range(num_lines):
line = f.readline().strip()
map_data.append(list(line))
return map_data, empty_char, obstacle_char, square_char

View File

@ -0,0 +1,101 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# map_solver.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jreix-ch <jreix-ch@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/09/27 17:48:24 by jreix-ch #+# #+# #
# Updated: 2023/09/27 18:02:18 by jreix-ch ### ########.fr #
# #
# **************************************************************************** #
# Internal modules
import argparse
import random
# Project modules
from map_generator import generate_map
from map_parser import parse_map
from pprint import pprint
def find_biggest_square(map_data, empty_char, obstacle_char, square_char):
rows = len(map_data)
cols = len(map_data[0])
# 2D array of biggest square ending for each cell (allowing fast process of the biggest square)
dp = [[0] * cols for _ in range(rows)]
# Fill it with zeros
for i in range(rows):
dp[i][0] = 1 if map_data[i][0] == empty_char else 0
for j in range(cols):
dp[0][j] = 1 if map_data[0][j] == empty_char else 0
max_size = 0
max_i = 0
max_j = 0
# Basically the same algorithm but in a few lines because it's Python :)
for i in range(1, rows):
for j in range(1, cols):
if map_data[i][j] == empty_char:
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1
if dp[i][j] > max_size:
max_size = dp[i][j]
max_i = i
max_j = j
# Fill square characters
for i in range(max_i, max_i - max_size, -1):
for j in range(max_j, max_j - max_size, -1):
map_data[i][j] = square_char
# Happy blobby now
return map_data
def main():
parser = argparse.ArgumentParser(description="Map generator and solver")
parser.add_argument("x", type=int, help="Width of the map")
parser.add_argument("y", type=int, help="Height of the map")
parser.add_argument("-d", "--density", type=float, default=0.4, help="Density of the map (default: 0.4)")
parser.add_argument("-s", "--seed", type=int, default=None, help="Random seed")
parser.add_argument("-p", "--process", type=str, help="Solved map file name")
args = parser.parse_args()
if args.seed is None:
args.seed = random.randint(0, 2**32 - 1)
map_data = generate_map(args.x, args.y, density=args.density, seed=args.seed)
print(f"[Map of {args.x} * {args.y} - Density of {args.density}; Random seed is {args.seed}]")
for row in map_data:
print(row)
if args.process:
map_file = args.process + ".map"
solution_file = args.process + "_solution.map"
with open(map_file, "w") as f:
f.write(f"{args.y}{map_data[0][-3]}{map_data[0][-2]}{map_data[0][-1]}\n")
for row in map_data[1:]:
f.write("".join(row) + "\n")
parsed_map_data, empty_char, obstacle_char, square_char = parse_map(map_file)
result = find_biggest_square(parsed_map_data, empty_char, obstacle_char, square_char)
with open(solution_file, "w") as f:
print(f"{args.y}{map_data[0][-3]}{map_data[0][-2]}{map_data[0][-1]}")
f.write(f"{args.y}{map_data[0][-3]}{map_data[0][-2]}{map_data[0][-1]}\n")
for row in result:
print("".join(row))
f.write("".join(row) + "\n")
print(f"Map saved to {map_file}")
print(f"Solution map saved to {solution_file}")
if __name__ == "__main__":
main()

190
bsq/BSQ-tester/test_bsq.py Normal file
View File

@ -0,0 +1,190 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# test_bsq.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jreix-ch <jreix-ch@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/09/27 16:55:07 by jreix-ch #+# #+# #
# Updated: 2024/06/26 17:12:34 by whaffman ### ########.fr #
# #
# **************************************************************************** #
# Internal modules
import os
import sys
import subprocess
import unittest
import tempfile
import random
import string
import time
# Project modules
from map_solver import find_biggest_square
from map_parser import parse_map
from map_generator import generate_map
BSQ_PATH = "../BSQ/bsq" # Relative or absolute path to binary
TIMEOUT = 100 # No more than 10 seconds per test
# This line below defines the seed used for random map generation
# The seed is randomized by default, but the used seed always shown at test start
USED_SEED = random.randint(0, 2**32 - 1)
# Uncomment the line below to use a custom seed
#USED_SEED = 123456789
class TestBSQ(unittest.TestCase):
def generate_map_file(self, x, y, density=0.4, chars=".ox", seed=None):
if not seed:
seed = USED_SEED
map_data = generate_map(x, y, density=density, chars=chars, seed=seed)
file = tempfile.NamedTemporaryFile(delete=False)
for row in map_data:
file.write((row + "\n").encode())
file.close()
return file.name
def compare_outputs(self, args, expected_output=None, expected_error=None, input_data=None, print_time=False, debug_print=False):
bsq_cmd = [BSQ_PATH] + [arg.replace("\n", os.linesep) for arg in args] # Important to fix \r\n issue
start_time = time.time()
bsq_output = subprocess.run(bsq_cmd, input=input_data, capture_output=True, text=True, timeout=TIMEOUT)
bsq_time = time.time() - start_time
if print_time:
print(f"\nExecution time for {print_time}:")
print(f"- bsq: {bsq_time:.6f} seconds\n")
if expected_output is not None:
try:
self.assertEqual(bsq_output.stdout.strip(), expected_output.strip())
except Exception as err:
if debug_print:
print(f"\nFAILURE:\nExpected:\n{expected_output}\n\nGot:\n{bsq_output.stdout}")
raise err
if expected_error is not None:
self.assertEqual(bsq_output.stderr, expected_error)
else:
self.assertEqual(bsq_output.stderr, "")
return bsq_output
def solve_map(self, map_data):
parsed_map_data, empty_char, obstacle_char, square_char = parse_map(map_data)
result = find_biggest_square(parsed_map_data, empty_char, obstacle_char, square_char)
return "\n".join("".join(row) for row in result)
def test_normal_maps(self):
for size, name in [(10, "10x10"), (50, "50x50"), (100, "100x100"), (1000, "1000x1000"), (2000, "2000x2000"), (4000, "4000x4000"), (10000, "10000x10000")]:
map_file = self.generate_map_file(size, size)
expected_stdout = self.solve_map(map_file)
self.compare_outputs([map_file], expected_output=expected_stdout, print_time=name)
os.remove(map_file)
def test_unsolvable_map(self):
map_file = self.generate_map_file(10, 10, density=1.0)
with open(map_file, "r") as f:
same_map = f.read()
expected_stdout = self.solve_map(map_file)
self.compare_outputs([map_file], expected_output=expected_stdout)
os.remove(map_file)
def test_empty_obstacles_map(self):
map_file = self.generate_map_file(10, 10, density=0.0)
expected_stdout = self.solve_map(map_file)
self.compare_outputs([map_file], expected_output=expected_stdout)
os.remove(map_file)
def test_different_chars(self):
map_file = self.generate_map_file(10, 10, chars="abc")
self.compare_outputs([map_file])
os.remove(map_file)
def test_stdin(self):
map_file = self.generate_map_file(10, 10)
with open(map_file, "r") as f:
input_data = f.read()
expected_solution = self.solve_map(map_file)
output = self.compare_outputs([], input_data=input_data, expected_output=expected_solution)
os.remove(map_file)
def test_4x4_maps(self):
maps = [
("4.ox\n..oo\n..oo\noooo\noooo\n", "topleft", "xxoo\nxxoo\noooo\noooo\n"),
("4.ox\noo..\noo..\noooo\noooo\n", "topright", "ooxx\nooxx\noooo\noooo\n"),
("4.ox\noooo\noooo\n..oo\n..oo\n", "bottomleft", "oooo\noooo\nxxoo\nxxoo\n"),
("4.ox\noooo\noooo\noo..\noo..\n", "bottomright", "oooo\noooo\nooxx\nooxx\n"),
("4.ox\noooo\no..o\no..o\noooo\n", "center", "oooo\noxxo\noxxo\noooo\n"),
]
for map_data, name, expected_solution in maps:
self.compare_outputs([], input_data=map_data, expected_output=expected_solution)
def test_corrupted_maps(self):
map_file = self.generate_map_file(10, 10)
with open(map_file, "r") as f:
lines = f.readlines()
lines[0] = "9.ox\n"
lines[5] = ".....o....\n"
corrupted_map = "".join(lines)
self.compare_outputs([], input_data=corrupted_map, expected_output="", expected_error="map error\n")
def test_wrong_char_map(self):
map_file = self.generate_map_file(10, 10)
with open(map_file, "r") as f:
lines = f.readlines()
lines[5] = ".....z....\n"
corrupted_map = "".join(lines)
output = self.compare_outputs([], input_data=corrupted_map, expected_output="", expected_error="map error\n")
def test_nonexistent_file(self):
output = self.compare_outputs(["nofile"], expected_output="", expected_error="map error\n")
def test_two_solvable_maps(self):
map1 = self.generate_map_file(10, 10)
map2 = self.generate_map_file(10, 10)
map1_solution = self.solve_map(map1)
map2_solution = self.solve_map(map2)
expected_solutions = f"{map1_solution}\n\n{map2_solution}"
self.compare_outputs([map1, map2], expected_output=expected_solutions)
os.remove(map1)
os.remove(map2)
def test_two_solvable_maps_with_nonexistent_file(self):
map1 = self.generate_map_file(10, 10)
map2 = self.generate_map_file(10, 10)
map1_solution = self.solve_map(map1)
map2_solution = self.solve_map(map2)
expected_solutions = f"{map1_solution}\n\n\n{map2_solution}"
self.compare_outputs([map1, "nofile", map2], expected_output=expected_solutions, expected_error="map error\n")
os.remove(map1)
os.remove(map2)
if __name__ == '__main__':
AGREEMENT_FILE = ".agree"
AGREEMENT_TEXT = "Blobfishes are the most amazing creatures in the world."
if not os.path.exists(AGREEMENT_FILE):
print("Blobfishes are the most amazing creatures in the world. You are agreeing to this by using this test. If you do not, you may not use it. (y/n)")
answer = input().lower()
if answer == 'y':
with open(AGREEMENT_FILE, "w") as f:
f.write(AGREEMENT_TEXT)
else:
print("You did not agree. Exiting.")
sys.exit(1)
else:
with open(AGREEMENT_FILE, "r") as f:
content = f.read().strip()
if content != AGREEMENT_TEXT:
print("Invalid agreement file. Exiting.")
os.remove(AGREEMENT_FILE)
sys.exit(1)
else:
print("Blobfishes are the most amazing creatures in the world.\n")
print("==============================================")
print(f"> Running BSQ Test Suite with seed {USED_SEED}")
print("==============================================\n")
unittest.main(verbosity=2)

60
bsq/Makefile Normal file
View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/06/24 12:05:19 by whaffman #+# #+# #
# Updated: 2024/06/24 12:47:17 by whaffman ### ########.fr #
# #
# **************************************************************************** #
NAME = bsq
SRC = src/ft_strlen.c \
src/check_invalid_map.c \
src/draw_biggest_square.c \
src/ft_findmax.c \
src/ft_min.c \
src/ft_puterror.c \
src/ft_putstr.c \
src/ft_strcat.c \
src/free_willy.c\
src/get_map_ptr.c \
src/handle_filedescriptor.c \
src/handle_first_line.c \
src/main.c \
src/make_charmap_and_metadata.c \
src/make_intmap.c \
src/read_file.c \
src/solve.c
OUT = $(SRC:.c=.o)
INCLUDE = include/
CFLAGS = -Wall -Wextra -Werror -O3
CC = cc
all: $(NAME)
$(NAME) : $(OUT)
$(CC) $^ -o $@
%.o: %.c
$(CC) -c $(CFLAGS) -I $(INCLUDE) $< -o $@
clean:
rm -f $(OUT)
fclean: clean
rm -f $(NAME)
re: fclean all

55
bsq/include/bsq.h Normal file
View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bsq.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyvan-de <lyvan-de@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 11:57:06 by whaffman #+# #+# */
/* Updated: 2024/06/24 16:22:05 by lyvan-de ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BSQ_H
# define BSQ_H
# include <unistd.h>
# include <stdlib.h>
# include <fcntl.h>
# define BUFFERSIZE 1000000
typedef struct s_map
{
char *charmap;
char *filename;
char empty;
char fill;
char obstacle;
char *filecontents;
int readlines;
int height;
int line_len;
int solution;
int *intmap;
int bsq;
size_t capacity;
ssize_t length;
} t_map;
char *ft_strcat(char *dest, char *src);
int check_invalid_map(t_map *map, int i);
int draw_biggest_square(t_map *map);
int ft_findmax(t_map *map);
int ft_min(int a, int b);
int ft_strlen(char *str);
int handle_filedescriptor(int fd);
int handle_first_line(t_map *map, char *str);
int make_charmap_and_metadata(t_map *map);
int make_intmap(t_map *map);
int read_file(int fd, t_map *map);
int solve(t_map *map);
t_map *get_map_ptr(void);
void free_willy(t_map *map);
void ft_puterror(t_map *map);
void ft_putstr(char *str);
#endif

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_invalid_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/26 17:15:20 by whaffman #+# #+# */
/* Updated: 2024/06/26 17:15:20 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int check_invalid_map(t_map *map, int i)
{
if (((i + 1) % map->line_len != 0) \
&& !((map->charmap[i] == map->empty) \
|| (map->charmap[i] == map->obstacle)))
return (1);
if ((i + 1) % map->line_len == 0 && map->charmap[i] != '\n')
return (1);
return (0);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_biggest_square.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 20:57:40 by whaffman #+# #+# */
/* Updated: 2024/06/24 21:04:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int draw_biggest_square(t_map *map)
{
int x;
int y;
y = 0;
while (y < map->bsq)
{
x = 0;
while (x < map->bsq)
{
map->charmap[map->solution - x - y * map->line_len] = map->fill;
x++;
}
y++;
}
return (1);
}

23
bsq/src/free_willy.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_willy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/26 17:14:24 by whaffman #+# #+# */
/* Updated: 2024/06/26 17:14:24 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
void free_willy(t_map *map)
{
if (map->filecontents)
free(map->filecontents);
if (map->intmap)
free(map->intmap);
if (map)
free(map);
}

33
bsq/src/ft_findmax.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_findmax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 20:06:46 by whaffman #+# #+# */
/* Updated: 2024/06/25 20:06:50 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int ft_findmax(t_map *map)
{
int i;
int max;
i = 0;
max = 0;
while (i < map->line_len * map->height)
{
if (map->intmap[i] > max)
{
max = map->intmap[i];
map->solution = i;
}
i++;
}
map->bsq = max;
return (1);
}

21
bsq/src/ft_min.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_min.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 20:06:38 by whaffman #+# #+# */
/* Updated: 2024/06/25 20:06:41 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int ft_min(int a, int b)
{
if (a < b)
return (a);
else
return (b);
}

19
bsq/src/ft_puterror.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puterror.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 22:07:46 by whaffman #+# #+# */
/* Updated: 2024/06/25 22:07:46 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
void ft_puterror(t_map *map)
{
free_willy(map);
write(2, "map error\n", 10);
}

23
bsq/src/ft_putstr.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 20:06:28 by whaffman #+# #+# */
/* Updated: 2024/06/25 20:06:30 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
i++;
write(1, str, i);
}

26
bsq/src/ft_strcat.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 20:06:18 by whaffman #+# #+# */
/* Updated: 2024/06/25 20:06:23 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
char *ft_strcat(char *dest, char *src)
{
char *odest;
odest = dest;
while (*dest)
dest++;
while (*src)
*dest++ = *src++;
*dest = '\0';
return (odest);
}

21
bsq/src/ft_strlen.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyvan-de <lyvan-de@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:42:15 by lyvan-de #+# #+# */
/* Updated: 2024/06/24 16:02:24 by lyvan-de ### ########.fr */
/* */
/* ************************************************************************** */
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}

40
bsq/src/get_map_ptr.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_map_ptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 22:10:56 by whaffman #+# #+# */
/* Updated: 2024/06/25 22:10:56 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
t_map *get_map_ptr(void)
{
t_map *ptr_map;
ptr_map = malloc(sizeof(t_map));
if (ptr_map == NULL)
exit(1);
ptr_map->charmap = NULL;
ptr_map->filename = NULL;
ptr_map->empty = '\0';
ptr_map->fill = '\0';
ptr_map->obstacle = '\0';
ptr_map->readlines = 0;
ptr_map->height = 0;
ptr_map->line_len = 0;
ptr_map->solution = 0;
ptr_map->intmap = NULL;
ptr_map->bsq = 0;
ptr_map->capacity = 0;
ptr_map->length = 0;
ptr_map->filecontents = malloc(10);
if (ptr_map->filecontents == NULL)
exit(1);
*ptr_map->filecontents = '\0';
return (ptr_map);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_filedescriptor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 14:21:23 by whaffman #+# #+# */
/* Updated: 2024/06/24 14:21:41 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int handle_filedescriptor(int fd)
{
t_map *map;
map = get_map_ptr();
read_file(fd, map);
if (!make_charmap_and_metadata(map)
|| map->readlines - 1 != map->height)
{
ft_puterror(map);
return (0);
}
if (!make_intmap(map))
{
ft_puterror(map);
return (0);
}
solve(map);
ft_findmax(map);
draw_biggest_square(map);
ft_putstr(map->charmap);
free_willy(map);
return (1);
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_first_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyvan-de <lyvan-de@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 12:42:50 by lyvan-de #+# #+# */
/* Updated: 2024/06/24 17:13:33 by lyvan-de ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int handle_first_line(t_map *map, char *str)
{
int i;
int number;
int n;
int lenght;
lenght = ft_strlen(str)-1;
i = lenght;
number = 0;
n = 0;
if (str[i] >= 32 && str[i] <= 126)
map->fill = str[i--];
if (str[i] >= 32 && str[i] <= 126)
map->obstacle = str[i--];
if (str[i] >= 32 && str[i] <= 126)
map->empty = str[i--];
if (str[i] < '0' || str[i] > '9')
return (0);
while (str[n] && str[n] >= '0' && str[n] <= '9' && n <= i)
number = number * 10 + str[n++] - '0';
map->height = number;
if (map->empty != map->obstacle && map->empty != map->fill
&& map->fill != map->obstacle && n + 2 == lenght)
return (1);
return (0);
}

41
bsq/src/main.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyvan-de <lyvan-de@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 11:57:46 by lyvan-de #+# #+# */
/* Updated: 2024/06/24 12:36:12 by lyvan-de ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int main(int argc, char *argv[])
{
int i;
int fd;
i = 1;
if (argc < 2)
{
fd = STDIN_FILENO;
handle_filedescriptor(fd);
}
while (i < argc)
{
fd = open(argv[i], O_RDONLY);
if (fd > 0)
{
handle_filedescriptor(fd);
close(fd);
}
else
write(2, "map error\n", 10);
if (argc > 2)
write(1, "\n", 1);
i++;
}
return (0);
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_first_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 14:33:51 by whaffman #+# #+# */
/* Updated: 2024/06/24 14:33:51 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int make_charmap_and_metadata(t_map *map)
{
int i;
i = 0;
map->readlines = 0;
while (map->filecontents[i])
{
if (map->filecontents[i] == '\n')
map->readlines++;
i++;
}
i = 0;
while (map->filecontents[i] != '\n')
i++;
map->filecontents[i++] = '\0';
map->charmap = &map->filecontents[i];
i = 0;
while (map->charmap[i] != '\n')
i++;
map->line_len = i + 1;
if (!handle_first_line(map, map->filecontents))
{
return (0);
}
return (1);
}

42
bsq/src/make_intmap.c Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* make_intmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 18:20:04 by whaffman #+# #+# */
/* Updated: 2024/06/24 18:20:04 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int make_intmap(t_map *map)
{
int i;
size_t size;
size = map->height * map->line_len;
map->intmap = malloc(size * sizeof(int));
if (map->intmap == NULL)
exit(1);
i = 0;
while (i < map->line_len * map->height)
{
if (check_invalid_map(map, i))
return (0);
if (map->charmap[i] == '\n')
map->intmap[i] = -1;
else if (map->charmap[i] == map->empty)
map->intmap[i] = 1;
else if (map->charmap[i] == map->obstacle)
map->intmap[i] = 0;
else
return (0);
i++;
}
if (map->charmap[i] != '\0')
return (0);
return (1);
}

50
bsq/src/read_file.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 18:01:23 by whaffman #+# #+# */
/* Updated: 2024/06/24 18:01:23 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int read_file(int fd, t_map *map)
{
char buffer[BUFFERSIZE];
char *new_space;
size_t content_length = 0;
map->capacity = BUFFERSIZE;
map->filecontents = malloc(map->capacity);
if (map->filecontents == NULL)
exit(1);
map->filecontents[0] = '\0';
while (1)
{
map->length = read(fd, buffer, sizeof(buffer) - 1);
if (map->length <= 0)
break ;
buffer[map->length] = '\0';
content_length += map->length;
while (map->capacity <= content_length + 1)
{
map->capacity *= 2;
new_space = malloc(map->capacity);
if (new_space == NULL)
exit(1);
new_space[0] = '\0';
ft_strcat(new_space, map->filecontents);
free(map->filecontents);
map->filecontents = new_space;
}
ft_strcat(map->filecontents, buffer);
}
return (1);
}

36
bsq/src/solve.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solve.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 19:58:13 by whaffman #+# #+# */
/* Updated: 2024/06/24 19:58:13 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "bsq.h"
int solve(t_map *map)
{
int i;
i = 0;
while (i < map->line_len * map->height)
{
if ((i < map->line_len) \
|| (i % map->line_len == 0) \
|| (i % map->line_len == map->line_len - 1))
{
i++;
continue ;
}
if (map->intmap[i] != 0)
map->intmap[i] = ft_min(map->intmap[i - 1],
ft_min(map->intmap[i - map->line_len],
map->intmap[i - map->line_len - 1])) + 1;
i++;
}
return (1);
}

11
bsq/test_grids/grid_0 Normal file
View File

@ -0,0 +1,11 @@
10.0x
000000000000
000000000000
00.000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000
000000000000

26
bsq/test_grids/grid_1 Normal file
View File

@ -0,0 +1,26 @@
25.ox
.o...............o.......
.........................
............o............
........o.......o........
............o.....o......
o...................oo...
.........................
.........o..............o
..oo..oooo...o.....o.....
.........................
..............o.o........
...............o.........
.........................
..........o..............
...o.....................
....o.......o...o.....o..
.........................
....o....................
..o......o..........oo...
............o..o.........
................o......o.
.............oo..o.......
.............o...........
.......o.......o.........
...o........o............

251
bsq/test_grids/grid_2 Normal file
View File

@ -0,0 +1,251 @@
250.ox
...................o.................................................................o..............
...............................................................................o....................
...........o................................................o................................o......
................................o....................................o..............................
....................o....................o...................o......................................
................................................o....................o..............................
............................................o.......................................................
................................................................o...................................
..................................o.................................................................
...........o.....................................................o..................................
....................................................................................................
................................................o.................................................o.
....................................................................................................
.............................................................................o......................
.................o.................................................................................o
...........o.........................................................o..............................
..................o.....................................o...........................................
.........o.............o............o.................................o.............................
.......o..............................................o....................o........................
....................................................................................................
..................................................................o.................................
....................................................................................................
o..................o................................................................................
.......................................................................................o............
...............................................................o....................................
.....................o.............................................o................................
.................o....o.............................................................................
.......................................o............................................................
...........................o.....o...........................................o......................
....................................................................................................
.............................o...........................o.........................o.............o..
...........................................o.....................................o..................
......................................................o........................o....................
.o.o..........................................................................o.....................
...o.....................o...............................................................o........o.
..............o...........................o.....................o........o..........................
.....................o.........o....................................................o...............
.................................................o..................................................
...............................o..............................o......................o..............
............................................................o.......................................
..........................................................................o.........................
.o..........o...o..................................................................................o
....................................................................................................
..............o...................................................................o.......o.........
.......................................o.......................................................o....
........................................o...........................................................
........................................................................................o...........
....................................................................................................
............o.......................................................................................
........o...............................................................o...........................
..................................................................................o...o.............
......................o..............................................................o..............
....................................................................................................
....................................................................................................
.......................o............................................................................
....................................o........o.................................o....................
.....................o..............................................................................
.............................................................................o......................
....................................................................................................
............................o..........o.........................o..................................
........................................o.............o.............................................
.......................o.......oo..........o........................................................
........o...........................................................................................
.........................................................................o..........................
.................................................o....................o........o....................
....................................................................................................
....................................................................................................
...........................................o.........o.........................o.....o..............
................................................................................................o...
..........................................o.............o.........................o......o..........
....................................................................................................
o.......o...........................o...............................................................
.o...o..................................................o.....o.....................................
............................................................................................o.......
.............................................o....................................o.................
........o.....................o.....................................................................
o....................................................................................o...o..........
....................................................................................................
................................................o...................................................
..........o.................................................o...................o................o..
..........................................o....................................o..o....o............
.....................................................o..............................................
.....................................o..............o...............................................
...................................o...................................o............................
...............................................................................o....................
....................................................................................................
...................................................oo...............................................
.........o.....o.................................................................o..................
....................................................................................................
...o.....................o.......o..................................................................
....................................................................................................
..................................o.................................................................
............................................................................o..........o............
..............oo.......................o.......................................................o....
................o.....o..................................o.........o................................
...o.........................................................................................o......
...................o................................................................................
....................o.........o...............................................o.....................
......o....o.................................................................o......................
...o...........................................o....................................................
...................................................................................o................
........................o.o.....................................................o............o......
....................................................................................................
................................................o.....o............................o................
.............................o..........o...........................................................
....................................................................................................
......................................................o...............o.............................
...........................o.........................o...o..........o...............................
..................................................o...........o.....................................
...........o...........................o.................o..........................................
..................o..........................................................................o......
.o................................................................o.............o...................
................................................................o...................................
....................................................................................................
.....................................................................o........................o.....
...................................................................................o................
...................o..............................................o.................................
............o.......................................................................................
...............................................................................................o....
.......................................................................................o............
....................................................................................................
.............................................o......................................o...............
....................................................................................................
o.........................o.........................o................................o..............
............................o......o........................................................o.......
...o.....o.............................................o............................................
...........................................................................................o........
................................................................................o...................
.......o......................o..........................................................o..........
............o.......................................................................................
.............................o......................................................................
....................................................................................................
...................................o...o.................................o..........................
.....................................o....................................o...........o.............
....................................................................................o...............
...............o....................................................................................
..................o...........................................................o..............o......
...............o............................................o..............................oo.......
...........o..........................o............................o.............................o..
.................................................o.............................o....................
..............................o.....................o...............................................
....................................................................................................
......................o..............................................o..............................
....................................................................................................
....................................................................................................
...............................................................................o....................
....................................................................................................
...................................................................................................o
.............................................o......................................................
........................................................................o..................o.....o..
........................................o...............................................o.....o.....
...............................................................................o....................
....................................................................o...............................
.....................................................o.........................o...o................
.............................................o......................................................
....................................................................................................
..............................................................o..................................o..
.........................................................o..........................................
.......................................................o............................................
....................................................................................................
...............................................................................o....................
.....o..............................................................................................
................................o...........................................................o.......
....................................................o.............................o.................
................................................................................................o..o
....................................................................................................
......o...........................................o......................o..........................
.........................o.......o...................o..............................................
..............................................o..........o..........................................
..............................................o.....................................................
...............................o..............o..................................o..................
..o...............o.................o......o.............................o....................o.....
......................................................................o.............................
....................................................................................................
..............................o....................................................o................
....................................................................................................
.....o..............................................................................................
..............o..........................o...............................................o..........
....................................o.........................................o.....................
....................................................................................................
..................o......o.......................o..................................................
....................................................................................................
....................................................................................................
...........o........................................................................................
.........................................................o.....o....................................
....................................................................................................
..............................................................................................o.....
.................................................................o.............................o....
................................o..........................................................o........
.....................................................................o..............................
....................................................................................................
...................................o................................................................
....................................................................................................
...........................................o........................................................
....................................................................................................
...........................o........................................................................
........o................................o..........................................................
......................o..................................................................o..........
...........................o..............................o.........................................
....................................................................................................
....................................................................................................
....................o...........................o......o............................................
.....................o..............................................................................
.........................o........o.................................................................
.........................................................o......o...................................
.......................................................................o............................
.................................................o.....o............o.............o.................
.........o..........................................................................................
.....................................................o..............................................
......................o.............................................................................
....................................................................................................
............o........o...........................................................................o..
................................................................o..............o.......o............
..............o.....................................................................................
.o................................................o..........o......................................
..................................................o.................................................
........................................o...........................................................
.......................................o...............o...o........................o...............
..........................................................o.........................................
....................................................................o...............................
..o..............o...........................o..........................o...........................
........................................................................o...........................
...........o....o.................o........o........................................................
.................................o.............o....................................................
................o...o................................................................o..............
..............................................................o.....................................
.............................................................o......................................
....................................................................o....o..........................
.o......................................................................................o...........
....................................................................................................
.............o.......o.........................................o....................................
....................................................................................................
.......................................................o........................o...................
....................o........o......................................................................
.......................................................................o............................
....................................................................................................
............o.......................................................o...........o...................
....................................................................................................
......o..................................................................................o..........
........o............................................................oo.............................
....................................................................................................
............................................................o............................o..........
....................................................................................................
.............................o...........o..........................................................
.......................................o.................................o........................o.
.....................o..............................................................................
...................o.............o...................o...............o..............................
......o..........................................................................................o..
............o....................................................o.........o........................
....................................................................................................

26
bsq/test_grids/grid_3 Normal file
View File

@ -0,0 +1,26 @@
25pwx
pppppwpppp
pppppppppp
wpppppwppp
pppppppppp
pppppppppp
pppppppppp
pppppppppp
pppppppppp
pppppppppp
pppppppppp
pppppwpppp
pppppppppp
pppppppppp
pppppppppp
pppppppppp
pppppwpppp
ppwppppppp
pppppppppp
pppppppppp
ppwppppppp
ppwppppppp
pppppppppp
pppppppppp
pppppppppp
pppppppppp

76
bsq/test_grids/grid_4 Normal file
View File

@ -0,0 +1,76 @@
75.+#
+..........................................................................
...........................................................................
.......................................................+..............+....
...........................+..........+....................................
...................+.......................................................
.....................................+.....................................
...........................................................................
.......................................+...................................
...........................................................................
......+.....................+...........+..................................
...........................................................................
...........................................................................
...........................................................................
..............+.............................+..............................
...................................+.......................................
........................+...........................+......................
.................................................+.........................
...................+................................................+......
...........................................................................
......+....................................................................
...........................................+..+............................
...........................................................................
................................+..........................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...............................................+...........................
....................................................................+......
...........................................................................
...........................................................................
...........................................................................
...................+.......................................................
............................+..............................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
..................................................................+........
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
......................+.........+..........................................
...................................+.......................................
...........................................................................
...........................................................................
...........................................................................
................+..+.......................................................
...........................................................................
...........................................................................
............................................................+..............
...........................................................................
...........................................................................
...........................................................................
.....+.....................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
..........+................................................................
.............................+.............................................
...........................................................................
.................................................+.........................
...........................................................................
...........................................................................
..................+........................................................

1001
bsq/test_grids/grid_5 Normal file

File diff suppressed because it is too large Load Diff

1001
bsq/test_grids/grid_6 Normal file

File diff suppressed because it is too large Load Diff

101
bsq/test_grids/grid_7 Normal file
View File

@ -0,0 +1,101 @@
100.+#
.........................................+.............................+............................
....................................................................................................
....................................................................................................
.................................................................................+.............+....
....................................................................................................
....................................................................................................
....................................................+...............................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
.............................................................+..............................+.......
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
..................................................................................+.................
....................................................................................................
...................+................................................................................
....................................................................................................
.........................................................................+..........................
....................................................................................................
....................................................................................................
....................................................................................................
+...................................................................................................
....................................................................................................
....................................................................................................
.+.............................................................+....+...............................
..................................................................+.................................
.........................+..........................................................................
.......................+............................................................................
..............................................................................................+.....
..........................+.........................................................................
..................................................+..........................................+......
....................................................................................................
......................+.............................................................................
...............................................................................................+....
...........................................................................+........................
....................................................................................................
....................................................................................................
....................................................................................................
.......................................+............+...............................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
.......................+............................................................................
....................................+...............................................................
....................................................................................................
.....+...................................................................+...............+..........
....................................................................................................
....................................................................................................
..............................................................................+.....................
..................................................................................+.................
....................................................................................................
....................................................................................................
.................+..................................................................................
....................................................................................................
....................................................................................................
..................................................................................+.................
....................................................................................................
................+.................................................+.................................
..............................................+.....................................................
....................................................................................................
....................................................................................................
....................................................+...............................................
....................................................................................................
....................................................................................................
.........+.........................................................+................................
....................................................................................................
.........+..........................................................................................
....................................................................................................
....................................................................................................
.......................................................+............................................
....................................................................................................
..........................+.........................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
.............................................................+......................................
...........................................................................+........................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................

View File

@ -0,0 +1,17 @@
#!/usr/bin/perl
use warnings;
use strict;
die "program x y density" unless (scalar(@ARGV) == 3);
my ($x, $y, $density) = @ARGV;
print "$y.+#\n";
for (my $i = 0; $i < $y; $i++) {
for (my $j = 0; $j < $x; $j++) {
if (int(rand($y) * 2) < $density) {
print "+";
}
else {
print ".";
}
}
print "\n";
}

18
c00/ex00/ft_putchar.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 13:53:36 by whaffman #+# #+# */
/* Updated: 2024/06/05 11:09:09 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_alphabet.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 13:54:05 by whaffman #+# #+# */
/* Updated: 2024/06/04 14:37:12 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_print_alphabet(void)
{
char c;
c = 'a';
while (c <= 'z')
{
write(1, &c, 1);
c++;
}
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_reverse_alphabet.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 13:54:53 by whaffman #+# #+# */
/* Updated: 2024/06/05 11:09:43 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_print_reverse_alphabet(void)
{
char c;
c = 'z';
while (c >= 'a')
{
write(1, &c, 1);
c--;
}
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_numbers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 13:55:41 by whaffman #+# #+# */
/* Updated: 2024/06/05 11:15:13 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_print_numbers(void)
{
int n;
char c;
n = 0;
while (n <= 9)
{
c = n + 48;
write(1, &c, 1);
n++;
}
}
/*
int main(void)
{
ft_print_numbers();
return (0);
}
*/

32
c00/ex04/ft_is_negative.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_negative.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 14:16:23 by whaffman #+# #+# */
/* Updated: 2024/06/05 11:46:39 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_is_negative(int n)
{
if (n < 0)
{
write(1, "N", 1);
}
else
{
write(1, "P", 1);
}
}
/*
int main(void)
{
ft_is_negative(-3);
ft_is_negative(2);
return (0);
}
*/

68
c00/ex05/ft_print_comb.c Normal file
View File

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_comb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 14:57:05 by whaffman #+# #+# */
/* Updated: 2024/06/04 16:50:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void write_number(int n)
{
char c;
if (n >= 0 && n <= 9)
{
c = n + 48;
write(1, &c, 1);
}
}
void write_numbers(int a, int b, int c)
{
write_number(a);
write_number(b);
write_number(c);
if (a != 7 || b != 8 || c != 9)
{
write(1, ", ", 2);
}
}
void ft_print_comb(void)
{
int a;
int b;
int c;
a = 0;
b = 0;
c = 0;
while (a <= 7)
{
b = a + 1;
while (b <= 8)
{
c = b + 1;
while (c <= 9)
{
write_numbers(a, b, c);
c++;
}
b++;
}
a++;
}
}
/*
int main(void)
{
ft_print_comb();
return (0);
}
*/

79
c00/ex06/ft_print_comb2.c Normal file
View File

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_comb2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 16:13:19 by whaffman #+# #+# */
/* Updated: 2024/06/05 10:40:14 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void write_number(int n)
{
char c;
if (n >= 0 && n <= 9)
{
c = n + 48;
write(1, &c, 1);
}
else if (n < 0)
{
write(1, "-", 1);
write_number(-n);
}
else if (n > 9)
{
write_number(n / 10);
write_number(n % 10);
}
}
void write_numbers(int a, int b)
{
if (a <= 9)
{
write(1, "0", 1);
}
write_number(a);
write(1, " ", 1);
if (b <= 9)
{
write(1, "0", 1);
}
write_number(b);
if (a != 98 || b != 99)
{
write(1, ", ", 2);
}
}
void ft_print_comb2(void)
{
int a;
int b;
a = 0;
b = 0;
while (a <= 99)
{
b = a + 1;
while (b <= 99)
{
write_numbers(a, b);
b++;
}
a++;
}
}
/*
int main(void)
{
ft_print_comb2();
return (0);
}
*/

49
c00/ex07/ft_putnbr.c Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 10:06:02 by whaffman #+# #+# */
/* Updated: 2024/06/10 19:31:37 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putnbr(int nb)
{
char c;
if (-2147483648 == nb)
{
write(1, "-2147483648", 11);
}
else if (nb >= 0 && nb <= 9)
{
c = nb + '0';
write(1, &c, 1);
}
else if (nb < 0)
{
write(1, "-", 1);
ft_putnbr(-nb);
}
else if (nb > 9)
{
ft_putnbr(nb / 10);
ft_putnbr(nb % 10);
}
}
int main(void)
{
ft_putnbr(0);
ft_putnbr(-10);
ft_putnbr(42);
ft_putnbr(-2147483648);
ft_putnbr(2147483647);
return (0);
}

28
c01/ex00/ft_ft.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ft.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 12:20:20 by whaffman #+# #+# */
/* Updated: 2024/06/06 14:37:28 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
void ft_ft(int *nbr)
{
*nbr = 42;
}
/*
#include <stdio.h>
int main(void)
{
int c = 20;
int *nbr = &c;
ft_ft(nbr);
printf("%d", *nbr);
return (0);
}
*/

16
c01/ex01/ft_ultimate_ft.c Normal file
View File

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultimate_ft.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 15:53:38 by whaffman #+# #+# */
/* Updated: 2024/06/05 16:07:57 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
void ft_ultimate_ft(int *********nbr)
{
*********nbr = 42;
}

33
c01/ex02/ft_swap.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 16:09:20 by whaffman #+# #+# */
/* Updated: 2024/06/05 16:24:04 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
void ft_swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
/*
#include <stdio.h>
int main(void)
{
int a = 2;
int b = 4;
ft_swap(&a, &b);
printf("%d",a);
printf("%d",b);
}
*/

31
c01/ex03/ft_div_mod.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_div_mod.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 16:25:25 by whaffman #+# #+# */
/* Updated: 2024/06/05 16:38:01 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
void ft_div_mod(int a, int b, int *div, int *mod)
{
*div = a / b;
*mod = a % b;
}
/*
#include <stdio.h>
int main(void)
{
int a = 12;
int b = 10;
int div = 0;
int mod = 0;
ft_div_mod(a, b, &div, &mod);
printf("from %d and %d the div is %d and the mod is %d", a, b, div, mod);
return (0);
}
*/

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultimate_div_mod.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 16:40:47 by whaffman #+# #+# */
/* Updated: 2024/06/05 16:47:24 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
void ft_ultimate_div_mod(int *a, int *b)
{
int div;
int mod;
div = *a / *b;
mod = *a % *b;
*a = div;
*b = mod;
}
/*
#include <stdio.h>
int main(void)
{
int a = 15;
int b = 10;
printf("a = %d, b = %d", a, b);
ft_ultimate_div_mod(&a ,&b);
printf("a = %d, b = %d", a, b);
return (0);
}
*/

31
c01/ex05/ft_putstr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 17:09:49 by whaffman #+# #+# */
/* Updated: 2024/06/05 17:19:32 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str != '\0')
{
write(1, str, 1);
str++;
}
}
/*
#include <stdio.h>
int main(void)
{
char *string = "Hello World!\n";
ft_putstr(string);
return (0);
}
*/

34
c01/ex06/ft_strlen.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 17:21:22 by whaffman #+# #+# */
/* Updated: 2024/06/05 17:47:50 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
int ft_strlen(char *str)
{
int result;
result = 0;
while (*str != '\0')
{
result++;
str++;
}
return (result);
}
/*
#include <stdio.h>
int main(void)
{
char *str = "Hello World!"; //12 characters
printf("the string \"Hello World\" \
contains %d characters,\n", ft_strlen(str));
return (0);
}
*/

45
c01/ex07/ft_rev_int_tab.c Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_rev_int_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 17:48:46 by whaffman #+# #+# */
/* Updated: 2024/06/10 11:52:30 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
void ft_rev_int_tab(int *tab, int size)
{
int temp;
int *back_tab;
back_tab = tab + size - 1;
while (back_tab > tab)
{
temp = *tab;
*tab = *back_tab;
*back_tab = temp;
tab++;
back_tab--;
}
}
#include <stdio.h>
int main(void)
{
int arr[] = {1};
int arr2[] = {};
ft_rev_int_tab(arr, 1);
ft_rev_int_tab(arr2, 0);
for (int i = 0; i < 1; i++)
printf("%d ", arr[i]);
printf("\n");
for (int i = 0; i < 0; i++)
printf("%d ", arr2[i]);
return (0);
}

View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort_int_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 18:49:17 by whaffman #+# #+# */
/* Updated: 2024/06/05 19:16:50 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
void ft_swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void ft_sort_int_tab(int *tab, int size)
{
int i;
int swapped;
swapped = 1;
while (swapped == 1)
{
swapped = 0;
i = 1;
while (i < size)
{
if (*(tab + i - 1) > *(tab + i))
{
swapped = 1;
ft_swap(tab + i - 1, tab + i);
}
i++;
}
}
}
/*
#include <stdio.h>
int main(void)
{
int arr[] = {4, 1, 3, 6, 2, 5};
ft_sort_int_tab(arr, 6);
for (int i = 0; i < 6; i++)
printf("%d ", arr[i]);
return (0);
}
*/

38
c02/ex00/ft_strcpy.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 09:55:08 by whaffman #+# #+# */
/* Updated: 2024/06/09 17:14:50 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strcpy(char *dest, char *src)
{
int i;
i = 0;
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = src[i];
return (dest);
}
/*
#include <stdio.h>
int main(void)
{
char src[] = "Hello, World!";
char dest[] = "World, Hello!";
printf("%s",&dest[0]);
ft_strcpy(&dest[0],&src[0]);
printf("%s",&dest[0]);
return (0);
}
*/

42
c02/ex01/ft_strncpy.c Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 12:11:53 by whaffman #+# #+# */
/* Updated: 2024/06/06 18:14:49 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strncpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
i = 0;
while (src[i] != '\0' && i < n)
{
dest[i] = src[i];
i++;
}
while (i < n)
{
dest[i] = '\0';
i++;
}
return (dest);
}
/*
#include <stdio.h>
int main(void)
{
char src[] = "Held!";
char dest[] = "World, Hello!";
printf("%s",&dest[0]);
ft_strncpy(&dest[0], &src[0], 6);
printf("%s",&dest[0]);
return (0);
}
*/

View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_alpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 14:06:00 by whaffman #+# #+# */
/* Updated: 2024/06/06 14:35:19 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_alpha(char *str)
{
int is_alpha;
is_alpha = 1;
while (*str != '\0')
{
if (*str < 'A' \
|| (*str > 'Z' && *str < 'a') \
|| *str > 'z')
{
is_alpha = 0;
}
str++;
}
return (is_alpha);
}
/*
#include <stdio.h>
int main(void)
{
char *s1 = "";
char *s2 = "adsf";
char *s3 = "asdfgr3asdf";
printf("%d, %d, %d", \
ft_str_is_alpha(s1),
ft_str_is_alpha(s2),
ft_str_is_alpha(s3));
return (0);
}
*/

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_numeric.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 14:38:19 by whaffman #+# #+# */
/* Updated: 2024/06/06 15:14:48 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_numeric(char *str)
{
int is_numeric;
is_numeric = 1;
while (*str != '\0')
{
if (*str < '0' || *str > '9')
{
is_numeric = 0;
}
str++;
}
return (is_numeric);
}
/*
#include <stdio.h>
int main(void)
{
char *s1 = "";
char *s2 = "123";
char *s3 = "123f";
printf("%d, %d, %d", \
ft_str_is_numeric(s1),
ft_str_is_numeric(s2),
ft_str_is_numeric(s3));
return (0);
}
*/

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_lowercase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 14:51:35 by whaffman #+# #+# */
/* Updated: 2024/06/06 14:58:04 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_lowercase(char *str)
{
int is_lowercase;
is_lowercase = 1;
while (*str != '\0')
{
if (*str < 'a' || *str > 'z')
{
is_lowercase = 0;
}
str++;
}
return (is_lowercase);
}
/*
#include <stdio.h>
int main(void)
{
char *s1 = "";
char *s2 = "adsf";
char *s3 = "asdfgrAasdf";
printf("%d, %d, %d", \
ft_str_is_lowercase(s1),
ft_str_is_lowercase(s2),
ft_str_is_lowercase(s3));
return (0);
}
*/

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_uppercase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 14:51:35 by whaffman #+# #+# */
/* Updated: 2024/06/06 15:01:30 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_uppercase(char *str)
{
int is_uppercase;
is_uppercase = 1;
while (*str != '\0')
{
if (*str < 'A' || *str > 'Z')
{
is_uppercase = 0;
}
str++;
}
return (is_uppercase);
}
/*
#include <stdio.h>
int main(void)
{
char *s1 = "";
char *s2 = "ASDF";
char *s3 = "asdfgrAasdf";
printf("%d, %d, %d", \
ft_str_is_uppercase(s1),
ft_str_is_uppercase(s2),
ft_str_is_uppercase(s3));
return (0);
}
*/

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_printable.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 14:51:35 by whaffman #+# #+# */
/* Updated: 2024/06/12 16:40:15 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_printable(char *str)
{
int is_printable;
is_printable = 1;
while (*str != '\0')
{
if (*str < 32 || *str > 126)
{
is_printable = 0;
}
str++;
}
return (is_printable);
}
/*
#include <stdio.h>
int main(void)
{
char *s1 = "";
char *s2 = "ASDF";
char *s3 = "asdfgrAasi\ndf";
printf("%d, %d, %d", \
ft_str_is_printable(s1),
ft_str_is_printable(s2),
ft_str_is_printable(s3));
return (0);
}
*/

37
c02/ex07/ft_strupcase.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strupcase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 15:57:01 by whaffman #+# #+# */
/* Updated: 2024/06/10 10:20:08 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strupcase(char *str)
{
char *org_str;
org_str = str;
while (*str)
{
if (*str >= 'a' && *str <= 'z')
{
*str = *str - 'a' + 'A';
}
str++;
}
return (org_str);
}
/*
#include <stdio.h>
int main(void)
{
char str[] = "Hello, World!";
ft_strupcase(&str[0]);
printf("%s", str);
return (0);
}
*/

35
c02/ex08/ft_strlowcase.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlowcase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 15:57:01 by whaffman #+# #+# */
/* Updated: 2024/06/09 17:25:15 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
char *ft_strlowcase(char *str)
{
char *org_str;
org_str = str;
while (*str)
{
if (*str >= 'A' && *str <= 'Z')
*str = *str - 'A' + 'a';
str++;
}
return (org_str);
}
/*
#include <stdio.h>
int main(void)
{
char str[] = "Hello, World!";
ft_strlowcase(&str[0]);
printf("%s", str);
return (0);
}
*/

View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcapitalize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 15:57:01 by whaffman #+# #+# */
/* Updated: 2024/06/09 17:29:10 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
int ft_is_num(char c)
{
return (c >= '0' && c <= '9');
}
int ft_is_upper(char c)
{
return (c >= 'A' && c <= 'Z');
}
int ft_is_lower(char c)
{
return (c >= 'a' && c <= 'z');
}
int ft_is_alphanumeric(char c)
{
return (ft_is_num(c) || ft_is_upper(c) || ft_is_lower(c));
}
char *ft_strcapitalize(char *str)
{
int first_letter;
char *org_str;
org_str = str;
first_letter = 1;
while (*str)
{
if (first_letter && ft_is_lower(*str))
*str = *str - 'a' + 'A';
if (!first_letter && ft_is_upper(*str))
*str = *str - 'A' + 'a';
if (ft_is_alphanumeric(*str))
first_letter = 0;
else
first_letter = 1;
str++;
}
return (org_str);
}
/*
#include <stdio.h>
int main(void)
{
char str[] = "salut, comment tu vas ? 42mots quaRante-deux; cinquante+et+un";
ft_strcapitalize(&str[0]);
printf("%s", str);
return (0);
}
*/

58
c02/ex10/ft_strlcpy.c Normal file
View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 12:11:53 by whaffman #+# #+# */
/* Updated: 2024/06/10 11:13:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_strlen(char *str)
{
int result;
result = 0;
while (str[result])
{
result++;
}
return (result);
}
unsigned int ft_strlcpy(char *dest, char *src, unsigned int size)
{
unsigned int i;
unsigned int result;
i = 0;
while (src[i] && i < size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
result = ft_strlen(src);
return (result);
}
#ifdef DEBUG
int main(void)
{
char src[13];
char dest[13];
int result;
ft_strlcpy(&src[0], "hello, world!", 13);
result = ft_strlcpy(&dest[0], "HELLO, WORLD!", 13);
printf("%d: %s\n", result, &dest[0]);
result = ft_strlcpy(&dest[0], &src[0], 13);
printf("%d: %s\n", result, &dest[0]);
return (0);
}
#endif

View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_non_printable.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 18:24:39 by whaffman #+# #+# */
/* Updated: 2024/06/10 12:06:13 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
#include <string.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_puthex(int c)
{
if (c < 10)
ft_putchar(c + '0');
else if (c > 9 && c < 16)
{
ft_putchar(c - 10 + 'a');
}
}
void ft_putstr_non_printable(char *str)
{
while (*str != '\0')
{
if (*str >= 32 && *str <= 126)
write(1, str, 1);
else
{
ft_putchar('\\');
ft_puthex(*str / 16);
ft_puthex(*str % 16);
}
str++;
}
}
#ifdef DEBUG
int main(void)
{
char str[15];
int i;
strcpy(&str[0], "Hello, \n World!");
ft_putstr_non_printable(str);
printf("\n");
i = 0;
while (i <= 127)
{
strcpy(&str[0], (char [2]){(char) i, '\0'});
ft_putstr_non_printable(str);
i++;
}
return (0);
}
#endif

43
c03/ex00/ft_strcmp.c Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 14:39:41 by whaffman #+# #+# */
/* Updated: 2024/06/10 15:50:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s1 == *s2)
{
s1++;
s2++;
}
return (*s1 - *s2);
}
#ifdef DEBUG
void test(char *s1, char *s2)
{
printf("s1: \"%s\"\ns2: \"%s\"\nft_strcmp: %d\nstrcmp: %d\n", \
s1, s2, ft_strcmp(s1, s2), strcmp(s1, s2));
}
int main(void)
{
test("abc", "abc");
test("ab", "abc");
test("abc", "ab");
test("abd", "abc");
test("abc", "abd");
return (0);
}
#endif

51
c03/ex01/ft_strncmp.c Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 14:39:41 by whaffman #+# #+# */
/* Updated: 2024/06/18 16:26:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
int ft_strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int i;
i = 0;
// if (n == 0)
// return (0);
while (*s1 && *s1 == *s2 && i < n - 1)
{
s1++;
s2++;
i++;
}
return (*s1 - *s2);
}
#ifdef DEBUG
void test(char *s1, char *s2, unsigned int n)
{
printf("s1: \"%s\"\ns2: \"%s\"\nstrncmp: %d\nft_strncmp: %d\n\n", \
s1, s2, strncmp(s1, s2, n), ft_strncmp(s1, s2, n));
}
int main(void)
{
test("abc", "abc", 3);
test("ab", "abc", 3);
test("abc", "ab", 3);
test("abd", "abc", 3);
test("abd", "abc", 2);
test("abd", "zbc", 0);
test("abd", "zbc", 1);
return (0);
}
#endif

54
c03/ex02/ft_strcat.c Normal file
View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 14:58:48 by whaffman #+# #+# */
/* Updated: 2024/06/11 15:00:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
char *ft_strcat(char *dest, char *src)
{
char *odest;
odest = dest;
while (*dest)
dest++;
while (*src)
*dest++ = *src++;
*dest = '\0';
return (odest);
}
#ifdef DEBUG
void test(char *s1, char *s2, char *s3)
{
printf("s1: \"%s\"\n", s1);
printf("s2: \"%s\"\n", s2);
printf("s3: \"%s\"\n", s3);
printf("strcat(s1, s3): \"%s\"\nft_strcat(s2, s3): \"%s\"\n\n", \
strcat(s1, s3), ft_strcat(s2, s3));
}
int main(void)
{
char s1[20];
char s2[20];
s1[0] = '\0';
test(&s1[0], &s2[0], "Hello!");
test(&s1[0], &s2[0], "Hello!");
test(&s1[0], &s2[0], "Hello!");
test(&s1[0], &s2[0], "Hello!");
test(&s1[0], &s2[0], "Hello!");
test(&s1[0], &s2[0], "Hello!");
return (0);
}
#endif

54
c03/ex03/ft_strncat.c Normal file
View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 14:58:31 by whaffman #+# #+# */
/* Updated: 2024/06/11 16:42:32 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
char *ft_strncat(char *dest, char *src, unsigned int nb)
{
char *odest;
unsigned int i;
odest = dest;
while (*dest)
dest++;
i = 0;
while (*src && i++ < nb)
*dest++ = *src++;
*dest = '\0';
return (odest);
}
#ifdef DEBUG
void test(char *dest0, char *dest1, char *src, int n)
{
printf("dest0: \"%s\"\n", dest0);
printf("dest1: \"%s\"\n", dest1);
printf("src: \"%s\"\n", src);
printf("strncat: \"%s\"\n", strncat(dest0, src, n));
printf("ft_strncat: \"%s\"\n", ft_strncat(dest1, src, n));
}
int main(void)
{
char dest0[20];
char dest1[20];
dest0[0] = 0;
dest1[0] = 0;
test(&dest0[0], &dest1[0], "0123456789", 10);
test(&dest0[0], &dest1[0], "0123456789", 5);
test(&dest0[0], &dest1[0], "0123456789", 5);
return (0);
}
#endif

73
c03/ex04/ft_strstr.c Normal file
View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 14:58:08 by whaffman #+# #+# */
/* Updated: 2024/06/11 14:58:17 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
int compare(char *str, char *to_find)
{
int found;
found = 0;
while (*to_find)
{
if (*str != *to_find)
{
found = 0;
break ;
}
else
{
str++;
to_find++;
found = 1;
}
}
return (found);
}
char *ft_strstr(char *str, char *to_find)
{
int found;
if (!*to_find)
return (str);
found = 0;
while (*str)
{
if (compare(str, to_find))
return (str);
str++;
}
return (NULL);
}
#ifdef DEBUG
void test(char *str, char *to_find)
{
printf("haystack: \"%s\"\nneedle: \"%s\"\n", \
str, to_find);
printf("index(ft_strstr): %ld\nindex(strstr): %ld\n", \
ft_strstr(str, to_find) - str, strstr(str, to_find) - str);
}
int main(void)
{
test("Hello, World!", "o, ");
test("Hello, World!", "o");
test("Hello, World!", "");
test("Hello, World!", "H");
test("Hello, World!", "Z");
return (0);
}
#endif

74
c03/ex05/ft_strlcat.c Normal file
View File

@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/09 14:28:08 by whaffman #+# #+# */
/* Updated: 2024/06/12 16:17:01 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <bsd/string.h>
unsigned int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
unsigned int ft_strlcat(char *dest, char *src, unsigned int size)
{
unsigned int src_len;
unsigned int dest_len;
unsigned int i;
src_len = ft_strlen(src);
dest_len = ft_strlen(dest);
if (dest_len >= (long int) size)
return (size + src_len);
i = 0;
while (src[i] && i < size - dest_len - 1)
{
dest[dest_len + i] = src[i];
i++;
}
dest[dest_len + i] = '\0';
return (src_len + dest_len);
}
#ifdef DEBUG
void test(char *dest1, char *dest2, char *src, int size)
{
int result_ft;
int result_bsd;
printf("=====================================\n");
printf("dest1(len = %d): \"%s\"\n", ft_strlen(dest1), dest1);
printf("dest2(len = %d): \"%s\"\n", ft_strlen(dest2), dest2);
printf("src(len = %d): \"%s\"\n", ft_strlen(src), src);
result_ft = ft_strlcat(dest1, src, size);
result_bsd = strlcat(dest2, src, size);
printf("ft: %d\n", result_ft);
printf("bsd: %d\n", result_bsd);
printf("dest1(ft) : \"%s\"\n", dest1);
printf("dest2(bsd): \"%s\"\n", dest2);
}
int main(void)
{
char dest1[5];
char dest2[5];
strcpy(dest1, "Ha");
strcpy(dest2, "Ha");
test(dest1, dest2, "ai", 4);
}
#endif

43
c04/ex00/ft_strlen.c Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 15:01:31 by whaffman #+# #+# */
/* Updated: 2024/06/12 11:10:18 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
int ft_strlen(char *str)
{
int i;
i = 0;
while (*str++)
i++;
return (i);
}
#ifdef DEBUG
void test(char *str)
{
printf("str: \"%s\"\n", str);
printf("strlen: %ld\n", strlen(str));
printf("ft_strlen: %d\n\n", ft_strlen(str));
}
int main(void)
{
test("Vogon");
test("");
test("Zaphod Beeblebrox");
return (0);
}
#endif

36
c04/ex01/ft_putstr.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 15:02:05 by whaffman #+# #+# */
/* Updated: 2024/06/12 11:13:48 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str)
{
write(1, str, 1);
str++;
}
}
#ifdef DEBUG
int main(void)
{
ft_putstr("Hello\n");
ft_putstr("");
ft_putstr("Don't ");
ft_putstr("Panic!!\n");
ft_putstr("And don't forget your towel\n");
return (0);
}
#endif

57
c04/ex02/ft_putnbr.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 15:01:50 by whaffman #+# #+# */
/* Updated: 2024/06/12 11:16:22 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putnbr(int nb)
{
char c;
if (-2147483648 == nb)
{
write(1, "-2147483648", 11);
}
else if (nb >= 0 && nb <= 9)
{
c = nb + '0';
write(1, &c, 1);
}
else if (nb < 0)
{
write(1, "-", 1);
ft_putnbr(-nb);
}
else if (nb > 9)
{
ft_putnbr(nb / 10);
ft_putnbr(nb % 10);
}
}
#ifdef DEBUG
int main(void)
{
ft_putnbr(0);
write(1, "\n", 1);
ft_putnbr(-10);
write(1, "\n", 1);
ft_putnbr(42);
write(1, "\n", 1);
ft_putnbr(-2147483648);
write(1, "\n", 1);
ft_putnbr(2147483647);
write(1, "\n", 1);
return (0);
}
#endif

69
c04/ex03/ft_atoi.c Normal file
View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 15:22:52 by whaffman #+# #+# */
/* Updated: 2024/06/12 20:21:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
int ft_isdigit(char c)
{
return (c >= '0' && c <= '9');
}
int ft_isspace(char c)
{
return (' ' == c || '\f' == c || \
'\n' == c || '\r' == c || \
'\t' == c || '\v' == c);
}
int ft_atoi(char *str)
{
int sign;
int res;
while (ft_isspace(*str))
str++;
res = 0;
sign = 1;
while (*str == '-' || *str == '+')
{
if (*str == '-')
sign *= -1;
str++;
}
while (ft_isdigit(*str))
res = 10 * res + sign * (*str++ - '0');
return (res);
}
#ifdef DEBUG
void test(char *str)
{
printf("string: \"%s\"\n", str);
printf("int: %d\n", ft_atoi(str));
}
int main(void)
{
test("");
test("123");
test("-123");
test("-2147483648");
test("2147483647");
test(" ----+++-1234");
test("12345a1234");
test("123.34");
return (0);
}
#endif

90
c04/ex04/ft_putnbr_base.c Normal file
View File

@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 17:49:33 by whaffman #+# #+# */
/* Updated: 2024/06/12 18:50:38 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
int ft_strlen(char *str)
{
int n;
n = 0;
while (str[n])
n++;
return (n);
}
int ft_isrepeating(char *str)
{
int i;
while (*str)
{
i = 1;
while (str[i])
{
if (str[0] == str[i])
return (1);
i++;
}
str++;
}
return (0);
}
int ft_contains_illegal_char(char *str)
{
while (*str)
{
if (*str == '-' || *str == '+')
return (1);
str++;
}
return (0);
}
void ft_putnbr_base(int nbr, char *base)
{
int b;
b = ft_strlen(base);
if (ft_isrepeating(base) || b < 2 || ft_contains_illegal_char(base))
return ;
if (nbr >= 0 && nbr < b)
{
write(1, &base[nbr], 1);
}
else if (nbr < 0)
{
write(1, "-", 1);
if (nbr > -b)
ft_putnbr_base(-nbr, base);
else
{
ft_putnbr_base(nbr / (-b), base);
ft_putnbr_base(-(nbr % b), base);
}
}
else if (nbr >= b)
{
ft_putnbr_base(nbr / b, base);
ft_putnbr_base(nbr % b, base);
}
}
#ifdef DEBUG
int main(void)
{
ft_putnbr_base(123, "012345678");
}
#endif

88
c04/ex05/ft_atoi_base.c Normal file
View File

@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 09:17:21 by whaffman #+# #+# */
/* Updated: 2024/06/12 18:55:08 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_strlen(char *str)
{
int i;
i = 0;
while (*str++)
i++;
return (i);
}
int ft_isrepeating(char *str)
{
int i;
while (*str)
{
i = 1;
while (str[i])
{
if (str[0] == str[i])
return (1);
i++;
}
str++;
}
return (0);
}
int ft_indexof(char c, char *str)
{
int result;
result = 0;
while (str[result])
{
if (str[result] == c)
return (result);
result++;
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int sign;
int res;
while (ft_indexof(*str, " \f\n\r\t\v") >= 0)
str++;
res = 0;
sign = 1;
while (*str == '-' || *str == '+')
{
if (*str == '-')
sign *= -1;
str++;
}
while (ft_indexof(*str, base) >= 0)
res = ft_strlen(base) * res + ft_indexof(*str++, base);
return (sign * res);
}
#ifdef DEBUG
int main(void)
{
int r;
r = ft_atoi_base("-2147483648", "0123456789");
printf("%d", r);
return (0);
}
#endif

View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_iterative_factorial.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 10:52:17 by whaffman #+# #+# */
/* Updated: 2024/06/13 11:06:44 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_iterative_factorial(int nb)
{
int result;
result = 1;
if (nb >= 0)
{
while (nb > 1)
result *= nb--;
return (result);
}
return (0);
}
#ifdef DEBUG
int main(void)
{
int i;
i = -3;
while (i < 10)
{
printf("%d! = %d\n", i, ft_iterative_factorial(i));
i++;
}
return (0);
}
#endif

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_recursive_factorial.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 11:07:32 by whaffman #+# #+# */
/* Updated: 2024/06/14 12:17:33 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_recursive_factorial(int nb)
{
if (nb < 0)
return (0);
if (nb <= 1)
return (1);
return (nb * ft_recursive_factorial(nb - 1));
}
#ifdef DEBUG
int main(void)
{
int i;
i = -3;
while (i < 10)
{
printf("%d! = %d\n", i, ft_recursive_factorial(i));
i++;
}
return (0);
}
#endif

View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_iterative_power.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 11:12:36 by whaffman #+# #+# */
/* Updated: 2024/06/14 12:18:06 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_iterative_power(int nb, int power)
{
int result;
result = 1;
if (power < 0)
return (0);
if (power == 0)
return (1);
while (power > 0)
{
result *= nb;
power--;
}
return (result);
}
#ifdef DEBUG
int main(void)
{
int i;
int j;
i = -3;
j = -3;
while (i < 5)
{
while (j < 5)
{
printf("%d^%d = %d\n", i, j, ft_iterative_power(i, j));
j++;
}
j = -3;
i++;
}
return (0);
}
#endif

View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_recursive_power.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 11:28:27 by whaffman #+# #+# */
/* Updated: 2024/06/13 11:32:29 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_recursive_power(int nb, int power)
{
int result;
result = 1;
if (power < 0)
return (0);
else if (power == 0)
return (1);
else
return (nb * ft_recursive_power(nb, power - 1));
}
#ifdef DEBUG
int main(void)
{
int i;
int j;
i = -2;
j = -2;
while (i < 3)
{
while (j < 3)
{
printf("%d^%d = %d\n", i, j, ft_recursive_power(i, j));
j++;
}
j = -2;
i++;
}
return (0);
}
#endif

40
c05/ex04/ft_fibonacci.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fibonacci.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 11:34:54 by whaffman #+# #+# */
/* Updated: 2024/06/13 11:40:20 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_fibonacci(int index)
{
if (index < 0)
return (-1);
else if (index < 2)
return (index);
else
return (ft_fibonacci(index - 1) + ft_fibonacci(index - 2));
}
#ifdef DEBUG
int main(void)
{
int i;
i = -3;
while (i < 10)
{
printf("fib(%d) = %d\n", i, ft_fibonacci(i));
i++;
}
return (0);
}
#endif

46
c05/ex05/ft_sqrt.c Normal file
View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sqrt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 12:07:01 by whaffman #+# #+# */
/* Updated: 2024/06/13 12:18:19 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_sqrt(int nb)
{
int i;
if (nb <= 0)
return (0);
i = 1;
while (i * i <= nb)
{
if (i * i == nb)
return (i);
i++;
}
return (0);
}
#ifdef DEBUG
int main(void)
{
int i;
i = -3;
while (i < 10)
{
printf("sqrt(%d) = %d\n", i, ft_sqrt(i));
i++;
}
return (0);
}
#endif

47
c05/ex06/ft_is_prime.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_prime.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 12:20:04 by whaffman #+# #+# */
/* Updated: 2024/06/13 12:39:01 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_is_prime(int nb)
{
int i;
if (nb < 2)
return (0);
i = 2;
while (i * i <= nb)
{
if (nb % i == 0)
return (0);
i++;
}
return (1);
}
#ifdef DEBUG
int main(void)
{
int i;
i = -3;
while (i < 100)
{
if (ft_is_prime(i))
printf("ft_is_prime(%d) = %d\n", i, ft_is_prime(i));
i++;
}
return (0);
}
#endif

View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_find_next_prime.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 12:40:48 by whaffman #+# #+# */
/* Updated: 2024/06/13 13:09:20 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
int ft_is_prime(int nb)
{
int i;
if (nb < 2)
return (0);
i = 2;
while (i * i <= nb)
{
if (nb % i == 0)
return (0);
i++;
}
return (1);
}
int ft_find_next_prime(int nb)
{
nb++;
while (1)
{
if (ft_is_prime(nb))
return (nb);
nb++;
}
return (0);
}
#ifdef DEBUG
void test(int nb)
{
printf("After %d the next prime is %d.\n", nb, ft_find_next_prime(nb));
}
int main(void)
{
test(3);
test(31);
test(4);
test(29);
test(103);
test(256);
test(123456783);
return (0);
}
#endif

View File

@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ten_queens_puzzle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 13:11:27 by whaffman #+# #+# */
/* Updated: 2024/06/15 11:24:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
#define SIZE 10
void ft_put_board(int board[SIZE])
{
int i;
char c;
i = 0;
while (i < SIZE)
{
c = board[i] + '0';
write(1, &c, 1);
i++;
}
write(1, "\n", 1);
}
int ft_test_queens(int board[SIZE], int x, int y)
{
int i;
i = 0;
if (x == 0)
return (1);
while (i <= x)
{
if (board[i] == y || i + board[i] == x + y || i - board[i] == x - y)
return (0);
i++;
}
return (1);
}
void ft_add_queen(int board[SIZE], int *res, int x)
{
int y;
if (x == SIZE)
{
*res += 1;
ft_put_board(&board[0]);
}
else
{
y = 0;
while (y < SIZE)
{
ft_put_board(&board[0]);
if (ft_test_queens(board, x, y))
{
board[x] = y;
ft_add_queen(board, res, x + 1);
board[x] = -3;
}
y++;
}
}
}
int ft_ten_queens_puzzle(void)
{
int res;
int board[SIZE];
int i;
i = 0;
res = 0;
while (i < SIZE)
{
board[i] = -3;
i++;
}
ft_add_queen(board, &res, 0);
return (res);
}
#ifdef DEBUG
int main(void)
{
printf("result = %d", ft_ten_queens_puzzle());
}
#endif

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_program_name.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 16:42:13 by whaffman #+# #+# */
/* Updated: 2024/06/16 16:56:36 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str != '\0')
{
write(1, str, 1);
str++;
}
}
int main(int argc, char **argv)
{
if (argc >= 1)
ft_putstr(argv[0]);
write(1, "\n", 1);
return (0);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_params.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 16:42:13 by whaffman #+# #+# */
/* Updated: 2024/06/12 19:38:55 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str != '\0')
{
write(1, str, 1);
str++;
}
}
int main(int argc, char **argv)
{
int i;
i = 1;
if (argc > 1)
{
while (i < argc)
{
ft_putstr(argv[i++]);
ft_putstr("\n");
}
}
return (0);
}

38
c06/ex02/ft_rev_params.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_rev_params.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 16:42:13 by whaffman #+# #+# */
/* Updated: 2024/06/12 19:38:27 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str != '\0')
{
write(1, str, 1);
str++;
}
}
int main(int argc, char **argv)
{
int i;
if (argc > 1)
{
i = argc - 1;
while (i > 0)
{
ft_putstr(argv[i--]);
ft_putstr("\n");
}
}
return (0);
}

80
c06/ex03/ft_sort_params.c Normal file
View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort_params.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 19:04:43 by whaffman #+# #+# */
/* Updated: 2024/06/16 16:58:42 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str != '\0')
{
write(1, str, 1);
str++;
}
}
void ft_swap(char **a, char **b)
{
char *temp;
temp = *a;
*a = *b;
*b = temp;
}
int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s1 == *s2)
{
s1++;
s2++;
}
return (*s1 - *s2);
}
void ft_sort_strings(char **strings, int size)
{
int i;
int swapped;
swapped = 1;
while (swapped == 1)
{
swapped = 0;
i = 2;
while (i < size)
{
if (ft_strcmp(strings[i - 1], strings[i]) > 0)
{
swapped = 1;
ft_swap(&strings[i - 1], &strings[i]);
}
i++;
}
}
}
int main(int argc, char **argv)
{
int i;
if (argc > 1)
{
ft_sort_strings(argv, argc);
i = 1;
while (i < argc)
{
ft_putstr(argv[i++]);
ft_putstr("\n");
}
}
return (0);
}

69
c07/ex00/ft_strdup.c Normal file
View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 16:46:42 by whaffman #+# #+# */
/* Updated: 2024/06/17 09:55:19 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int ft_strlen(char *str)
{
int i;
i = 0;
while (*str++)
i++;
return (i);
}
char *ft_strncpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
i = 0;
while (src[i] != '\0' && i < n)
{
dest[i] = src[i];
i++;
}
while (i < n)
{
dest[i] = '\0';
i++;
}
return (dest);
}
char *ft_strdup(char *src)
{
int str_size;
char *dest;
str_size = ft_strlen(src) + 1;
dest = (char *) malloc(str_size * sizeof(char));
if (dest)
ft_strncpy(dest, src, str_size);
return (dest);
}
#ifdef DEBUG
int main(void)
{
char src[7];
char *dest;
ft_strncpy(&src[0], "hallo", 7);
dest = ft_strdup(&src[0]);
printf("%s: %s", &src[0], dest);
}
#endif

64
c07/ex01/ft_range.c Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/16 15:57:44 by whaffman #+# #+# */
/* Updated: 2024/06/17 10:13:54 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
int *ft_range(int min, int max)
{
int *res;
int *ores;
if (min >= max)
return ((void *) 0);
res = (int *) malloc((max - min) * sizeof(int));
ores = res;
while (min < max)
{
*res = min;
res++;
min++;
}
return (ores);
}
#ifdef DEBUG
void test(int min, int max)
{
int *range;
range = ft_range(min, max);
if (!range)
{
printf("NULL POINTER\n");
return ;
}
while (min < max)
{
printf("%d ", *range++);
min++;
}
printf("\n");
}
int main(void)
{
test(-3, 3);
test(0, 10);
test(-10, 2);
test(3, -3);
test(3, 3);
return (0);
}
#endif

View File

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultimate_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/16 16:26:42 by whaffman #+# #+# */
/* Updated: 2024/06/19 11:56:32 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
int ft_ultimate_range(int **range, int min, int max)
{
int i;
i = 0;
if (min >= max)
{
*range = (void *) 0;
return (0);
}
*range = (int *) malloc((max - min) * sizeof(int));
if (!*range)
return (-1);
while (min < max)
{
(*range)[i] = min;
i++;
min++;
}
return (i);
}
#ifdef DEBUG
void test(int min, int max)
{
int *range;
range = 0;
ft_ultimate_range(&range, min, max);
if (!range)
{
printf("NULL POINTER\n");
return ;
}
while (min < max)
{
printf("%d ", *range);
range++;
min++;
}
printf("\n");
}
int main(void)
{
test(-3, 3);
test(0, 10);
test(-10, 2);
test(3, -3);
test(3, 3);
return (0);
}
#endif

94
c07/ex03/ft_strjoin.c Normal file
View File

@ -0,0 +1,94 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 10:17:32 by whaffman #+# #+# */
/* Updated: 2024/06/20 13:57:12 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char *ft_strcat(char *dest, char *src)
{
char *odest;
odest = dest;
while (*dest)
dest++;
while (*src)
*dest++ = *src++;
*dest = '\0';
return (odest);
}
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
int ft_strjoin_len(int size, char **strs, char *sep)
{
int result;
int i;
i = 0;
result = 0;
if (size <= 0)
return (0);
while (i < size)
{
result += ft_strlen(strs[i]);
i++;
}
result += (size - 1) * ft_strlen(sep);
return (result);
}
char *ft_strjoin(int size, char **strs, char *sep)
{
char *result;
int length;
int i;
i = 0;
length = ft_strjoin_len(size, strs, sep) + 1;
result = (char *) malloc(length * sizeof(char));
if (!result)
return ((void *) 0);
*result = '\0';
while (i < size)
{
ft_strcat(result, strs[i]);
if (i != size - 1 && ft_strlen(sep) > 0)
ft_strcat(result, sep);
i++;
}
return (result);
}
/*
int main(void)
{
char *strs[3];
char str1[] = "HAllo";
char str2[] = "";
char str3[] = "mwaah";
char sep[] = ", ";
strs[0] = &str1[0];
strs[1] = &str2[0];
strs[2] = &str3[0];
printf("%s", ft_strjoin(0, strs, sep));
printf("DONE!");
}
*/

View File

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 13:54:50 by whaffman #+# #+# */
/* Updated: 2024/06/19 20:36:11 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
void ft_putnbr_base(int nbr, char *base, char *result);
int ft_atoi_base(char *str, char *base);
int ft_isbase(char *base);
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
{
int i;
char *result;
if (ft_isbase(base_from) > 1 && ft_isbase(base_to) > 1)
{
result = (char *) malloc(33 * sizeof(char));
if (result)
{
*result = '\0';
i = ft_atoi_base(nbr, base_from);
ft_putnbr_base(i, base_to, result);
}
}
else
{
result = (void *) 0;
}
return (result);
}
#ifdef DEBUG
int main(void)
{
char *result;
result = (void *) 0;
result = ft_convert_base("-21474836489", "0123456789", "0123456789ABCDEF");
printf("%s", result);
free(result);
}
#endif

115
c07/ex04/ft_convert_base2.c Normal file
View File

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_convert_base2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 17:26:04 by whaffman #+# #+# */
/* Updated: 2024/06/19 18:51:15 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int ft_isbase(char *str)
{
int i;
int n;
n = 0;
while (str[n])
{
i = 1;
if (str[n] == '-' || str[n] == '+')
return (0);
while (str[n + i])
{
if (str[n] == str[n + i])
return (0);
i++;
}
n++;
}
return (n);
}
int ft_indexof(char c, char *str)
{
int result;
result = 0;
while (str[result])
{
if (str[result] == c)
return (result);
result++;
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int sign;
int res;
int b;
b = ft_isbase(base);
while (ft_indexof(*str, " \f\n\r\t\v") >= 0)
str++;
res = 0;
sign = 1;
while (*str == '-' || *str == '+')
{
if (*str == '-')
sign *= -1;
str++;
}
while (ft_indexof(*str, base) >= 0)
res = b * res + ft_indexof(*str++, base);
return (sign * res);
}
void ft_write_str(char c, char *str)
{
int i;
i = 0;
while (str[i])
{
i++;
}
str[i++] = c;
str[i] = '\0';
}
void ft_putnbr_base(int nbr, char *base, char *result)
{
int b;
b = ft_isbase(base);
if (b < 2)
return ;
if (nbr >= 0 && nbr < b)
{
ft_write_str(base[nbr], result);
}
else if (nbr < 0)
{
ft_write_str('-', result);
if (nbr > -b)
ft_putnbr_base(-nbr, base, result);
else
{
ft_putnbr_base(nbr / (-b), base, result);
ft_putnbr_base(-(nbr % b), base, result);
}
}
else if (nbr >= b)
{
ft_putnbr_base(nbr / b, base, result);
ft_putnbr_base(nbr % b, base, result);
}
}

126
c07/ex05/ft_split.c Normal file
View File

@ -0,0 +1,126 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/18 10:44:32 by whaffman #+# #+# */
/* Updated: 2024/06/19 14:46:27 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
/*
...Halooo dit is een zin, met woorden erin.
^ ^ ^
f->f---->b
^^ ^
ff->b
*/
#define WORD 1
#define SEP 0
#include <stdio.h>
#include <stdlib.h>
char **ft_split(char *str, char *charset);
char *ptr_skip(int word, char *str, char *sep);
int count_words(char *str, char *sep);
int is_sep(char c, char *sep);
int is_sep(char c, char *sep)
{
while (*sep)
{
if (c == *sep)
return (1);
sep++;
}
return (0);
}
int count_words(char *str, char *sep)
{
int i;
int count;
i = 0;
count = 0;
str = ptr_skip(SEP, str, sep);
while (str[i])
{
if (!is_sep(str[i], sep) \
&& (is_sep(str[i + 1], sep) \
|| str[i + 1] == '\0'))
count++;
i++;
}
return (count);
}
char *ptr_skip(int word, char *str, char *sep)
{
while (*str && word ^ is_sep(*str, sep))
str++;
return (str);
}
unsigned int ft_strlcpy(char *dest, char *src, unsigned int n)
{
unsigned int i;
i = 0;
while (src[i] && i + 1 < n)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (i);
}
char **ft_split(char *str, char *charset)
{
int n_words;
char **result;
char *end;
char *word;
int i;
str = ptr_skip(SEP, str, charset);
n_words = count_words(str, charset);
result = (char **) malloc((n_words + 1) * sizeof(char *));
i = 0;
result[0] = 0;
if (!*str)
return (result);
while (i < n_words)
{
end = ptr_skip(WORD, str, charset);
word = (char *) malloc((end - str + 1) * sizeof(char));
ft_strlcpy(word, str, (end - str + 1));
word[end - str] = '\0';
result[i] = word;
str = ptr_skip(SEP, end, charset);
i++;
}
result[i] = 0;
return (result);
}
/*
#ifdef DEBUG
int main(void)
{
char **segments;
segments = ft_split("", "");
while (*segments)
{
printf("%s\n", *segments);
segments++;
}
printf("%p",*segments);
}
#endif
*/

22
c08/ex00/ft.h Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 17:54:33 by whaffman #+# #+# */
/* Updated: 2024/06/17 17:56:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_H
# define FT_H
void ft_putchar(char c);
void ft_swap(int *a, int *b);
void ft_putstr(char *str);
int ft_strlen(char *str);
int ft_strcmp(char *s1, char *s2);
#endif

26
c08/ex01/ft_boolean.h Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_boolean.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 17:58:03 by whaffman #+# #+# */
/* Updated: 2024/06/22 11:41:28 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_BOOLEAN_H
# define FT_BOOLEAN_H
# include <unistd.h>
# define TRUE 1
# define FALSE 0
# define EVEN(nbr) ((nbr) % 2 == 0)
# define EVEN_MSG "I have an even number of arguments.\n"
# define ODD_MSG "I have an odd number of arguments.\n"
# define SUCCESS 0
typedef char t_bool;
#endif

18
c08/ex02/ft_abs.h Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_abs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 18:06:09 by whaffman #+# #+# */
/* Updated: 2024/06/20 14:05:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_ABS_H
# define FT_ABS_H
# define ABS(value) (((value) >= 0) * (value) - ((value) < 0) * (value))
#endif

Some files were not shown because too many files have changed in this diff Show More