Skip to content

Commit 8a12c69

Browse files
committed
feat/ fixed changes for lesson 7
1 parent 68305cb commit 8a12c69

File tree

2 files changed

+121
-10
lines changed

2 files changed

+121
-10
lines changed

lesson_07/conditionals/src/lesson7.ts

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { computeLexicographicDistance } from "./util.js";
77
* @return True if the age corresponds to a voting age and false otherwise.
88
*/
99
export function canVote(age: number): boolean {
10-
return false;
10+
if (age >= 18) {
11+
return true;
12+
} else {
13+
return false;
14+
}
15+
1116
}
1217

1318
/**
@@ -23,10 +28,12 @@ export function compareStrings(a: string, b: string): number {
2328
const distance = computeLexicographicDistance(a, b);
2429

2530
// TODO(you): Finish this method.
31+
if (distance < 0) {
32+
return -1;
33+
}
2634

27-
return 0;
35+
return distance;
2836
}
29-
3037
/**
3138
* Converts a GPA on the 4.0 scale to the corresponding letter grade using the college board
3239
* scale. See
@@ -37,17 +44,46 @@ export function compareStrings(a: string, b: string): number {
3744
* @return The letter grade ("A+", "A", "A-", "B+", etc.).
3845
*/
3946
export function convertGpaToLetterGrade(gpa: number): string {
40-
return "F";
47+
if (gpa === 4.0) {
48+
return "A";
49+
} else if (gpa > 4.0) {
50+
return "A";
51+
} else if (gpa <= 3.99 && gpa >= 3.7) {
52+
return "A-";
53+
} else if (gpa <= 3.69 && gpa >= 3.3) {
54+
return "B+";
55+
} else if (gpa <= 3.29 && gpa >= 3.0) {
56+
return "B";
57+
} else if (gpa <= 2.99 && gpa >= 2.7) {
58+
return "B-";
59+
} else if (gpa <= 2.69 && gpa >= 2.3) {
60+
return "C+";
61+
} else if (gpa <= 2.29 && gpa >= 2.0) {
62+
return "C";
63+
} else if (gpa <= 1.99 && gpa >= 1.7) {
64+
return "C-";
65+
} else if (gpa <= 1.69 && gpa >= 1.3) {
66+
return "D+";
67+
} else if (gpa <= 1.29 && gpa >= 1.0) {
68+
return "D";
69+
} else {
70+
return "F";
71+
}
4172
}
4273

43-
/**
44-
* Computes the factorial of the given value of `n`.
74+
/**km
75+
* computes the factorial of the given value of `n`.
4576
*
4677
* @param n The value for which to compute the factorial.
4778
* @return The factorial of n.
4879
*/
4980
export function computeFactorial(n: number): number {
50-
return 0;
81+
let product = 1;
82+
for (let i = 1; i <= n; i++) {
83+
product *= i;
84+
}
85+
86+
return product;
5187
}
5288

5389
/**
@@ -57,7 +93,12 @@ export function computeFactorial(n: number): number {
5793
* @return The sum of all the values.
5894
*/
5995
export function addNumbers(values: number[]): number {
60-
return 0;
96+
let sum = 0; //initialize variable
97+
98+
for (const value of values) {
99+
sum += value; //adds value to each
100+
}
101+
return sum;
61102
}
62103

63104
/**
@@ -67,7 +108,21 @@ export function addNumbers(values: number[]): number {
67108
* @return An array containing the first `n` Fibonacci values.
68109
*/
69110
export function getFirstNFibonacciNumbers(n: number): number[] {
70-
return [];
111+
let current = 1;
112+
let prev = 0;
113+
114+
const numbers = [];
115+
116+
for (let i = 1; i<=n; i++){
117+
numbers.push(current);
118+
const nextNum = current + prev; // 1
119+
prev = current; // 1
120+
current = nextNum
121+
}
122+
123+
124+
125+
return numbers;
71126
}
72127

73128
/**
@@ -86,12 +141,22 @@ export function binarySearch(
86141
value: number,
87142
): number {
88143
if (end < start) {
144+
145+
89146
// The range is not valid so just return -1.
90147
return -1;
91148
}
92149

93150
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
94-
151+
if (values[pivotIndex] === value) {
152+
return pivotIndex;
153+
}
154+
else if (values[pivotIndex] > value) {
155+
return binarySearch(values, start, pivotIndex - 1, value);
156+
}
157+
else {
158+
return binarySearch(values, pivotIndex + 1, end, value);
159+
}
95160
// TODO(you): Finish implementing this algorithm
96161

97162
// If values[pivotIndex] is equal to value then return `pivotIndex`.

lesson_08/algos/src/test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function printPermutations(value: string, answer: string): void {
2+
if (value.length === 0) {
3+
console.log(answer);
4+
return;
5+
}
6+
7+
for (let i = 0; i < value.length; i++) {
8+
const ch = value.charAt(i);
9+
const left = value.substring(0, i);
10+
const right = value.substring(i + 1);
11+
const rest = left + right;
12+
printPermutations(rest, answer + ch);
13+
}
14+
}
15+
16+
function printPermutations(value: string, answer: string): void{
17+
if (value.length ===0){
18+
console.log(answer);
19+
return;
20+
}
21+
for (let i = 0; i < value.length; i++) {
22+
const ch = value.charAt(i);
23+
24+
}
25+
26+
}
27+
}
28+
29+
30+
31+
32+
function reverseString(input: string): string {
33+
if (input.length === 0) {
34+
return input;
35+
}
36+
37+
const charArray = input.split('');
38+
39+
for (let i = 0; i < charArray.length / 2; i++) {
40+
// Compute the corresponding index from the back of the string.
41+
const j = charArray.length - i - 1;
42+
swapCharacters(charArray, i, j);
43+
}
44+
45+
return charArray.join('');
46+
}

0 commit comments

Comments
 (0)