Skip to content

Commit 32c8c6f

Browse files
author
Meiko-S22
committed
feat: adds Meiko's lesson 07
1 parent 9bb736a commit 32c8c6f

File tree

5 files changed

+83
-19
lines changed

5 files changed

+83
-19
lines changed

lesson_07/conditionals/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
HW_VERSION=your homework version here
1+
HW_VERSION=C

lesson_07/conditionals/package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lesson_07/conditionals/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"eslint-config-prettier": "^9.1.0",
2727
"jest": "^29.7.0",
2828
"prettier": "3.4.2",
29-
"ts-jest": "^29.2.5",
29+
"ts-jest": "^29.2.6",
3030
"ts-node": "^10.9.2",
3131
"typescript": "^5.7.2",
3232
"typescript-eslint": "^8.18.0"

lesson_07/conditionals/src/lesson7.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { log } from "console";
12
import { computeLexicographicDistance } from "./util.js";
23

34
/**
@@ -13,28 +14,48 @@ export function compareStrings(a: string, b: string): number {
1314
const distance = computeLexicographicDistance(a, b);
1415

1516
// TODO(you): Finish this method.
16-
17-
return 0;
17+
if (distance < 0) {
18+
return -1;
19+
} else if (distance > 0) {
20+
return 1;
21+
}
22+
else {
23+
return 0;
24+
}
1825
}
1926

27+
2028
/**
2129
* Computes the factorial of the given value of `n`.
2230
*
2331
* @param n The value for which to compute the factorial.
2432
* @return The factorial of n.
2533
*/
2634
export function computeFactorial(n: number): number {
27-
return 0;
35+
if (n < 0) {
36+
return 0;
37+
}
38+
if (n === 0) {
39+
return 1;
40+
}
41+
return n * computeFactorial(n - 1);
2842
}
2943

44+
3045
/**
3146
* Returns an array of the first `n` Fibonacci numbers starting from 1.
3247
*
3348
* @param n The first `n` of Fibonacci values to compute.
3449
* @return An array containing the first `n` Fibonacci values.
3550
*/
3651
export function getFirstNFibonacciNumbers(n: number): number[] {
37-
return [];
52+
if(n <= 0) return [];
53+
if(n === 1) return[1];
54+
const fib: number[] = [1,1];
55+
for(let i = 2; i < n; i++){
56+
fib.push(fib[i - 1] + fib[i - 2]);
57+
}
58+
return fib;
3859
}
3960

4061
/**
@@ -58,12 +79,22 @@ export function binarySearch(
5879
}
5980

6081
const pivotIndex = Math.floor((start + end) / 2); // The index in the middle of the array.
61-
82+
const pivotValue = values[pivotIndex];
83+
if (pivotValue === value) {
84+
return pivotIndex;
85+
}
86+
if (pivotValue > value) {
87+
return binarySearch(values, start, pivotIndex - 1, value);
88+
}
89+
if (pivotValue < value){
90+
return binarySearch(values, pivotIndex + 1, end, value);
91+
}
92+
return -1;
93+
}
6294
// TODO(you): Finish implementing this algorithm
6395

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

lesson_07/conditionals/src/part_c.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
* @returns
77
*/
88
export function isStrongPassword(password: string): boolean {
9-
return false;
9+
10+
if (password.length < 8){
11+
return false;
1012
}
13+
const passUpperCase = /[A-Z]/.test(password);
14+
const passNumber=/\d/.test(password);
15+
return passUpperCase && passNumber;
16+
}
17+
1118

1219
/**
1320
* Determines the day of the week on the given 0-based number.
@@ -16,8 +23,24 @@ export function isStrongPassword(password: string): boolean {
1623
* @returns
1724
*/
1825
export function getDayOfWeek(day: number): string {
26+
if(day==0){
27+
return "Sunday";
28+
} else if(day==1){
29+
return "Monday";
30+
} else if(day==2){
31+
return "Tuesday";
32+
} else if(day==3){
33+
return "Wednesday"
34+
} else if(day==4){
35+
return "Thursday";
36+
}else if(day==5){
37+
return "Friday";
38+
} else if(day==6){
39+
return "Saturday";
40+
}else{
1941
return "";
2042
}
43+
}
2144

2245
/**
2346
* Determines the ticket price based on the given age. The price is
@@ -31,5 +54,15 @@ export function getDayOfWeek(day: number): string {
3154
* @returns
3255
*/
3356
export function getTicketPrice(age: number): number {
34-
return 0;
57+
if(age <= 5){
58+
return 0;
59+
}else if(age >= 5 && age <= 17){
60+
return 10;
61+
}else if(age >= 18 && age <= 59){
62+
return 20;
63+
}else{
64+
return 15;
65+
}
3566
}
67+
68+

0 commit comments

Comments
 (0)