Skip to content

Commit e76bdcc

Browse files
committed
[main] Added functions
1 parent 24ccc39 commit e76bdcc

File tree

15 files changed

+459
-1
lines changed

15 files changed

+459
-1
lines changed

cProblems/apple/apple.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const char *apple(int x)
2+
{
3+
return (x * x) > 1000 ? "It's hotter than th e sun!!" : "Help yourself to a honeycomb Yorkie for the glovebox.";
4+
}

cProblems/evenFib/evenFib.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
typedef unsigned long long ull;
4+
5+
ull even_fib(ull limit)
6+
{
7+
ull *fib = (ull *)malloc(sizeof(ull) * 3);
8+
ull *evens = (ull *)malloc(sizeof(ull) * 1);
9+
fib[0] = 0;
10+
fib[1] = 1;
11+
fib[2] = 1;
12+
evens[0] = 0;
13+
int len = 3;
14+
int ind = 2;
15+
int evenIndex = 0;
16+
ull new_number = 0;
17+
while (new_number < limit)
18+
{
19+
len++;
20+
fib = (ull *)realloc(fib, sizeof(ull) * len);
21+
ind++;
22+
new_number = fib[ind - 1] + fib[ind - 2];
23+
fib[ind] = new_number;
24+
if (new_number % 2 == 0 && new_number < limit)
25+
{
26+
evenIndex++;
27+
evens = (ull *)realloc(evens, sizeof(ull) * (evenIndex + 1));
28+
evens[evenIndex] = new_number;
29+
}
30+
}
31+
ull sum = 0;
32+
for (int i = 0; i < evenIndex + 1; i++)
33+
{
34+
sum += evens[i];
35+
}
36+
return sum;
37+
}
38+
39+
int main(void)
40+
{
41+
printf("Result = %u\n", even_fib(10));
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stddef.h>
2+
#include <limits.h>
3+
#include <stdio.h>
4+
5+
int find_max_product(size_t length, const int array[length])
6+
{
7+
int max_product = INT_MIN;
8+
for (int i = 0; i < length; i++)
9+
{
10+
// the # of digits apart
11+
int total_product = 1;
12+
for (int j = i; j < length; j += i + 1)
13+
{
14+
total_product *= array[j];
15+
}
16+
max_product = total_product > max_product ? total_product : max_product;
17+
}
18+
return max_product;
19+
}
20+
21+
int main(void)
22+
{
23+
int arr[7] = {11, 6, -2, 0, 5, -4, 2};
24+
printf("%d\n", find_max_product(sizeof(int) * 7, arr));
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
typedef unsigned int ui;
4+
5+
ui range_bit_count(ui a, ui b)
6+
{
7+
int totalCount = 0;
8+
for (int i = a; i <= b; i++)
9+
{
10+
int count = 0;
11+
int _i = i;
12+
while (_i > 0)
13+
{
14+
count += _i % 2;
15+
_i /= 2;
16+
}
17+
totalCount += count;
18+
}
19+
return totalCount;
20+
}
21+
22+
int main(void)
23+
{
24+
printf("%d\n", range_bit_count(2, 7));
25+
}

cppProblems/sumIntervals/sumIntervals.cpp

Whitespace-only changes.

jsProblems/findCombo/findCombo.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Whether or not the card can be played
3+
*
4+
* @param {string} resources - Your current resources
5+
* @param {string} required - The resources required
6+
*/
7+
const canPlay = (resources, required) => {
8+
/**
9+
* @type {Map}
10+
*/
11+
const requiredLetters = new Map();
12+
for (let i = 0; i < required.length; i++) {
13+
if (requiredLetters.has(required.charAt(i))) {
14+
requiredLetters.set(required.charAt(i), requiredLetters.get(required.charAt(i)) + 1);
15+
} else {
16+
requiredLetters.set(required.charAt(i), 1);
17+
}
18+
}
19+
const resourcesLetters = new Map();
20+
for (let i = 0; i < resources.length; i++) {
21+
if (resourcesLetters.has(resources.charAt(i))) {
22+
resourcesLetters.set(resources.charAt(i), resourcesLetters.get(resources.charAt(i)) + 1);
23+
} else {
24+
resourcesLetters.set(resources.charAt(i), 1);
25+
}
26+
}
27+
return [...resourcesLetters.keys()].every(e => requiredLetters.has(e) && resourcesLetters.get(e) >= requiredLetters.get(e));
28+
}
29+
30+
/**
31+
* Makes the play on the card, given the resources and playIn and playOut values
32+
*
33+
* @param {string} resources - Your current resources
34+
* @param {string} playIn - The resources you are losing
35+
* @param {string} playOut - The resources you are gaining
36+
*/
37+
const makePlay = (resources, playIn, playOut) => {
38+
39+
}
40+
41+
class GraphNodeEdge {
42+
/**
43+
* @type {GraphNode}
44+
*/
45+
connected_node;
46+
/**
47+
* @type {GraphNode}
48+
*/
49+
target_node;
50+
/**
51+
* @type {string}
52+
*/
53+
weight;
54+
}
55+
56+
class GraphNode {
57+
/**
58+
* @type {Array<GraphNodeEdge>}
59+
*/
60+
edges;
61+
62+
/**
63+
* Adds a node to the list of connected nodes this current node contains
64+
*
65+
* @param {GraphNode} new_node - The node we are adding
66+
*/
67+
add_edge(new_node) {
68+
this.edges.push({ connected_node: this, target_node: new_node, weight: })
69+
}
70+
71+
72+
}
73+
74+
class Graph {
75+
root;
76+
77+
}
78+
79+
const findCombo = function(starting, cards) {
80+
// create a graph, of all potential plays the user can make from resources I, and then assign a weight to each edge, see if there is any vertex that contains the resource D, and if there is, follow
81+
// the path back and see if it's possible to reach the beginning of the path from the end.
82+
// root being the initial resources
83+
84+
return {
85+
preparation: [],
86+
loop: []
87+
}
88+
}

jsProblems/foldr/foldr.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Object.defineProperty( Array.prototype, "foldr", { value: function foldr(fn,z) {
2+
return function _foldr(a) {
3+
if (a.length > 0) {
4+
value_ = fn(a[0], z);
5+
if (value_ ===
6+
return fn(a[0], _foldr(a.slice(1)));
7+
}
8+
return z;
9+
} (this)
10+
} } );
11+
12+
Object.defineProperty( String.prototype, "foldr", { value: [].foldr } );
13+
14+
15+
const nil = (x, z) => false;
16+
let i = 0;
17+
const arg = [1, 2, 3];
18+
const logging = fn => function logging(...a) { i++; return fn(...a); } ;
19+
console.log(arg.foldr(logging(nil), true));
20+
console.log(i);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function howManyLightsabersDoYouOwn(name) {
2+
return name === "Zach" ? 18 : 0;
3+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from typing import List
2+
3+
4+
def generate_determinant(matrix: List[List[int]], column_index: int) -> List[List[int]]:
5+
"""
6+
Generates the determinant from the column index given, which means that the column is removed alongside the row in which the element is present
7+
8+
Args:
9+
matrix (List[List[int]]): The matrix given
10+
column_index (int): The index of the column to remove
11+
12+
Returns:
13+
List[List[int]]: The matrix modified to have the determinant applied
14+
"""
15+
cloned_matrix: List[List[int]] = [x[:] for x in matrix]
16+
i = 0
17+
del cloned_matrix[i]
18+
for each_row in cloned_matrix:
19+
del cloned_matrix[i][column_index]
20+
i += 1
21+
return cloned_matrix
22+
23+
24+
def determinant(matrix: List[List[int]]) -> int:
25+
"""
26+
Generates a determinant of the matrix passed into the function
27+
28+
Args:
29+
matrix (List[List[int]]): The matrix we are given to calculate the determinant of
30+
31+
Returns:
32+
int: The determinant of the matrix, as described above.
33+
"""
34+
## what we have to do, is to first have 2 cases: 1x1, and 2x2, then we just basically do this
35+
# for each part of the matrix, we generate the determinant of the first row, by passing in the generated matrix into this same function, which will eventually
36+
# return a value for whichever matrix we pass into it
37+
if len(matrix) == 1:
38+
return matrix[0][0]
39+
elif len(matrix) == 2:
40+
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
41+
else:
42+
determinant_sum = 0
43+
minus_ = False
44+
# generate determinant of first row
45+
for j in range(len(matrix[0])):
46+
if minus_:
47+
determinant_sum -= matrix[0][j] * determinant(
48+
generate_determinant(matrix, j)
49+
)
50+
else:
51+
determinant_sum += matrix[0][j] * determinant(
52+
generate_determinant(matrix, j)
53+
)
54+
minus_ = not minus_
55+
return determinant_sum
56+
57+
58+
if __name__ == "__main__":
59+
m1 = [[4, 6], [3, 8]]
60+
m5 = [[2, 4, 2], [3, 1, 1], [1, 2, 0]]
61+
m6 = [
62+
[
63+
2,
64+
]
65+
]
66+
print(determinant([[5]]))
67+
print(determinant(m5))
68+
69+
"""
70+
| 2 4 2 |
71+
| 3 1 1 |
72+
| 1 2 0 |
73+
"""
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from typing import List
2+
3+
4+
def find_uniq(arr: List[str]) -> str:
5+
"""
6+
Given an array of strings, return the one string among all of them that contains similar letters
7+
8+
Args:
9+
arr (List[str]): The list of strings we are generating the unique string from
10+
11+
Returns:
12+
str: The unique string that does not contain any similar letters among all of the other elements
13+
"""
14+
unique_strings = {}
15+
modified_arr = ["".join(sorted(set(x.lower().replace(" ", "")))) for x in arr]
16+
17+
for ind, element in enumerate(modified_arr):
18+
if element in unique_strings:
19+
unique_strings[element] = -1
20+
else:
21+
unique_strings[element] = ind
22+
for ind, element in enumerate(unique_strings.items()):
23+
if element[1] != -1:
24+
return arr[element[1]]
25+
26+
27+
if __name__ == "__main__":
28+
arr2 = ["Aa", "aaa", "aaaaa", "BbBb", "Aaaa", "AaAaAa", "a"]
29+
arr3 = ["abc", "acb", "bac", "foo", "bca", "cab", "cba"]
30+
arr = [" ", "a", " "]
31+
arr4 = ["Tom Marvolo Riddle", "I am Lord Voldemort", "Harry Potter"]
32+
arr5 = ["i", "t", "t", "t"]
33+
print(find_uniq(arr))
34+
print(find_uniq(arr2))
35+
print(find_uniq(arr3))
36+
print(find_uniq(arr4))
37+
print(find_uniq(arr5))

0 commit comments

Comments
 (0)