Skip to content

Commit 3b9d908

Browse files
committed
[main] Added funcs
1 parent 6722060 commit 3b9d908

File tree

6 files changed

+638
-15
lines changed

6 files changed

+638
-15
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
* @param {number[]} arr
4+
*/
5+
function highestRank(arr) {
6+
occurrences = {};
7+
arr.forEach((e) => {
8+
if (e in occurrences) {
9+
occurrences[e] += 1;
10+
} else {
11+
occurrences[e] = 1;
12+
}
13+
});
14+
15+
const sortedKeys = Object.entries(occurrences).sort((entry1, entry2) =>
16+
entry1[1] === entry2[1] ? entry1[0] - entry2[0] : entry1[1] - entry2[1]
17+
);
18+
return +sortedKeys.slice(-1)[0][0];
19+
}
20+
21+
const arr1 = [12, 10, 8, 12, 7, 6, 4, 10, 12];
22+
const arr2 = [12, 10, 8, 12, 7, 6, 4, 10, 12, 10];
23+
const arr3 = [12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10];
24+
25+
console.log(highestRank(arr1));
26+
console.log(highestRank(arr2));
27+
console.log(highestRank(arr3));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from typing import Dict, List
2+
3+
4+
def simple_assembler(commands: List[str]) -> Dict[str, int]:
5+
"""
6+
Takes in a program string, and returns the registers present within the program
7+
8+
Args:
9+
commands (List[str]): Array of commands to execute
10+
11+
Returns:
12+
Dict[str, str]: The registers from executing the program
13+
"""
14+
registers = {}
15+
command_ind = 0
16+
while command_ind < len(commands):
17+
split_command = commands[command_ind].split(" ")
18+
command = split_command[0]
19+
if command == "inc" or command == "dec":
20+
register = split_command[1]
21+
if register in registers:
22+
registers[register] += 1 if command == "inc" else -1
23+
command_ind += 1
24+
elif command == "mov":
25+
register = split_command[1]
26+
value: str = split_command[2]
27+
if value.replace("-", "").isnumeric():
28+
registers[register] = int(value)
29+
elif value in registers:
30+
registers[register] = registers[value]
31+
command_ind += 1
32+
else:
33+
# jump
34+
check_register = split_command[1]
35+
if (
36+
not check_register.replace("-", "").isnumeric()
37+
and registers[check_register] != 0
38+
):
39+
## execute jump
40+
command_ind += int(split_command[2])
41+
elif (
42+
check_register.replace("-", "").isnumeric() and int(check_register) != 0
43+
):
44+
command_ind += int(split_command[2])
45+
else:
46+
command_ind += 1
47+
return registers
48+
49+
50+
if __name__ == "__main__":
51+
code = """\
52+
mov a 5
53+
inc a
54+
dec a
55+
dec a
56+
jnz a -1
57+
inc a"""
58+
print(simple_assembler(code.splitlines()), {"a": 1})
59+
60+
code = """\
61+
mov c 12
62+
mov b 0
63+
mov a 200
64+
dec a
65+
inc b
66+
jnz a -2
67+
dec c
68+
mov a b
69+
jnz c -5
70+
jnz 0 1
71+
mov c a"""
72+
print(simple_assembler(code.splitlines()), {"a": 409600, "c": 409600, "b": 409600})
73+
print(simple_assembler(["mov a -10", "mov b a", "inc a", "dec b", "jnz a -2"]))

pythonProblems/test/test.py

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,94 @@
1-
# set the board
2-
board = ["-","-","-",
3-
"-","-","-",
4-
"-","-","-"]
1+
from typing import List
2+
from itertools import accumulate
53

6-
# set the player
4+
# set the board
5+
board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"]
6+
7+
# set the player
78
currentPlayer = "p1"
89
winner = None
910
gameRunning = True
10-
# print the game board
11+
# print the game board
1112
def printBoard(board):
12-
print(board[0]+"|" + board[1]+"|"+ board[2] + "|")
13+
print(board[0] + "|" + board[1] + "|" + board[2] + "|")
1314
print("-----------")
14-
print(board[3]+"|" + board[4]+"|"+ board[5] + "|")
15+
print(board[3] + "|" + board[4] + "|" + board[5] + "|")
1516
print("-----------")
16-
print(board[6]+"|" + board[7]+"|"+ board[8] + "|")
17+
print(board[6] + "|" + board[7] + "|" + board[8] + "|")
1718
print("-----------")
1819

1920

20-
def _(int, str)-> MemoryError | ZeroDivisionError:
21+
def _(int, str) -> MemoryError | ZeroDivisionError:
2122
print(len(list(set(list()))))
2223
if int == 1 and str == 1:
23-
return (not not(True + (not True and not False or (not False)))) + (_.__code__.co_argcount - 1)
24+
return (not not (True + (not True and not False or (not False)))) + (
25+
_.__code__.co_argcount - 1
26+
)
2427
else:
25-
if False is int and False + (not(not(not(not(not(not(not(not(not(False)))))))))):
28+
if False is int and False + (
29+
not (not (not (not (not (not (not (not (not (False)))))))))
30+
):
2631
return 3
2732
else:
2833
return (lambda: lambda: lambda: round((lambda: float(int + str))()))()()()
2934

3035

31-
print(_(4, 4))
36+
def my_first_kata(a, b):
37+
return False if isinstance(a, int) or isinstance(b, int) else (a % b) + (b % a)
38+
39+
40+
def uefa_euro_2016(teams, scores):
41+
if scores[0] != scores[1]:
42+
return f"At match {teams[0]} - {teams[1]}, {teams[0] if scores[0] > scores[1] else teams[1]} won!"
43+
else:
44+
return f"At match {teams[0]} - {teams[1]}, teams played draw."
45+
46+
47+
def sort_my_string(s: str) -> str:
48+
evens = ""
49+
odds = ""
50+
for ind, element in enumerate(s):
51+
if ind % 2 == 0:
52+
evens += element
53+
else:
54+
odds += element
55+
return f"{evens} {odds}"
56+
57+
58+
def guessBlue(blue_start, red_start, blue_pulled, red_pulled):
59+
blue_left = blue_start - blue_pulled
60+
red_left = red_start - red_pulled
61+
print(blue_left, red_left)
62+
return blue_left / (blue_left + red_left)
63+
64+
65+
def reverseNoBuiltin(lst: List[str | int]) -> List[str | int]:
66+
empty_list = list()
67+
for i in range(len(lst) - 1, -1, -1):
68+
empty_list.append(lst[i])
69+
return empty_list
70+
71+
72+
def halving_sum(n):
73+
total_sum = 0
74+
start = 1
75+
exp = 1
76+
while start < n:
77+
total_sum += n // start
78+
start = 2**exp
79+
exp += 1
80+
return total_sum
81+
82+
83+
def averages(arr):
84+
averages_arr = []
85+
for i in range(1, len(arr) if arr is not None else 1):
86+
averages_arr.append((arr[i] + arr[i - 1]) / 2)
87+
return averages_arr
88+
89+
90+
def is_ruby_coming(lst):
91+
return any(["Ruby" in x["language"] for x in lst])
92+
93+
94+
print(halving_sum(25))

0 commit comments

Comments
 (0)