Skip to content

Commit af80f46

Browse files
committed
[main] Added functions
1 parent 85c5877 commit af80f46

File tree

17 files changed

+772
-98
lines changed

17 files changed

+772
-98
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ obj
44
bin
55
*.cache
66
*.cache
7-
*.log
7+
*.log
8+
9+
tsProblems/node_modules

esoteric/bf/bf.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
def brain_luck(code, program_input):
2+
TAPE_LEN = 4000
3+
CODE_LEN = len(code)
4+
5+
tape = [0] * TAPE_LEN
6+
bracket_idxs = {}
7+
tape_idx = 0
8+
code_idx = 0
9+
output = ""
10+
11+
while code_idx < CODE_LEN:
12+
if code[code_idx] == ",":
13+
tape[tape_idx] = ord(program_input[0])
14+
program_input = program_input[1:]
15+
elif code[code_idx] == "+":
16+
tape[tape_idx] = (tape[tape_idx] + 1) % 256
17+
elif code[code_idx] == "-":
18+
tape[tape_idx] -= 1
19+
if tape[tape_idx] < 0:
20+
tape[tape_idx] = 255
21+
elif code[code_idx] == ">":
22+
tape_idx += 1
23+
elif code[code_idx] == "<":
24+
tape_idx -= 1
25+
elif code[code_idx] == ".":
26+
output += chr(tape[tape_idx])
27+
elif code[code_idx] == "[":
28+
if code_idx in bracket_idxs and code[code_idx] == 0:
29+
code_idx = bracket_idxs[code_idx]
30+
else:
31+
get_matching_bracket(code, code_idx, bracket_idxs)
32+
if code[code_idx] == 0:
33+
code_idx = bracket_idxs[code_idx]
34+
35+
elif code[code_idx] == "]" and tape[tape_idx] != 0:
36+
code_idx = bracket_idxs[code_idx]
37+
38+
code_idx += 1
39+
40+
return output
41+
42+
43+
def get_matching_bracket(code, idx, bracket_idxs):
44+
match_idx = idx + 1
45+
46+
while match_idx < len(code) and (
47+
code[match_idx] != "]" or match_idx in bracket_idxs
48+
):
49+
if code[match_idx] == "[" and match_idx not in bracket_idxs:
50+
get_matching_bracket(code, match_idx, bracket_idxs)
51+
match_idx += 1
52+
53+
bracket_idxs[idx] = match_idx
54+
bracket_idxs[match_idx] = idx
55+
56+
57+
if __name__ == "__main__":
58+
x = ",>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]"
59+
print(brain_luck(x, "\n"))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def interpreter(tape: str) -> str:
2+
output = ""
3+
tape_value = 0
4+
for each_char in tape:
5+
if each_char == "?":
6+
tape_value += 1
7+
elif each_char == "!":
8+
output += chr(tape_value)
9+
tape_value = 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://esolangs.org/wiki/(%3F!)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class kataExampleTest {
2+
public static String[] kataExampleTwist() {
3+
String[] websites = {};
4+
for (int i = 0; i < 100; i++) {
5+
6+
}
7+
return websites;
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function* letters() {
2+
yield* "abcdefghijklmnopqrstuvwxyz";
3+
yield* "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4+
while (true) {
5+
console.log(yield* letters());
6+
}
7+
}
8+
9+
let iter = letters();
10+
for (let i = 0; i < 109; i++) {
11+
console.log(iter.next().value);
12+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def countingSort(self, arr, exp1):
6+
n = len(arr)
7+
8+
# The output array elements that will have sorted arr
9+
output = [0] * (n)
10+
11+
# initialize count array as 0
12+
count = [0] * (10)
13+
14+
# Store count of occurrences in count[]
15+
for i in range(0, n):
16+
index = arr[i] / exp1
17+
count[int((index) % 10)] += 1
18+
19+
# Change count[i] so that count[i] now contains actual
20+
# position of this digit in output array
21+
for i in range(1, 10):
22+
count[i] += count[i - 1]
23+
24+
# Build the output array
25+
i = n - 1
26+
while i >= 0:
27+
index = arr[i] / exp1
28+
output[count[int((index) % 10)] - 1] = arr[i]
29+
count[int((index) % 10)] -= 1
30+
i -= 1
31+
32+
# Copying the output array to arr[],
33+
# so that arr now contains sorted numbers
34+
i = 0
35+
for i in range(0, len(arr)):
36+
arr[i] = output[i]
37+
38+
# Method to do Radix Sort
39+
def radixSort(self, arr, max1):
40+
41+
# Do counting sort for every digit. Note that instead
42+
# of passing digit number, exp is passed. exp is 10^i
43+
# where i is current digit number
44+
exp = 1
45+
while max1 / exp > 0:
46+
self.countingSort(arr, exp)
47+
exp *= 10
48+
return arr
49+
50+
def firstMissingPositive(self, nums: List[int]) -> int:
51+
"""
52+
Find the first missing positive number in O(n) time, meaning that we cycle through the array once and only once, no double fors
53+
54+
Args:
55+
nums (List[int]): list of numbers to analyze
56+
57+
Returns:
58+
int: the first missing positive
59+
"""
60+
altered_nums = set()
61+
the_sum = 0
62+
for each_number in nums:
63+
if each_number > 0 and each_number not in altered_nums:
64+
altered_nums.add(each_number)
65+
the_sum += each_number
66+
67+
arr = self.radixSort(list(altered_nums), max(altered_nums))
68+
69+
print(arr)
70+
71+
if len(arr) == 0:
72+
return 1
73+
74+
min_number = arr[0]
75+
max_number = arr[-1]
76+
range_sum = ((min_number + max_number) * (max_number - min_number + 1)) // 2
77+
if range_sum == the_sum and min_number == 1:
78+
return max_number + 1
79+
else:
80+
if min_number == 1:
81+
## cycle through, find gap, return
82+
for i in range(len(arr) - 1):
83+
if arr[i + 1] - arr[i] > 1:
84+
return arr[i] + 1
85+
return -1
86+
return 1
87+
88+
def firstMissingPositiveV2(self, nums: List[int]) -> int:
89+
90+
n = len(nums)
91+
r_n = range(n)
92+
for i in r_n:
93+
if nums[i] <= 0 or nums[i] > n:
94+
nums[i] = n + 1
95+
96+
for i in r_n:
97+
i_abs = abs(nums[i])
98+
99+
if i_abs > n:
100+
continue
101+
102+
i_abs -= 1
103+
if nums[i_abs] > 0:
104+
nums[i_abs] = -1 * nums[i_abs]
105+
106+
for i in r_n:
107+
if nums[i] >= 0:
108+
return i + 1
109+
110+
return n + 1
111+
112+
113+
if __name__ == "__main__":
114+
a = [1, 2, 0]
115+
b = [3, 4, -1, 1]
116+
c = [7, 8, 9, 11, 12]
117+
d = [2]
118+
e = [1, 1]
119+
f = [
120+
3,
121+
17,
122+
7,
123+
16,
124+
16,
125+
8,
126+
-4,
127+
5,
128+
-4,
129+
3,
130+
-2,
131+
18,
132+
34,
133+
5,
134+
1,
135+
-7,
136+
3,
137+
3,
138+
27,
139+
8,
140+
23,
141+
3,
142+
-3,
143+
2,
144+
27,
145+
8,
146+
15,
147+
7,
148+
-6,
149+
15,
150+
23,
151+
-6,
152+
3,
153+
2,
154+
5,
155+
23,
156+
21,
157+
3,
158+
2,
159+
]
160+
sol = Solution()
161+
print(sol.firstMissingPositiveV2(a))
162+
print(sol.firstMissingPositiveV2(b))
163+
print(sol.firstMissingPositiveV2(c))
164+
print(sol.firstMissingPositiveV2(d))
165+
print(sol.firstMissingPositiveV2(e))
166+
print(sol.firstMissingPositiveV2(f))

pythonProblems/iterpi/iterpi.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import math
2+
3+
4+
def iter_pi(epsilon):
5+
start = 1
6+
total = 1
7+
iterations = 1
8+
while abs((total - math.pi)) > epsilon:
9+
start = start / start + 2
10+
total += start
11+
start += 2
12+
iterations += 1
13+
return iterations
14+
15+
16+
if __name__ == "__main__":
17+
print(iter_pi(0.1))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import List
2+
3+
4+
def longer_word(word1: str, word2: str) -> str:
5+
"""
6+
Returns the longer word when passed 2 words
7+
8+
Args:
9+
word1 (str): First word
10+
word2 (str): Second word
11+
12+
Returns:
13+
str: The longer word
14+
"""
15+
if len(word1) > len(word2):
16+
return word1
17+
elif len(word2) > len(word1):
18+
return word2
19+
else:
20+
return word1
21+
22+
23+
def longest_word(sentence: str | List[str]) -> str:
24+
"""
25+
Computes longest word recursively given a sentence
26+
27+
Args:
28+
sentence (str): the sentence we are given
29+
30+
Returns:
31+
str: The longest word
32+
"""
33+
# "This has the longest word." --> ['This', 'has', 'the', 'longest', 'word']
34+
# base case: 1 element in the list, then we just return it
35+
split_sentence = sentence.split(" ")
36+
if (
37+
len(split_sentence) == 1
38+
): # doing this, because we receive the sentence initially as a str, and if it is a list, testing if it only contains 1 element
39+
return sentence[0]
40+
else:
41+
# magic happens, we recursively call the longest_word, with a SHORTER argument, aka reducing the problem
42+
# that gives us an array of all the words in the sentence
43+
return longer_word(split_sentence[0], longest_word(" ".join(split_sentence)))
44+
45+
46+
if __name__ == "__main__":
47+
example_sentence = "This has the longest word."
48+
print(longest_word(example_sentence))
49+
print(longest_word(example_sentence) == "longest")

tsProblems/buyCar/buyCar.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.nbMonths = void 0;
4+
function nbMonths(startPriceOld, startPriceNew, savingPerMonth, percentLossByMonth) {
5+
var months = 0;
6+
var currentSavings = 0;
7+
var tmpStartPriceOld = startPriceOld; // avoid mutating
8+
var tmpStartPriceNew = startPriceNew;
9+
while (tmpStartPriceNew - (currentSavings + tmpStartPriceOld) > 0) {
10+
months++;
11+
if (months > 0 && months % 2 == 0) {
12+
percentLossByMonth += 0.5;
13+
}
14+
currentSavings += savingPerMonth;
15+
tmpStartPriceOld -= tmpStartPriceOld * (percentLossByMonth / 100);
16+
tmpStartPriceNew -= tmpStartPriceNew * (percentLossByMonth / 100);
17+
}
18+
var currTotal = currentSavings + tmpStartPriceOld;
19+
return [months, Math.round(currTotal - tmpStartPriceNew)];
20+
}
21+
exports.nbMonths = nbMonths;
22+
console.log(nbMonths(12000, 8000, 1000, 1.5));
23+
console.log(nbMonths(2000, 8000, 1000, 1.5));

0 commit comments

Comments
 (0)