Skip to content

Commit 24ccc39

Browse files
committed
[main] Added problems
1 parent 0033f8c commit 24ccc39

File tree

12 files changed

+302
-0
lines changed

12 files changed

+302
-0
lines changed

cProblems/sumPower/sumPower.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdlib.h>
2+
#include <math.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
unsigned long long sumDig(int n)
7+
{
8+
char *strNum;
9+
strNum = itoa(n, strNum, 10);
10+
int total = 0;
11+
for (int i = 0; i < strlen(strNum); i++)
12+
{
13+
total += strNum[i] - '0';
14+
}
15+
return total;
16+
}
17+
18+
unsigned long long power_sum_dig_term(unsigned n)
19+
{
20+
int *collector_arr = malloc(sizeof(int) * 1);
21+
int collector_len = 1;
22+
collector_arr[0] = 81;
23+
int power = 3;
24+
int starter = 9;
25+
int curr_num = 81;
26+
int power_limit = 6;
27+
printf("Before while\n");
28+
while (collector_len < n)
29+
{
30+
for (int i = 2;; i++)
31+
{
32+
for (int j = 2;; j++)
33+
{
34+
unsigned long long pow_num = (unsigned long long)pow(i, j);
35+
if (pow_num % i == 0 && sumDig(pow_num) == i && log(pow_num) / log(i) == j && pow_num > curr_num)
36+
{
37+
printf("in if\n");
38+
collector_arr = (int *)realloc(collector_arr, sizeof(int) * (collector_len + 1));
39+
collector_arr[collector_len - 1] = pow_num;
40+
}
41+
}
42+
}
43+
}
44+
printf("Result = %d\n", collector_arr[collector_len - 1]);
45+
return collector_arr[collector_len - 1];
46+
}
47+
48+
// 2 -- 9 | 9 * 9 | 81
49+
// 3 -- 8 | 8 * 8 * 8 | 512
50+
// 4 -- 7 | 7 * 7 * 7 * 7 | 2401
51+
// 3 -- 17 | 17 * 17 * 17 | 4913
52+
// 3 -- 18 | 18 * 18 * 18 | 5832
53+
// 3 -- 26 | 26 * 26 * 26 | 17576
54+
55+
int main(void)
56+
{
57+
printf("Result = %d\n", power_sum_dig_term(2));
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const fs = require("fs");
2+
3+
/**
4+
*
5+
* @param {number[][]} moves - The list of available moves
6+
* @param {number[]} curr_pattern - The current pattern we have
7+
* @param {number} curr_x - The current x
8+
* @param {number} curr_y - The current y
9+
* @param {number[][]} guesses - The array of guesses we have made, cannot exceed 500
10+
*/
11+
function blindfoldChess(moves) {
12+
let moves = {}; // hashmap consisting of the initial moves, and their chains
13+
let visited_spots = {}; // hashmap consisting of the initial moves, and all spots we visited along that path
14+
let curr_x = 0;
15+
let curr_y = 0;
16+
for (let i = 0; i < moves.length; i++) {
17+
moves[moves[i]] = [moves[i]];
18+
curr_x += moves[i][0];
19+
curr_y += moves[i][1];
20+
visited_spots[moves[i]];
21+
}
22+
}
23+
24+
// So what we need to do, is to pick a starting move, and then simulate the subsequent moves while removing duplicate moves from the equation
25+
// Then we map out each move, at each point, and what that resulting move places the piece at, as we are adding moves to the current chain, we check has that location already been
26+
// visited, if so, we don't add it to the chain, and move on to another move. Repeating this process for each initial move.
27+
28+
const kingMoves = [
29+
[-1, 1],
30+
[1, 0],
31+
[0, -1],
32+
[0, 1],
33+
[-1, 0],
34+
[1, -1],
35+
[-1, -1],
36+
[1, 1],
37+
];
38+
39+
fs.writeFileSync("output.txt", JSON.stringify(blindfoldChess(kingMoves)));
40+
console.log(blindfoldChess(kingMoves));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[0,1],[-1,0],[-1,2],[-2,1],[0,0],[-2,0],[0,2],[0,1],[1,-1],[1,1],[0,0],[2,-1],[0,-1],[2,1],[-1,0],[1,-1],[0,0],[-1,-1],[1,-2],[-1,-2],[1,0],[-1,2],[1,1],[0,0],[-1,1],[1,0],[-1,0],[1,2],[-2,1],[0,0],[-1,-1],[-1,1],[0,-1],[-2,-1],[0,1],[0,0],[2,-1],[1,-2],[1,0],[0,-1],[0,-2],[2,0],[-2,0],[0,-1],[-1,-2],[-1,0],[-2,-1],[0,-2],[0,0],[0,2],[2,1],[1,0],[1,2],[0,1],[2,0],[0,0]]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function isExactlyThree(n) {
2+
let ct = 1;
3+
for (let i = 2; i <= Math.ceil(Math.sqrt(n)) + 1; i++) {
4+
if (ct > 3) {
5+
return false;
6+
}
7+
if (n % i == 0) {
8+
ct++;
9+
}
10+
}
11+
return ct == 3;
12+
}

