Skip to content

Commit 177e3af

Browse files
committed
feat: optimize all 8 functions across JavaScript and Python
1 parent b1d794c commit 177e3af

32 files changed

Lines changed: 830 additions & 91 deletions

File tree

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ contact_links:
99
- name: CYF Slack
1010
url: codeyourfuture.slack.com
1111
about: Come to #cyf-curriculum and chat
12+
- name: CYF Tech Ed
13+
url: https://github.com/orgs/CodeYourFuture/teams/mentors
14+
about: CYF mentors on Github
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: PD Coursework
2+
description: Assign a piece of PD coursework
3+
title: "[PD] <title>"
4+
labels: [PD, 🏝 Priority Stretch, 🐇 Size Small, 📅 Sprint 1]
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
Thanks for taking the time to assign this coursework!
10+
- type: input
11+
attributes:
12+
label: Coursework content
13+
validations:
14+
required: true
15+
- type: input
16+
attributes:
17+
label: Estimated time in hours
18+
description: (PD has max 4 per week total)
19+
validations:
20+
required: true
21+
- type: textarea
22+
attributes:
23+
label: What is the purpose of this assignment?
24+
description: Clearly explain the purpose of this assignment and how trainees can evaluate this.
25+
validations:
26+
required: true
27+
- type: textarea
28+
attributes:
29+
label: How to submit
30+
description: State in clear steps how a trainee can submit this assignment.
31+
placeholder: |
32+
Copy the Google doc to your own Google Drive
33+
Complete the work assigned
34+
When you are ready, move your document to your class Drive
35+
validations:
36+
required: true
37+
- type: textarea
38+
attributes:
39+
label: Anything else?
40+
description: |
41+
Links? References? Anything that will give more context
42+
43+
Tip: You can attach images or log files by clicking this area to highlight it and then dragging files in.
44+
- type: markdown
45+
attributes:
46+
value: |
47+
**Thank you so much.**
48+
49+
Please now complete this ticket by filling in the options on the sidebar.
50+
51+
1. Update labels
52+
- priority -- is this coursework key, mandatory, or stretch?
53+
- size -- help trainees plan their time with rough estimation
54+
2. Add to project backlog
55+
- add to the project named the same as this repo
56+
- fill in custom fields -- priority, size, hours, week -- to match this issue
57+
58+
Once your ticket is complete, you may like to check it out on the example project board attached to this repo.
59+
This is so you understand how trainees will use your work.

.github/ISSUE_TEMPLATE/coursework.yml renamed to .github/ISSUE_TEMPLATE/tech-ed-assignment.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
name: Coursework
2-
description: Assign a piece of coursework
1+
name: Tech Ed Coursework
2+
description: Assign a piece of technical coursework
33
title: "<title>"
4-
labels: [Tech Ed, 🏕 Priority Mandatory, 🐂 Size Medium, 📅 Module 1]
4+
labels: [Tech Ed, 🏕 Priority Mandatory, 🐂 Size Medium, 📅 Sprint 1]
55
body:
66
- type: markdown
77
attributes:
88
value: |
9-
Thanks for taking the time to assign this coursework!
9+
Thanks for taking the time to assign this coursework!
1010
11-
To support our trainees with planning and prioritising their own learning journey, we want our coursework assignments to be more informative.
11+
To support our trainees with planning and prioritising their own learning journey, we want our coursework assignments to be more informative.
1212
We don't just want to tell them what to do, we want to tell them stuff like:
1313
- why we are doing it
1414
- what it's "for" (problem-solving, debugging, etc)
1515
- how long they should spend on it, maximum
16-
- how to get help
17-
- how to review it with answers
16+
- how to get help
17+
- how to review it with answers
1818
- how to get it reviewed from mentors and peers
1919
- type: input
2020
attributes:
@@ -26,7 +26,7 @@ body:
2626
label: Learning Objectives
2727
description: https://common.codeyourfuture.io/common-theme/shortcodes/objectives/
2828
placeholder: |
29-
<!--{{<objectives}}-->
29+
<!--{{<objectives}}-->
3030
- [ ] CYF format, task list formatting
3131
<!--{{</objectives}}-->
3232
- type: textarea
@@ -38,6 +38,7 @@ body:
3838
- type: input
3939
attributes:
4040
label: Maximum time in hours
41+
description: (Tech has max 16 per week total)
4142
validations:
4243
required: true
4344
- type: textarea
@@ -79,7 +80,7 @@ body:
7980
1. Update labels
8081
- priority -- is this coursework key, mandatory, or stretch? Pick one.
8182
- size -- help trainees plan their time with rough estimation. Pick one
82-
- topics -- add all that seem relevant to you.
83+
- topics -- add all that seem relevant to you.
8384
2. Add a Sprint label to add to the backlog view
8485
- sprint 1,2,3,4
8586

