Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions solutions/typescript/median_sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
declare var require: any;
declare var process: any;

var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);



const get_index = (arr: number[], num: number) => {
let i: number = Math.ceil(((arr.length)*1/3) - 1);
let j: number = Math.ceil(((arr.length)*2/3)-1);

while (true) {
console.log(arr[i], num, arr[j]);
let m: number = parseInt(data.join(''));
if (m == num) {
if (j-i == 1){
return j;
}
else {
i = i+(j-i)*1/3;
j = i+(j-i)*2/3;
}
}
else if (m == arr[i]) {
if (i == 0){
return 0
}
else if (j-i == 1) {
i = i -1;
j = i;
}
else {
i = i-(j-i)*2/3
j = i-(j-i)*1/3
}
}
else {
if (j == arr.length-1){
return arr.length;
}
else if (j-i == 1) {
i = j;
j = j+1;
}
else {
i = j+(j-i)*1/3
j = j+(j-i)*2/3
}
}
if (i == j){
j += 1
}
};
};


const medianSort = (t: number,n: number) => {

while (t>0) {
console.log(1,2,3);
let k = parseInt(data.join(''));
let arr: number[] = [];
if (k == 1) {
arr = [2, 1, 3]
}
else if (k == 2) {
arr = [1, 2, 3]
}
else {
arr = [1, 3, 2]
}
while (arr.length < n) {
let nextIndex = arr.length + 1
let placeIndex = get_index(arr, nextIndex)
arr.splice(placeIndex,0, nextIndex)
}


console.log(arr.join(' '));
let answer = parseInt(data.join(''));
if (answer == 1) {
console.log('correct');
}
if (answer == -1) {
throw new Error('ERROR');
}

t--;
};
};

let data: string[] = [];
let t: number = 0;
let n: number = 0;

rl.on('line', (input: string) => {
data = input.split(' ');
t = parseInt(data[0]);
n = parseInt(data[1]);
medianSort(t,n)

}).on('close',function(){
process.exit(0);
});
66 changes: 66 additions & 0 deletions solutions/typescript/moons_and_umbrellas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
declare var require: any;
declare var process: any;

const readline = require("readline");
let rl = readline.createInterface(process.stdin, process.stdout);

// Define an interface for the data needed for each test case.
interface TestData {
testNumber: number;
x: number;
y: number;
s: string;
}

function parseInput(input: string[]) {
let line: number = 0;
// Read the number of test cases from the first line.
const numCases = Number(input[line++]);

const testData: TestData[] = [];
for (let testNumber = 1; testNumber <= numCases; ++testNumber) {
const x = Number(input[line].split(" ")[0]);
const y = Number(input[line].split(" ")[1]);
const s = String(input[line].split(" ")[2]);
line++;
testData.push({ testNumber, x, y, s });
}
return testData;
}

function runTestCase(data: TestData) {
//with dynamic programming
let cCost: number = data.s[0] === "J" ? Number.POSITIVE_INFINITY : 0;
let jCost: number = data.s[0] === "C" ? Number.POSITIVE_INFINITY : 0;

for (let i: number = 1; i < data.s.length; i++) {
let cCostNew: number = Number.POSITIVE_INFINITY;
let jCostNew: number = Number.POSITIVE_INFINITY;

if (data.s[i] === "C") {
cCostNew = Math.min(cCost, jCost + data.y);
} else if (data.s[i] === "J") {
jCostNew = Math.min(jCost, cCost + data.x);
} else {
cCostNew = Math.min(cCost, jCost + data.y);
jCostNew = Math.min(jCost, cCost + data.x);
}
cCost = cCostNew;
jCost = jCostNew;
}

// Print the result
console.log(`Case #${data.testNumber}: ${Math.min(cCost, jCost)}`);
}

function runAllTests(input: string[]) {
// Parse the input into TestData objects.
const testCases = parseInput(input);

// Run each test case.
testCases.forEach(runTestCase);
}

// Read the input data and run all test cases.
const lines: string[] = [];
rl.on("line", (line) => lines.push(line)).on("close", () => runAllTests(lines));
96 changes: 96 additions & 0 deletions solutions/typescript/parenting_partnering_returns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
declare var require: any;
declare var process: any;

const readline = require("readline");
let rl = readline.createInterface(process.stdin, process.stdout);

// Define an interface for the data needed for each test case.
interface TestData {
testNumber: number;
nActivities: number;
activities: string[];
}

function parseInput(input: string[]) {
let line: number = 0;
// Read the number of test cases from the first line.
const numCases = Number(input[line++]);

const testData: TestData[] = [];
for (let testNumber = 1; testNumber <= numCases; ++testNumber) {
let activities: string[] = [];
const nActivities = Number(input[line++].split(" ")[0]);
for (let i = 1; i <= nActivities; i++) {
activities.push(input[line++]);
}
testData.push({ testNumber, nActivities, activities });
}
return testData;
}

