Skip to content

Commit

Permalink
148th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 15, 2024
1 parent 9bd2be0 commit 4507db1
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 84 deletions.
10 changes: 0 additions & 10 deletions src/page-11/1154. Day of the Year/day-of-year.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/page-11/1154. Day of the Year/day-of-year.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/page-11/1154. Day of the Year/dayOfYear.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { dayOfYear, dayOfYear2 } from './dayOfYear';

describe('1154. Day of the Year', () => {
test('dayOfYear', () => {
expect(dayOfYear('2019-01-09')).toBe(9);
expect(dayOfYear('2019-02-10')).toBe(41);
});

test('dayOfYear2', () => {
expect(dayOfYear2('2019-01-09')).toBe(9);
expect(dayOfYear2('2019-02-10')).toBe(41);
});
});
37 changes: 37 additions & 0 deletions src/page-11/1154. Day of the Year/dayOfYear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
type DayOfYear = (date: string) => number;

/**
* Accepted
*/
export const dayOfYear: DayOfYear = (date) => {
const target = new Date(date);
const start = new Date(target.getFullYear(), 0, 0);
const diff = target.getTime() - start.getTime();
return Math.floor(diff / (24 * 60 * 60 * 1000));
};

/**
* Accepted
*/
export const dayOfYear2: DayOfYear = (date) => {
const [yearStr, monthStr, dayStr] = date.split('-');
const [year, month, day] = [Number(yearStr), Number(monthStr), Number(dayStr)];

// Array with days in each month for a non-leap year
const daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// Check if the year is a leap year
const isLeapYear = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;

// If it's a leap year, February has 29 days
if (isLeapYear) daysInMonth[1] = 29;

// Calculate the day of year
let dayOfYear = day;

for (let i = 0; i < month - 1; i++) {
dayOfYear += daysInMonth[i];
}

return dayOfYear;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { numPrimeArrangements } from './num-prime-arrangements';
import { numPrimeArrangements } from './numPrimeArrangements';

describe('1175. Prime Arrangements', () => {
test('numPrimeArrangements', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export const numPrimeArrangements = (n: number): number => {
type NumPrimeArrangements = (n: number) => number;

/**
* Accepted
*/
export const numPrimeArrangements: NumPrimeArrangements = (n) => {
const kMod = 1e9 + 7;

const isPrime = (val: number): boolean => {
Expand Down

This file was deleted.

21 changes: 0 additions & 21 deletions src/page-11/1176. Diet Plan Performance/diet-plan-performance.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/page-12/1185. Day of the Week/day-of-the-week.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/page-12/1185. Day of the Week/day-of-the-week.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/page-12/1185. Day of the Week/dayOfTheWeek.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { dayOfTheWeek, dayOfTheWeek2 } from './dayOfTheWeek';

describe('1185. Day of the Week', () => {
test('dayOfTheWeek', () => {
expect(dayOfTheWeek(31, 8, 2019)).toBe('Saturday');
expect(dayOfTheWeek(18, 7, 1999)).toBe('Sunday');
expect(dayOfTheWeek(15, 8, 1993)).toBe('Sunday');
});

test('dayOfTheWeek2', () => {
expect(dayOfTheWeek2(31, 8, 2019)).toBe('Saturday');
expect(dayOfTheWeek2(18, 7, 1999)).toBe('Sunday');
expect(dayOfTheWeek2(15, 8, 1993)).toBe('Sunday');
});
});
48 changes: 48 additions & 0 deletions src/page-12/1185. Day of the Week/dayOfTheWeek.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
type DayOfTheWeek = (day: number, month: number, year: number) => string;

/**
* Accepted
*/
export const dayOfTheWeek: DayOfTheWeek = (day, month, year) => {
const weekList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return weekList[new Date(`${year}-${month}-${day}`).getDay()];
};

/**
* Accepted
*/
export const dayOfTheWeek2: DayOfTheWeek = (day, month, year) => {
const weekList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

// Array with days in each month for a non-leap year
const daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

// Check if the year is a leap year
function isLeapYear(year: number): boolean {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}

// Number of days from reference day (Jan 1, 1971 which was a Friday)
let daysPassed = 0;

// Calculate days passed from 1971 to the start of given year
for (let y = 1971; y < year; y++) {
daysPassed += isLeapYear(y) ? 366 : 365;
}

// Calculate days passed in the current year up to the given month
for (let m = 0; m < month - 1; m++) {
daysPassed += daysInMonths[m];
}

// Add days in the current month
daysPassed += day - 1;

// Adjust for leap year if current year is leap and after February
if (month > 2 && isLeapYear(year)) daysPassed += 1;

// Calculate day of the week using reference day and days passed
const dayIndex = (daysPassed + 5) % 7; // January 1, 1971 was a Friday, so 5 is offset
if (dayIndex < 0) return weekList[dayIndex + 7];
return weekList[dayIndex];
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkStraightLine } from './check-straight-line';
import { checkStraightLine } from './checkStraightLine';

describe('1232. Check If It Is a Straight Line', () => {
test('checkStraightLine', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type CheckStraightLine = (coordinates: number[][]) => boolean;

/**
* Accepted
*/
export const checkStraightLine: CheckStraightLine = (coordinates) => {
// If there are less than 2 points, cannot form a line
if (coordinates.length < 2) return false;

// Extract first two points
const [x1, y1] = coordinates[0];
const [x2, y2] = coordinates[1];

// Check if all points lie on the same line
for (let i = 2; i < coordinates.length; i++) {
const [x, y] = coordinates[i];

// Calculate slope of the line formed by (x1, y1) and (x2, y2)
// slope = (y2 - y1) / (x2 - x1)
// Avoid division by zero by checking x2 - x1
if ((y2 - y1) * (x - x1) !== (y - y1) * (x2 - x1)) return false;
}

// All points are on the same line
return true;
};

0 comments on commit 4507db1

Please sign in to comment.