Skip to content

lesson07 #330

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

Closed
wants to merge 2 commits into from
Closed
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
63 changes: 39 additions & 24 deletions lesson_07/conditionals/src/lesson7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import { computeLexicographicDistance } from "./util.js";
* @return -1 if a is less than b, 1 if a is greater than b, and 0 otherwise.
*/
export function compareStrings(a: string, b: string): number {
// The distance will be a number less than 0 if string `a` is lexicographically less than `b`, 1
// if it is greater, and 0 if the strings are equal.
const distance = computeLexicographicDistance(a, b);

// TODO(you): Finish this method.

if (a < b) return -1;
if (a > b) return 1;

return 0;
}
Expand All @@ -24,19 +23,36 @@ export function compareStrings(a: string, b: string): number {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;

if (n < 0) {
console.error("Factorial is not defined for negative numbers.");
return -1;
}
if (n === 0 || n === 1) return 1;
return n * computeFactorial(n - 1);
}

console.log(computeFactorial(-2));



/**
* Returns an array of the first `n` Fibonacci numbers starting from 1.
*
* @param n The first `n` of Fibonacci values to compute.
* @return An array containing the first `n` Fibonacci values.
*/
export function getFirstNFibonacciNumbers(n: number): number[] {
return [];
if (n <= 0) return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation here

if (n === 1) return [1];

const fib: number[] = [1, 1];
for (let i = 2; i < n; i++) {
fib.push(fib[i - 1] + fib[i - 2]);
}
return fib;
}

/**
* Finds a value in an array of values.
*
Expand All @@ -46,24 +62,23 @@ export function getFirstNFibonacciNumbers(n: number): number[] {
* @param value The value to look for.
* @return The index of the value if found in the array and -1 otherwise.
*/
export function binarySearch(
values: number[],
start: number,
end: number,
value: number,
): number {
if (end < start) {
// The range is not valid so just return -1.
return -1;
}
export function binarySearch(values: number[], value: number): number {
let start = 0;
let end = values.length - 1;

const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
while (start <= end) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the iterative way to do binary search. Good work doing your research.

let mid = Math.floor((start + end) / 2);

// TODO(you): Finish implementing this algorithm
if (values[mid] === value) {
return mid;
} else if (values[mid] > value) {
end = mid - 1;
} else {
start = mid + 1;
}
}

// If values[pivotIndex] is equal to value then return `pivotIndex`.
// Else if values[pivotIndex] is greater than the value, then
// call `binarySearch(values, start, pivotIndex - 1, value)` and return its value;
// Else call `binarySearch(values, pivotIndex + 1, end, value)` and return its value.
return -1;
return -1;
}


22 changes: 19 additions & 3 deletions lesson_07/conditionals/src/part_a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@
* @return True if the age corresponds to a voting age and false otherwise.
*/
export function canVote(age: number): boolean {
return false;
if (age >= 18) {
return true;
}
else {
return false;
}
}



/**
* Adds all of the provided values and returns the sum.
*
* @param values The values to sum.
* @return The sum of all the values.
*/
export function addNumbers(values: number[]): number {
return 0;
let sum= 0;
for (let i = 0; i < values.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation here and everywhere else.

sum += values[i];
}
return sum;
}

/**
Expand All @@ -25,5 +36,10 @@ export function addNumbers(values: number[]): number {
* @return The factorial of n.
*/
export function computeFactorial(n: number): number {
return 0;

let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}
Loading