Skip to content

Commit 0033f8c

Browse files
committed
[main] Added functions
1 parent 54e6d62 commit 0033f8c

File tree

10 files changed

+352
-0
lines changed

10 files changed

+352
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stddef.h>
2+
3+
double mean_square_error(size_t n, const int a[n], const int b[n])
4+
{
5+
double absSum = 0;
6+
for (int i = 0; i < n; i++)
7+
{
8+
int diff = abs(a[i] - b[i]);
9+
absSum += diff * diff;
10+
}
11+
return absSum / n;
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
*
3+
* @param {number[]} arr1
4+
* @param {number[]} arr2
5+
* @returns {number[]} The merged sorted array
6+
*/
7+
function mergeArrays(arr1, arr2) {
8+
return [...new Set(arr1.concat(arr2))].sort((e1, e2) => e1 - e2);
9+
}
10+
11+
console.log(mergeArrays([1, 3, 5, 7, 9, 11, 12], [1, 2, 3, 4, 5, 10, 12]));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function nthEven(n) {
2+
return n * 2 - 2;
3+
}
4+
5+
for (let i = 0; i <= 100; i++) {
6+
console.log(nthEven(i));
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
*
3+
* @param {number} d - the # of days the car is rented for
4+
*/
5+
function rentalCarCost(d) {
6+
return d * 40 - ((d >= 3 && d < 7 ? 20 : 0) + (d >= 7 ? 50 : 0));
7+
}
8+
9+
console.log(rentalCarCost(3));
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import string
2+
3+
def alphanumeric(password: str) -> bool:
4+
print('password = ', password)
5+
if len(password) == 0:
6+
return False
7+
else:
8+
return ' ' not in password and '_' not in password and '\n' not in password and '\t' not in password and len([x for x in string.punctuation if x in password]) == 0

tsProblems/gematria/gematria.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export function gematria(str: string): number {
2+
const letter_dict: { [key: string]: number } = {
3+
a: 1,
4+
b: 2,
5+
c: 3,
6+
d: 4,
7+
e: 5,
8+
f: 6,
9+
g: 7,
10+
h: 8,
11+
i: 9,
12+
k: 10,
13+
l: 20,
14+
m: 30,
15+
n: 40,
16+
o: 50,
17+
p: 60,
18+
q: 70,
19+
r: 80,
20+
s: 90,
21+
t: 100,
22+
u: 200,
23+
x: 300,
24+
y: 400,
25+
z: 500,
26+
j: 600,
27+
v: 700,
28+
w: 900,
29+
};
30+
31+
return str
32+
.toLowerCase()
33+
.split("")
34+
.map((eachLetter) =>
35+
letter_dict[eachLetter] ? letter_dict[eachLetter] : 0
36+
)
37+
.reduce((e1, e2) => e1 + e2, 0);
38+
}

tsProblems/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "tsproblems",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC"
12+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.decrypt = exports.encrypt = exports.flipBit = void 0;
4+
function flipBit(bit) {
5+
return bit === "0" ? "1" : "0";
6+
}
7+
exports.flipBit = flipBit;
8+
function encrypt(text) {
9+
var _a, _b, _c, _d;
10+
var allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .";
11+
var doesContainIllegalChars = text
12+
.split("")
13+
.some(function (eachChar) { return !allowedChars.includes(eachChar); });
14+
if (doesContainIllegalChars) {
15+
throw new Error("Contains illegal chars");
16+
}
17+
else {
18+
var splitStr = text.split("");
19+
var binValues = [];
20+
for (var _i = 0, splitStr_1 = splitStr; _i < splitStr_1.length; _i++) {
21+
var eachStr = splitStr_1[_i];
22+
var convertedChar = allowedChars
23+
.indexOf(eachStr)
24+
.toString(2)
25+
.padStart(6, "0");
26+
binValues.push(convertedChar);
27+
}
28+
for (var i = 0; i < binValues.length; i++) {
29+
var firstBin = binValues[i].split("");
30+
// Step 1
31+
if (i < binValues.length - 1) {
32+
var forwardBin = binValues[i + 1].split("");
33+
_a = [forwardBin[0], firstBin[4]], firstBin[4] = _a[0], forwardBin[0] = _a[1];
34+
}
35+
// Step 2
36+
firstBin[1] = flipBit(firstBin[1]);
37+
firstBin[3] = flipBit(firstBin[3]);
38+
// Step 3
39+
_b = [
40+
firstBin[3],
41+
firstBin[4],
42+
firstBin[5],
43+
firstBin[0],
44+
firstBin[1],
45+
firstBin[2],
46+
], firstBin[0] = _b[0], firstBin[1] = _b[1], firstBin[2] = _b[2], firstBin[3] = _b[3], firstBin[4] = _b[4], firstBin[5] = _b[5];
47+
// Step 4
48+
for (var i_1 = 0; i_1 < firstBin.length - 1; i_1 += 2) {
49+
_c = [firstBin[i_1 + 1], firstBin[i_1]], firstBin[i_1] = _c[0], firstBin[i_1 + 1] = _c[1];
50+
}
51+
// Step 5
52+
firstBin.reverse();
53+
// Step 6
54+
_d = [firstBin[2], firstBin[0]], firstBin[0] = _d[0], firstBin[2] = _d[1];
55+
binValues[i] = firstBin.join("");
56+
}
57+
for (var _e = 0, binValues_1 = binValues; _e < binValues_1.length; _e++) {
58+
var eachBinValue = binValues_1[_e];
59+
console.log("bin = ", eachBinValue);
60+
var indexValue = Number.parseInt(eachBinValue, 2);
61+
console.log("ind = ", indexValue);
62+
console.log(allowedChars[indexValue]);
63+
}
64+
}
65+
return "";
66+
}
67+
exports.encrypt = encrypt;
68+
function decrypt(encryptedText) {
69+
return "";
70+
}
71+
exports.decrypt = decrypt;
72+
encrypt("B9");
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
export function flipBit(bit: string): string {
2+
return bit === "0" ? "1" : "0";
3+
}
4+
5+
export function encrypt(text: string): string {
6+
const allowedChars: string =
7+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .";
8+
const doesContainIllegalChars = text
9+
.split("")
10+
.some((eachChar) => !allowedChars.includes(eachChar));
11+
if (doesContainIllegalChars) {
12+
throw new Error("Contains illegal chars");
13+
} else {
14+
const splitStr = text.split("");
15+
const binValues: string[] = [];
16+
for (const eachStr of splitStr) {
17+
const convertedChar = allowedChars
18+
.indexOf(eachStr)
19+
.toString(2)
20+
.padStart(6, "0");
21+
binValues.push(convertedChar);
22+
}
23+
for (let i = 0; i < binValues.length; i++) {
24+
const firstBin = binValues[i].split("");
25+
// Step 1
26+
if (i < binValues.length - 1) {
27+
const forwardBin = binValues[i + 1].split("");
28+
[firstBin[4], forwardBin[0]] = [forwardBin[0], firstBin[4]];
29+
}
30+
31+
// Step 2
32+
firstBin[1] = flipBit(firstBin[1]);
33+
firstBin[3] = flipBit(firstBin[3]);
34+
35+
// Step 3
36+
[
37+
firstBin[0],
38+
firstBin[1],
39+
firstBin[2],
40+
firstBin[3],
41+
firstBin[4],
42+
firstBin[5],
43+
] = [
44+
firstBin[3],
45+
firstBin[4],
46+
firstBin[5],
47+
firstBin[0],
48+
firstBin[1],
49+
firstBin[2],
50+
];
51+
52+
// Step 4
53+
for (let i = 0; i < firstBin.length - 1; i += 2) {
54+
[firstBin[i], firstBin[i + 1]] = [firstBin[i + 1], firstBin[i]];
55+
}
56+
57+
// Step 5
58+
firstBin.reverse();
59+
60+
// Step 6
61+
[firstBin[0], firstBin[2]] = [firstBin[2], firstBin[0]];
62+
binValues[i] = firstBin.join("");
63+
}
64+
for (const eachBinValue of binValues) {
65+
console.log("bin = ", eachBinValue);
66+
const indexValue = Number.parseInt(eachBinValue, 2);
67+
console.log("ind = ", indexValue);
68+
console.log(allowedChars[indexValue]);
69+
}
70+
}
71+
return "";
72+
}
73+
74+
export function decrypt(encryptedText: string): string {
75+
return "";
76+
}
77+
78+
encrypt("B9");

tsProblems/tsconfig.json

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"lib": ["es2015"],
5+
/* Visit https://aka.ms/tsconfig to read more about this file */
6+
7+
/* Projects */
8+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
9+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
10+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
11+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
12+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
13+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
14+
15+
/* Language and Environment */
16+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
17+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
18+
// "jsx": "preserve", /* Specify what JSX code is generated. */
19+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
20+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
21+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
22+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
23+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
24+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
25+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
26+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
27+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
28+
29+
/* Modules */
30+
"module": "commonjs" /* Specify what module code is generated. */,
31+
// "rootDir": "./", /* Specify the root folder within your source files. */
32+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
33+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
34+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
35+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
36+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
37+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
38+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
39+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
40+
// "resolveJsonModule": true, /* Enable importing .json files. */
41+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
42+
43+
/* JavaScript Support */
44+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
45+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
46+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
47+
48+
/* Emit */
49+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
50+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
51+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
52+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
53+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
54+
// "outDir": "./", /* Specify an output folder for all emitted files. */
55+
// "removeComments": true, /* Disable emitting comments. */
56+
// "noEmit": true, /* Disable emitting files from a compilation. */
57+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
58+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
59+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
60+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
61+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
62+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
63+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
64+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
65+
// "newLine": "crlf", /* Set the newline character for emitting files. */
66+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
67+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
68+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
69+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
70+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
71+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
72+
73+
/* Interop Constraints */
74+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
75+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
76+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
77+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
78+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
79+
80+
/* Type Checking */
81+
"strict": true /* Enable all strict type-checking options. */,
82+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
83+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
84+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
85+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
86+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
87+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
88+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
89+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
90+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
91+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
92+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
93+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
94+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
95+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
96+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
97+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
98+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
99+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
100+
101+
/* Completeness */
102+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
103+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
104+
}
105+
}

0 commit comments

Comments
 (0)