Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching 1 #54

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
28 changes: 28 additions & 0 deletions JayKay24/Searching 1/assignment-one.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type TargetSumPair = [number, number];

/**
* Returns indices of two numbers such that they add up to target
* @param nums - An array of integers
* @param target
*
* @returns Indices of two numbers in nums that add up to target
*/
function findIdxs(nums: number[], target: number): TargetSumPair {
const idxs: TargetSumPair = [-1, -1];
const seen: Map<number, number> = new Map();

for (let i = 0; i < nums.length; i++) {
const current = nums[i];

if (seen.has(current)) {
idxs[0] = seen.get(current);
idxs[1] = i;
break;
}

const result = target - current;
seen.set(result, i);
}

return idxs;
}
81 changes: 81 additions & 0 deletions JayKay24/Searching 1/assignment-two.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class Grade {
start: number;
to: number;
gradeName: string;

constructor(start: number, to: number, gradeName: string) {
this.start = start;
this.to = to;
this.gradeName = gradeName;
}
}

class Student {
name: string;
marks: number;

constructor(name: string, marks: number) {
this.name = name;
this.marks = marks;
}
}

/**
* Build and return lower and upper bounds of grades "A" to "B";
*
* @param grades - Grades to use to establish a valid range
* @returns Tuple containing lower and upper bounds of grade A to B
*/
function build_grade_bounds_A_to_B(grades: Grade[]): [number, number] {
let lower_bound = 0,
upper_bound = 0;

for (const grade of grades) {
if (grade.gradeName === "A") {
upper_bound = grade.to;
} else if (grade.gradeName === "B") {
lower_bound = grade.start;
}
}

return [lower_bound, upper_bound];
}

/**
* Return names of students who scored grades "A" and "B"
*
* @param grades - An array of grades each containing it's letter and it's lower and upper bound
* @param students - An array of students
* @returns Names of students who scored B and above
*/
function super_students(grades: Grade[], students: Student[]): string[] {
const passed_students: string[] = [];
const [lower_bound, upper_bound] = build_grade_bounds_A_to_B(grades);

for (const student of students) {
if (student.marks >= lower_bound && student.marks <= upper_bound) {
passed_students.push(student.name);
}
}

return passed_students;
}

const grades = [
new Grade(90, 100, "A"),
new Grade(80, 90, "B"),
new Grade(70, 79, "C"),
new Grade(60, 69, "D"),
new Grade(0, 59, "E"),
];

const students = [
new Student("Dennis", 44),
new Student("Ken", 90),
new Student("Derick", 32),
new Student("James", 67),
new Student("Joyce", 76),
new Student("Linet", 29),
new Student("Ben", 96),
new Student("Jane", 82),
];