.github/pull_request_template.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,35 @@
22
33
You must title your PR like this:
44
5-
REGION | COHORT_NAME | FIRST_NAME LAST_NAME | PROJ_NAME
5+
Region | Cohort | FirstName LastName | Sprint | Assignment Title
66
77
For example,
88
9-
London | March-2025 | Carol Owen | Wireframe
9+
London | 25-ITP-May | Carol Owen | Sprint 1 | Alarm Clock
1010
11-
Complete the task list below this message.
12-
If your PR is rejected, check the task list.
11+
Fill in the template below - remove any sections that don't apply.
12+
13+
Complete the self checklist - replace each empty box in the checklist [ ] with a [x].
14+
15+
Add the label "Needs Review" and you will get review.
16+
17+
Respond to volunteer reviews until the volunteer marks it as "Complete".
1318
1419
-->
1520

1621
## Learners, PR Template
1722

1823
Self checklist
1924

20-
- [ ] I have committed my files one by one, on purpose, and for a reason
21-
- [ ] I have titled my PR with REGION | COHORT_NAME | FIRST_NAME LAST_NAME | PROJ_NAME
25+
- [ ] I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
26+
- [ ] My changes meet the requirements of the task
2227
- [ ] I have tested my changes
2328
- [ ] My changes follow the [style guide](https://curriculum.codeyourfuture.io/guides/reviewing/style-guide/)
24-
- [ ] My changes meet the [requirements](./README.md) of this task
29+
30+
## Changelist
31+
32+
Briefly explain your PR.
2533

2634
## Questions
2735

28-
Ask any questions you have for your reviewer. Delete this section if there are none.
36+
Ask any questions you have for your reviewer.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Validate PR Metadata
2+
on:
3+
pull_request_target:
4+
types:
5+
- labeled
6+
- unlabeled
7+
- opened
8+
- edited
9+
- reopened
10+
11+
jobs:
12+
validate_pr_metadata:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: CodeYourFuture/actions/validate-pr-metadata@main
17+
with:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,53 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity: O(n) - Single pass through the array
13+
* Space Complexity: O(1) - Only using constant extra space
14+
* Optimal Time Complexity: O(n) - Cannot do better than linear time
1515
*
1616
* @param {Array<number>} numbers - Numbers to process
1717
* @returns {Object} Object containing running total and product
1818
*/
1919
export function calculateSumAndProduct(numbers) {
20-
let sum = 0;
21-
for (const num of numbers) {
22-
sum += num;
23-
}
20+
// OPTIMIZED IMPLEMENTATION: Single pass algorithm
21+
// Previous implementation used two separate loops (2n operations)
22+
// This version combines both calculations in one loop (n operations)
23+
24+
let sum = 0; // O(1) space
25+
let product = 1; // O(1) space
2426

25-
let product = 1;
27+
// Single pass through array: O(n) time complexity
2628
for (const num of numbers) {
27-
product *= num;
29+
sum += num; // O(1) operation per element
30+
product *= num; // O(1) operation per element
2831
}
2932

30-
return {
31-
sum: sum,
32-
product: product,
33-
};
33+
// Return optimized object syntax: O(1) space
34+
return { sum, product };
3435
}
36+
37+
/*
38+
* ORIGINAL IMPLEMENTATION (for comparison):
39+
*
40+
* export function calculateSumAndProduct(numbers) {
41+
* let sum = 0;
42+
* for (const num of numbers) { // First pass: O(n)
43+
* sum += num;
44+
* }
45+
*
46+
* let product = 1;
47+
* for (const num of numbers) { // Second pass: O(n)
48+
* product *= num;
49+
* }
50+
*
51+
* return { // Total: O(2n) = O(n) time
52+
* sum: sum, // O(1) space
53+
* product: product,
54+
* };
55+
* }
56+
*
57+
* IMPROVEMENTS MADE:
58+
* 1. Reduced from 2n to n operations (50% fewer iterations)
59+
* 2. Better cache locality (single pass through memory)
60+
* 3. Same O(n) time complexity but with better constant factors
61+
*/
Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
/**
22
* Finds common items between two arrays.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: O(n + m) - Single pass through both arrays
5+
* Space Complexity: O(min(n, m)) - Set size bounded by smaller array
6+
* Optimal Time Complexity: O(n + m) - Cannot do better than linear time
77
*
88
* @param {Array} firstArray - First array to compare
99
* @param {Array} secondArray - Second array to compare
1010
* @returns {Array} Array containing unique common items
1111
*/
12-
export const findCommonItems = (firstArray, secondArray) => [
13-
...new Set(firstArray.filter((item) => secondArray.includes(item))),
14-
];
12+
export const findCommonItems = (firstArray, secondArray) => {
13+
// OPTIMIZED IMPLEMENTATION: O(n + m) time complexity
14+
// Previous implementation: O(n × m) due to nested includes() calls
15+
16+
// Convert second array to Set for O(1) lookup: O(m) time, O(m) space
17+
const secondSet = new Set(secondArray);
18+
19+
// Find common items using Set lookup: O(n) time
20+
const commonItems = firstArray.filter((item) => secondSet.has(item));
21+
22+
// Remove duplicates: O(n) time in worst case
23+
return [...new Set(commonItems)];
24+
};
25+
26+
/*
27+
* ORIGINAL IMPLEMENTATION (for comparison):
28+
*
29+
* export const findCommonItems = (firstArray, secondArray) => [
30+
* ...new Set(firstArray.filter((item) => secondArray.includes(item))),
31+
* ];
32+
*
33+
* COMPLEXITY ANALYSIS OF ORIGINAL:
34+
* - firstArray.filter(): O(n) iterations
35+
* - secondArray.includes(): O(m) for each iteration
36+
* - Total: O(n × m) time complexity
37+
* - Space: O(n) for Set creation
38+
*
39+
40+
* IMPROVEMENTS MADE:
41+
* 1. Reduced from O(n × m) to O(n + m) time complexity
42+
* 2. Set lookup is O(1) vs Array.includes() O(m)
43+
* 3. Significant performance gain for large arrays
44+
* 4. Same functionality with better algorithmic efficiency
45+
46+
*/
Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,59 @@
11
/**
22
* Find if there is a pair of numbers that sum to a given target value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: O(n) - Single pass through the array
5+
* Space Complexity: O(n) - Set to store seen numbers
6+
* Optimal Time Complexity: O(n) - Cannot do better than linear time
77
*
88
* @param {Array<number>} numbers - Array of numbers to search through
99
* @param {number} target - Target sum to find
1010
* @returns {boolean} True if pair exists, false otherwise
1111
*/
1212
export function hasPairWithSum(numbers, target) {
13-
for (let i = 0; i < numbers.length; i++) {
14-
for (let j = i + 1; j < numbers.length; j++) {
15-
if (numbers[i] + numbers[j] === target) {
16-
return true;
17-
}
13+
// OPTIMIZED IMPLEMENTATION: O(n) time complexity
14+
// Previous implementation: O(n²) due to nested loops
15+
16+
const seen = new Set(); // O(n)
17+
18+
// O(n) time complexity
19+
for (const num of numbers) {
20+
const complement = target - num;
21+
// O(1) lookup
22+
if (seen.has(complement)) {
23+
return true;
1824
}
25+
26+
// O(1) operation
27+
seen.add(num);
1928
}
2029
return false;
2130
}
31+
console.log(hasPairWithSum([3, 2, 3, 4, 5], 9));
32+
/*
33+
* ORIGINAL IMPLEMENTATION (for comparison):
34+
*
35+
* export function hasPairWithSum(numbers, target) {
36+
* for (let i = 0; i < numbers.length; i++) { // O(n) iterations
37+
* for (let j = i + 1; j < numbers.length; j++) { // O(n) iterations each
38+
* if (numbers[i] + numbers[j] === target) { // O(1) comparison
39+
* return true;
40+
* }
41+
* }
42+
* }
43+
* return false;
44+
* }
45+
*
46+
* COMPLEXITY ANALYSIS OF ORIGINAL:
47+
* - Outer loop: O(n) iterations
48+
* - Inner loop: O(n) iterations for each outer iteration
49+
* - Total: O(n²) time complexity
50+
* - Space: O(1) - only using loop variables
51+
*
52+
* PERFORMANCE ISSUES:
53+
* - Quadratic time complexity O(n²)
54+
*
55+
* IMPROVEMENTS MADE:
56+
* 1. Reduced from O(n²) to O(n) time complexity
57+
* 2. Single pass through array instead of nested loops
58+
* 3. Set lookup is O(1) vs nested iteration O(n)
59+
*/

Sprint-1/JavaScript/package-lock.json

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

0 commit comments

Comments
 (0)