jsProblems/multCurry/multCurry.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
*
3+
* @param {number[]} arr
4+
*/
5+
function multCurry(arr) {
6+
return (mult) => arr.map((eachNumber) => eachNumber * mult);
7+
}
8+
9+
/**
10+
*
11+
* @param {number[]} arr
12+
*/
13+
function mirror(arr) {
14+
return arr.concat(arr.slice().reverse());
15+
}
16+
17+
class Book {
18+
title;
19+
author;
20+
21+
constructor(title, author) {
22+
this.title = title;
23+
this.author = author;
24+
}
25+
26+
getTitle() {
27+
return `Title: ${this.title}`;
28+
}
29+
30+
getAuthor() {
31+
return `Author: ${this.author}`;
32+
}
33+
}
34+
35+
const PP = new Book("Pride and Prejudice", "Jane Austen");
36+
const H = new Book("Hamlet", "William Shakespeare");
37+
const WP = new Book("War and Peace", "Leo Tolstoy");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
*
3+
* @param {string[]} act
4+
* @param {string} txt
5+
*/
6+
function runningAthlete(act, txt) {
7+
let resultStr = "";
8+
for (let i = 0; i < act.length; i++) {
9+
// O(n)
10+
switch (act[i]) {
11+
case "run": {
12+
switch (txt[i]) {
13+
case "_": {
14+
resultStr += "_";
15+
break;
16+
}
17+
case "|": {
18+
resultStr += "/";
19+
}
20+
}
21+
break;
22+
}
23+
case "jump": {
24+
switch (txt[i]) {
25+
case "_": {
26+
resultStr += "x";
27+
break;
28+
}
29+
case "|": {
30+
resultStr += "|";
31+
break;
32+
}
33+
}
34+
break;
35+
}
36+
}
37+
}
38+
return resultStr;
39+
}

output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[-1,1],[1,0],[0,-1],[0,1],[-1,0],[1,-1],[-1,-1],[1,1]]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import math
2+
3+
def santa():
4+
num_tests = int(input())
5+
for i in range(num_tests):
6+
the_str = input()
7+
distinct_letters = []
8+
days = 0
9+
for eachchar in the_str:
10+
if eachchar not in distinct_letters and len(distinct_letters) == 3:
11+
days += 1
12+
distinct_letters = [eachchar]
13+
elif eachchar in distinct_letters:
14+
continue
15+
else:
16+
distinct_letters.append(eachchar)
17+
print(days + 1)
18+
19+
20+
if __name__ == '__main__':
21+
santa()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from operator import itemgetter
2+
from typing import List
3+
4+
5+
class Solution:
6+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
7+
hashmap = {}
8+
for eachnumber in nums: # O(n)
9+
if eachnumber in hashmap:
10+
hashmap[eachnumber] += 1
11+
else:
12+
hashmap[eachnumber] = 1
13+
keys = sorted(list(hashmap.items()), key=itemgetter(1))
14+
t_keys = keys[len(keys) - k:]
15+
nums = []
16+
for eachentry in t_keys:
17+
nums.append(eachentry[0])
18+
return nums
19+
20+
21+
22+
if __name__ == '__main__':
23+
elems = [1, 1, 1, 2, 2, 3]
24+
k = 2
25+
sol = Solution()
26+
print(sol.topKFrequent(elems, k))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const fs = require("fs");
2+
3+
/**
4+
*
5+
* @param {number[][]} moves - The list of available moves
6+
* @param {number[]} curr_pattern - The current pattern we have
7+
* @param {number} curr_x - The current x
8+
* @param {number} curr_y - The current y
9+
* @param {number[][]} guesses - The array of guesses we have made, cannot exceed 500
10+
*/
11+
function blindfoldChess(moves: number[][]) {
12+
let move_chains: { [key: string]: number[][] } = {}; // hashmap consisting of the initial moves, and their chains
13+
let visited_spots: { [key: string]: number[][] } = {}; // hashmap consisting of the initial moves, and all spots we visited along that path
14+
let curr_x = 0;
15+
let curr_y = 0;
16+
for (let i = 0; i < moves.length; i++) {
17+
move_chains[`${moves[i][0]},${moves[i][1]}`] = [moves[i]];
18+
curr_x += moves[i][0];
19+
curr_y += moves[i][1];
20+
visited_spots[`${moves[i][0]},${moves[i][1]}`] = [
21+
...visited_spots[`${moves[i][0]},${moves[i][1]}`],
22+
[curr_x, curr_y],
23+
];
24+
}
25+
}
26+
27+
// So what we need to do, is to pick a starting move, and then simulate the subsequent moves while removing duplicate moves from the equation
28+
// Then we map out each move, at each point, and what that resulting move places the piece at, as we are adding moves to the current chain, we check has that location already been
29+
// visited, if so, we don't add it to the chain, and move on to another move. Repeating this process for each initial move.
30+
31+
// const kingMoves = [
32+
// [-1, 1],
33+
// [1, 0],
34+
// [0, -1],
35+
// [0, 1],
36+
// [-1, 0],
37+
// [1, -1],
38+
// [-1, -1],
39+
// [1, 1],
40+
// ];
41+
42+
// fs.writeFileSync("output.txt", JSON.stringify(blindfoldChess(kingMoves)));
43+
// console.log(blindfoldChess(kingMoves));

0 commit comments

Comments
 (0)