Skip to content

Commit 888672a

Browse files
authored
chore: adds additional lesson_07 group assignments (code-differently#325)
Signed-off-by: Anthony D. Mays <[email protected]>
1 parent 840208d commit 888672a

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as jest from "@jest/globals";
2+
import { countCharacter, getLetterGrade, sumEvenNumbers } from "./part_f.js";
3+
4+
const describe =
5+
!process.env.HW_VERSION || process.env.HW_VERSION === "F"
6+
? jest.describe
7+
: jest.describe.skip;
8+
9+
describe("getLetterGrade", () => {
10+
it("should return 'A' for scores 90-100", () => {
11+
expect(getLetterGrade(95)).toBe("A");
12+
expect(getLetterGrade(90)).toBe("A");
13+
expect(getLetterGrade(100)).toBe("A");
14+
});
15+
16+
it("should return 'B' for scores 80-89", () => {
17+
expect(getLetterGrade(85)).toBe("B");
18+
expect(getLetterGrade(80)).toBe("B");
19+
expect(getLetterGrade(89)).toBe("B");
20+
});
21+
22+
it("should return 'C' for scores 70-79", () => {
23+
expect(getLetterGrade(75)).toBe("C");
24+
expect(getLetterGrade(70)).toBe("C");
25+
expect(getLetterGrade(79)).toBe("C");
26+
});
27+
28+
it("should return 'D' for scores 60-69", () => {
29+
expect(getLetterGrade(65)).toBe("D");
30+
expect(getLetterGrade(60)).toBe("D");
31+
expect(getLetterGrade(69)).toBe("D");
32+
});
33+
34+
it("should return 'F' for scores below 60", () => {
35+
expect(getLetterGrade(59)).toBe("F");
36+
expect(getLetterGrade(0)).toBe("F");
37+
expect(getLetterGrade(45)).toBe("F");
38+
});
39+
});
40+
41+
describe("sumEvenNumbers", () => {
42+
it("should return 0 for an empty array", () => {
43+
expect(sumEvenNumbers([])).toBe(0);
44+
});
45+
46+
it("should return the sum of even numbers", () => {
47+
expect(sumEvenNumbers([1, 2, 3, 4, 5, 6])).toBe(12); // 2 + 4 + 6
48+
expect(sumEvenNumbers([2, 4, 6, 8])).toBe(20);
49+
});
50+
51+
it("should return 0 if no even numbers exist", () => {
52+
expect(sumEvenNumbers([1, 3, 5, 7])).toBe(0);
53+
});
54+
55+
it("should handle negative even numbers", () => {
56+
expect(sumEvenNumbers([-2, -1, 0, 1, 2])).toBe(0); // -2 + 0 + 2
57+
});
58+
});
59+
60+
describe("countCharacter", () => {
61+
it("should count occurrences of a character", () => {
62+
expect(countCharacter("hello", "l")).toBe(2);
63+
expect(countCharacter("hello", "o")).toBe(1);
64+
expect(countCharacter("hello", "h")).toBe(1);
65+
});
66+
67+
it("should return 0 if character is not found", () => {
68+
expect(countCharacter("hello", "x")).toBe(0);
69+
});
70+
71+
it("should handle empty string", () => {
72+
expect(countCharacter("", "a")).toBe(0);
73+
});
74+
75+
it("should be case sensitive", () => {
76+
expect(countCharacter("Hello", "h")).toBe(0);
77+
expect(countCharacter("Hello", "H")).toBe(1);
78+
});
79+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Determines the grade letter based on a numeric score.
3+
* A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: below 60
4+
*
5+
* @param score
6+
* @returns
7+
*/
8+
export function getLetterGrade(score: number): string {
9+
return "";
10+
}
11+
12+
/**
13+
* Calculates the sum of all even numbers in the given array.
14+
*
15+
* @param numbers
16+
* @returns
17+
*/
18+
export function sumEvenNumbers(numbers: number[]): number {
19+
return 0;
20+
}
21+
22+
/**
23+
* Counts how many times a specific character appears in a string.
24+
*
25+
* @param text
26+
* @param character
27+
* @returns
28+
*/
29+
export function countCharacter(text: string, character: string): number {
30+
return 0;
31+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as jest from "@jest/globals";
2+
import { daysUntilBirthday, findLargestNumber, isPalindrome } from "./part_g.js";
3+
4+
const describe =
5+
!process.env.HW_VERSION || process.env.HW_VERSION === "G"
6+
? jest.describe
7+
: jest.describe.skip;
8+
9+
describe("findLargestNumber", () => {
10+
it("should find the largest number in an array", () => {
11+
expect(findLargestNumber([1, 5, 3, 9, 2])).toBe(9);
12+
expect(findLargestNumber([10, 20, 5, 30])).toBe(30);
13+
});
14+
15+
it("should handle arrays with one element", () => {
16+
expect(findLargestNumber([42])).toBe(42);
17+
});
18+
19+
it("should handle negative numbers", () => {
20+
expect(findLargestNumber([-1, -5, -3])).toBe(-1);
21+
});
22+
23+
it("should handle arrays with duplicate largest values", () => {
24+
expect(findLargestNumber([5, 5, 5])).toBe(5);
25+
});
26+
});
27+
28+
describe("isPalindrome", () => {
29+
it("should return true for palindromes", () => {
30+
expect(isPalindrome("racecar")).toBe(true);
31+
expect(isPalindrome("A man a plan a canal Panama")).toBe(true);
32+
expect(isPalindrome("race a car")).toBe(false);
33+
});
34+
35+
it("should ignore case", () => {
36+
expect(isPalindrome("Racecar")).toBe(true);
37+
expect(isPalindrome("RaceCar")).toBe(true);
38+
});
39+
40+
it("should ignore spaces", () => {
41+
expect(isPalindrome("race car")).toBe(false);
42+
expect(isPalindrome("taco cat")).toBe(true);
43+
});
44+
45+
it("should handle empty string", () => {
46+
expect(isPalindrome("")).toBe(true);
47+
});
48+
49+
it("should handle single character", () => {
50+
expect(isPalindrome("a")).toBe(true);
51+
});
52+
});
53+
54+
describe("daysUntilBirthday", () => {
55+
it("should calculate days until birthday in same year", () => {
56+
// Birthday is later in the same year
57+
expect(daysUntilBirthday(3, 15, 6, 15)).toBe(92); // March 15 to June 15 (roughly)
58+
});
59+
60+
it("should calculate days until birthday next year", () => {
61+
// Birthday already passed this year
62+
expect(daysUntilBirthday(6, 15, 3, 15)).toBe(273); // June 15 to March 15 next year (roughly)
63+
});
64+
65+
it("should return 0 if today is the birthday", () => {
66+
expect(daysUntilBirthday(5, 20, 5, 20)).toBe(0);
67+
});
68+
69+
it("should handle birthdays tomorrow", () => {
70+
expect(daysUntilBirthday(5, 20, 5, 21)).toBe(1);
71+
});
72+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Finds the largest number in an array.
3+
*
4+
* @param numbers
5+
* @returns
6+
*/
7+
export function findLargestNumber(numbers: number[]): number {
8+
return 0;
9+
}
10+
11+
/**
12+
* Determines if a string is a palindrome (reads the same forwards and backwards).
13+
* Ignore case and spaces.
14+
*
15+
* @param text
16+
* @returns
17+
*/
18+
export function isPalindrome(text: string): boolean {
19+
return false;
20+
}
21+
22+
/**
23+
* Calculates the number of days until the next birthday.
24+
* Assume currentMonth and currentDay represent today's date,
25+
* and birthMonth and birthDay represent the birthday.
26+
*
27+
* @param currentMonth (1-12)
28+
* @param currentDay (1-31)
29+
* @param birthMonth (1-12)
30+
* @param birthDay (1-31)
31+
* @returns
32+
*/
33+
export function daysUntilBirthday(
34+
currentMonth: number,
35+
currentDay: number,
36+
birthMonth: number,
37+
birthDay: number
38+
): number {
39+
return 0;
40+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import * as jest from "@jest/globals";
2+
import { convertTemperature, findGCD, generateMultiplicationTable } from "./part_h.js";
3+
4+
const describe =
5+
!process.env.HW_VERSION || process.env.HW_VERSION === "H"
6+
? jest.describe
7+
: jest.describe.skip;
8+
9+
describe("generateMultiplicationTable", () => {
10+
it("should generate multiplication table for a given number", () => {
11+
const result = generateMultiplicationTable(3);
12+
expect(result).toEqual([
13+
"3 x 1 = 3",
14+
"3 x 2 = 6",
15+
"3 x 3 = 9",
16+
"3 x 4 = 12",
17+
"3 x 5 = 15",
18+
"3 x 6 = 18",
19+
"3 x 7 = 21",
20+
"3 x 8 = 24",
21+
"3 x 9 = 27",
22+
"3 x 10 = 30"
23+
]);
24+
});
25+
26+
it("should handle multiplication table for 1", () => {
27+
const result = generateMultiplicationTable(1);
28+
expect(result[0]).toBe("1 x 1 = 1");
29+
expect(result[9]).toBe("1 x 10 = 10");
30+
expect(result.length).toBe(10);
31+
});
32+
});
33+
34+
describe("findGCD", () => {
35+
it("should find GCD of two positive numbers", () => {
36+
expect(findGCD(12, 8)).toBe(4);
37+
expect(findGCD(15, 25)).toBe(5);
38+
expect(findGCD(17, 13)).toBe(1);
39+
expect(findGCD(100, 75)).toBe(25);
40+
});
41+
42+
it("should handle when one number is zero", () => {
43+
expect(findGCD(5, 0)).toBe(5);
44+
expect(findGCD(0, 7)).toBe(7);
45+
});
46+
47+
it("should handle identical numbers", () => {
48+
expect(findGCD(7, 7)).toBe(7);
49+
expect(findGCD(20, 20)).toBe(20);
50+
});
51+
52+
it("should handle when one number divides the other", () => {
53+
expect(findGCD(20, 5)).toBe(5);
54+
expect(findGCD(6, 18)).toBe(6);
55+
});
56+
57+
it("should handle order independence", () => {
58+
expect(findGCD(8, 12)).toBe(4);
59+
expect(findGCD(12, 8)).toBe(4);
60+
});
61+
});
62+
63+
describe("convertTemperature", () => {
64+
it("should convert Celsius to Fahrenheit", () => {
65+
expect(convertTemperature(0, "C", "F")).toBe(32);
66+
expect(convertTemperature(100, "C", "F")).toBe(212);
67+
expect(convertTemperature(25, "C", "F")).toBe(77);
68+
});
69+
70+
it("should convert Fahrenheit to Celsius", () => {
71+
expect(convertTemperature(32, "F", "C")).toBe(0);
72+
expect(convertTemperature(212, "F", "C")).toBe(100);
73+
});
74+
75+
it("should convert Celsius to Kelvin", () => {
76+
expect(convertTemperature(0, "C", "K")).toBe(273.15);
77+
expect(convertTemperature(25, "C", "K")).toBe(298.15);
78+
});
79+
80+
it("should convert Kelvin to Celsius", () => {
81+
expect(convertTemperature(273.15, "K", "C")).toBe(0);
82+
expect(convertTemperature(298.15, "K", "C")).toBe(25);
83+
});
84+
85+
it("should return same temperature for same scales", () => {
86+
expect(convertTemperature(25, "C", "C")).toBe(25);
87+
expect(convertTemperature(77, "F", "F")).toBe(77);
88+
});
89+
90+
it("should handle complex conversions", () => {
91+
// F to K: (77°F - 32) × 5/9 + 273.15 = 298.15K
92+
expect(convertTemperature(77, "F", "K")).toBeCloseTo(298.15, 2);
93+
// K to F: (298.15K - 273.15) × 9/5 + 32 = 77°F
94+
expect(convertTemperature(298.15, "K", "F")).toBeCloseTo(77, 2);
95+
});
96+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Generates a multiplication table for the given number up to 10.
3+
* Returns an array of strings in the format "number x 1 = result".
4+
*
5+
* @param number
6+
* @returns
7+
*/
8+
export function generateMultiplicationTable(number: number): string[] {
9+
return [];
10+
}
11+
12+
/**
13+
* Finds the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.
14+
*
15+
* @param a First number
16+
* @param b Second number
17+
* @returns The greatest common divisor of a and b
18+
*/
19+
export function findGCD(a: number, b: number): number {
20+
return 0;
21+
}
22+
23+
/**
24+
* Converts a temperature from one scale to another.
25+
* fromScale and toScale can be "C" (Celsius), "F" (Fahrenheit), or "K" (Kelvin).
26+
*
27+
* @param temperature
28+
* @param fromScale
29+
* @param toScale
30+
* @returns
31+
*/
32+
export function convertTemperature(
33+
temperature: number,
34+
fromScale: string,
35+
toScale: string
36+
): number {
37+
return 0;
38+
}

0 commit comments

Comments
 (0)