function runTestCase(data: TestData) {
//sort the activity list by start time
let list: number[][] = data.activities
.map((x) =>
String(x)
.split(" ")
.map((x) => Number(x))
)
.sort(sortByStart);
//declare the lists where we will save the last activity for each kid
let cameron: number[] = [-1, -1];
let jamie: number[] = [-1, -1];

//loop through the activities list to assign them to the kids
for (let i: number = 0; i < list.length; i++) {
//if the activity overlaps with the last activity from cameron
//give it to jamie
if (!isOverlap(cameron, list[i])) {
cameron = list[i];
//replace the activity in the unsorted array with "C"
data.activities[data.activities.indexOf(list[i].join(" "))] = "C";
} else if (!isOverlap(jamie, list[i])) {
jamie = list[i];
//replace the activity in the unsorted array with "J"
data.activities[data.activities.indexOf(list[i].join(" "))] = "J";
} else {
console.log(`Case #${data.testNumber}: IMPOSSIBLE`);
return;
}
}

// Print the result
console.log(`Case #${data.testNumber}: ${data.activities.join("")} `);
}

//function to check if two activities overlap with eachother
function isOverlap(a1: number[], a2: number[]) {
if (a1[0] === -1) {
return false;
}
//if the start or end of an activity is in between another activity
if (a2[0] < a1[1]) {
return true;
}
return false;
}

function sortByStart(a: number[], b: number[]) {
if (a[0] === b[0]) {
return 0;
} else {
return a[0] < b[0] ? -1 : 1;
}
}

function runAllTests(input: string[]) {
// Parse the input into TestData objects.
const testCases = parseInput(input);

// Run each test case.
testCases.forEach(runTestCase);
}

// Read the input data and run all test cases.
const lines: string[] = [];
rl.on("line", (line) => lines.push(line)).on("close", () => runAllTests(lines));
85 changes: 85 additions & 0 deletions solutions/typescript/reversort_engineering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
declare var require: any;
declare var process: any;

const readline = require("readline");
let rl = readline.createInterface(process.stdin, process.stdout);

// Define an interface for the data needed for each test case.
interface TestData {
testNumber: number;
N: number;
C: number;
}

function parseInput(input: string[]) {
let line: number = 0;
// Read the number of test cases from the first line.
const numCases = Number(input[line++]);

const testData: TestData[] = [];
for (let testNumber = 1; testNumber <= numCases; ++testNumber) {
const N = Number(input[line].split(" ")[0]);
const C = Number(input[line++].split(" ")[1]);
testData.push({ testNumber, N, C });
}
return testData;
}

function runTestCase(data: TestData) {
let maxCost: number = -1 + (data.N * (data.N + 1)) / 2; //max cost of entire algorithm
let minCost: number = data.N - 1; //min cost of entire algorithm
let iterations: number = data.N - 2; //how many iterations does it have left
let min: number = 1; //min cost of the current operation/round
let max: number = data.N; //max cost of the current operation/round
let array: number[] = []; //base array of positions
let sortedArray: number[] = []; //array from 1 to N
let subArray: number[] = []; //Array to be reversed
let x: number = max; //must be between min and max
let result: number[] = [];

//Checks if it's possible in the first place
if (data.C < minCost || data.C > maxCost) {
process.stdout.write(`Case #${data.testNumber}: IMPOSSIBLE\n`);
return;
}

//Creates 2 arrays; one array that'll be our base array of positions and the other one will be for the numbers themselves
for (let i: number = 0; i < data.N; ++i) {
sortedArray[i] = i + 1;
array[i] = i;
}
/*The trick is to subtract the cost of each operation from the goal cost C,
each operation's cost must be between the min 1, and the max n (min is always 1 while the max changes for each iteration -1)*/
for (let i: number = 0; i <= data.N - 2; ++i) {
if (data.C - x >= 0 && data.C - x >= iterations) {
subArray = array.slice(i, i + x);
subArray.reverse();
array = [...array.slice(0, i), ...subArray, ...array.slice(i + x, array.length)];
data.C = data.C - x;
max += -1;
iterations += -1;
x = max;
if (data.C == 0) break;
} else {
x += -1;
i += -1;
continue;
}
}
for (let i: number = 0; i < array.length; ++i) {
result[array[i]] = sortedArray[i];
}
process.stdout.write(`Case #${data.testNumber}: ${result.join(" ")}\n`);
}

function runAllTests(input: string[]) {
// Parse the input into TestData objects.
const testCases = parseInput(input);

// Run each test case.
testCases.forEach(runTestCase);
}

// Read the input data and run all test cases.
const lines: string[] = [];
rl.on("line", (line) => lines.push(line)).on("close", () => runAllTests(lines));