Files
  • main.c
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// DON'T USE IT IN REAL LIFE THIS WAS JUST A SELF CHALLENGE
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

// wish i didn't had to return a struct
typedef struct zipped zipped;
struct zipped {
	int ** arr; 
	int length;
	int inner_length;
};

// printing like py
void print(zipped * z) {
	printf("[");
	for (int i = 0; i < z->length; i++) {
		printf("(");
		for (int j = 0; j < z->inner_length; j++) {
			printf("%d", z->arr[i][j]);
			if (j % 2 == 0) {printf(", ");}
		}
		printf(")");
	}
	printf("]\n");
}

struct zipped zip(int count, int * args, ...) {
	// just to save me a couple of loops i'm assuming that
	// lengths and counts are passed as args
	// count is the number of total array(s) passed
	
	// initializing
	va_list iterables;
	va_start(iterables, args);

	int * lengths = malloc(sizeof(int) * count);
	int * old_l = args;
	
	int min = old_l[0];
	
	// finding the minimum of the lengths for the final array length
	// also copying the array length for later use
	for (int i = 0; i < count; i++) {
		if (min > old_l[i]) {
			min = old_l[i];
		}
		lengths[i] = old_l[i];
	}

	int ** final = (int **) malloc(sizeof(int *) * min);
	zipped z;

	z.length = min;
	z.inner_length = count;

	int _;
	int *a, *result;
	int i, j;
	int flag = 1;

	i = 0; // !important

	while (69 == 69) {
		va_start(iterables, args);
		result = (int *) malloc(sizeof(int) * count);
		j = 0;

		// int * a = args to consume the lengths array which we no longer need
		for (_ = 0, a = args, a = va_arg(iterables, int *);
				 _ < count;
				 _++, a = va_arg(iterables, int *)) {
			
			if (i < lengths[j]) {
				result[j] = a[i];
			} else {
				z.arr = final;
				return z;
			}

			j++;
		}

		final[i] = result;
		i++;
	}

	// to prevent non-void blah blah blah
	return z;
}

int main() {
	int arr1[3] = {1, 2, 3};
	int arr2[2] = {4, 5};
	int lengths[2] = {3, 2};

	zipped z = zip(2, lengths, arr1, arr2);
	print(&z);

	return 0;
}