Skip to content

Commit aa214a5

Browse files
committed
sprint 1 tasks completed
1 parent e718fb4 commit aa214a5

9 files changed

Lines changed: 187 additions & 87 deletions

File tree

Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,34 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity: O(N) 2 for loops for loops are 0
13+
* Space Complexity: O(1) 2 variables
14+
* Optimal Time Complexity: O(N)
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+
// let sum = 0;
21+
// for (const num of numbers) {
22+
// sum += num;
23+
// }
2424

25-
let product = 1;
26-
for (const num of numbers) {
27-
product *= num;
28-
}
25+
// let product = 1;
26+
// for (const num of numbers) {
27+
// product *= num;
28+
// }
2929

30-
return {
31-
sum: sum,
32-
product: product,
33-
};
30+
// product and sum can be combined to one loop
31+
let sum = 0;
32+
let product = 1;
33+
34+
for (const num of numbers) {
35+
sum += num;
36+
product *= num;
37+
}
38+
return {
39+
sum: sum,
40+
product: product,
41+
};
3442
}
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
/**
22
* Finds common items between two arrays.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: Logarythmic? No Quadratic
5+
*
6+
// nested so includes loops over each item in first arr
7+
// nested loops are not efficient and used filter and inlcudes
8+
//array.inlcudes method goes item by item
9+
* Space Complexity: O(N)
10+
* Optimal Time Complexity: o(N)?
711
*
812
* @param {Array} firstArray - First array to compare
913
* @param {Array} secondArray - Second array to compare
1014
* @returns {Array} Array containing unique common items
1115
*/
12-
export const findCommonItems = (firstArray, secondArray) => [
13-
...new Set(firstArray.filter((item) => secondArray.includes(item))),
14-
];
16+
export const findCommonItems = (firstArray, secondArray) => {
17+
// ...new Set(firstArray.filter((item) => secondArray.includes(item))),
18+
const dictToCheck = {};
19+
const doubled = [];
20+
21+
for (const item of firstArray) {
22+
dictToCheck[item] = true;
23+
}
24+
25+
for (const item of secondArray) {
26+
if (dictToCheck[item]) {
27+
doubled.push(item);
28+
dictToCheck[item] = false;
29+
}
30+
}
31+
return doubled;
32+
};
Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
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:Quadratic time
5+
* nested loop so inefficient comparing of each i j to target
6+
* Space Complexity: O(N)
7+
* Optimal Time Complexity: O(N)
78
*
89
* @param {Array<number>} numbers - Array of numbers to search through
910
* @param {number} target - Target sum to find
1011
* @returns {boolean} True if pair exists, false otherwise
1112
*/
12-
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-
}
18-
}
19-
}
20-
return false;
13+
// export function hasPairWithSum(numbers, target) {
14+
// we are now making a cake from cake slices
15+
// numbers are now slices of cake and target is a a number of slices taht makes a whole cake
16+
17+
export function hasPairWithSum(slices, targetWholeCake) {
18+
// for (let i = 0; i < numbers.length; i++) {
19+
// for (let j = i + 1; j < numbers.length; j++) {
20+
// if (numbers[i] + numbers[j] === target) {
21+
// return true;
22+
// }
23+
// }
24+
// }
25+
const inventoryOfSlices = {};
26+
27+
for (const slice of slices) {
28+
const missingPieces = targetWholeCake - slice;
29+
30+
if (inventoryOfSlices[missingPieces]) {
31+
return true;
32+
}
33+
34+
inventoryOfSlices[slice] = true; // add slice
35+
}
36+
return false;
2137
}

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.
Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
/**
22
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: Quadratic
5+
* nested loop
6+
* Space Complexity: O(N)
7+
* Optimal Time Complexity:O(N)
78
*
89
* @param {Array} inputSequence - Sequence to remove duplicates from
910
* @returns {Array} New sequence with duplicates removed
1011
*/
1112
export function removeDuplicates(inputSequence) {
12-
const uniqueItems = [];
13+
const uniqueItems = [];
1314

14-
for (
15-
let currentIndex = 0;
16-
currentIndex < inputSequence.length;
17-
currentIndex++
18-
) {
19-
let isDuplicate = false;
20-
for (
21-
let compareIndex = 0;
22-
compareIndex < uniqueItems.length;
23-
compareIndex++
24-
) {
25-
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
26-
isDuplicate = true;
27-
break;
28-
}
29-
}
30-
if (!isDuplicate) {
31-
uniqueItems.push(inputSequence[currentIndex]);
32-
}
33-
}
15+
// for (
16+
// let currentIndex = 0;
17+
// currentIndex < inputSequence.length;
18+
// currentIndex++
19+
// ) {
20+
// let isDuplicate = false;
21+
// for (
22+
// let compareIndex = 0;
23+
// compareIndex < uniqueItems.length;
24+
// compareIndex++
25+
// ) {
26+
// if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
27+
// isDuplicate = true;
28+
// break;
29+
// }
30+
// }
31+
// if (!isDuplicate) {
32+
// uniqueItems.push(inputSequence[currentIndex]);
33+
// }
34+
// }
35+
const itemsWeChecked = {};
36+
for (const item of inputSequence) {
37+
if (!itemsWeChecked[item]) {
38+
uniqueItems.push(item);
39+
itemsWeChecked[item] = true;
40+
}
41+
}
3442

35-
return uniqueItems;
43+
return uniqueItems;
3644
}

Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,25 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
1212
"sum": 10, // 2 + 3 + 5
1313
"product": 30 // 2 * 3 * 5
1414
}
15-
Time Complexity:
16-
Space Complexity:
17-
Optimal time complexity:
15+
Time Complexity: O(N)
16+
Space Complexity: O(1)
17+
Optimal time complexity:O(N)
1818
"""
1919
# Edge case: empty list
2020
if not input_numbers:
2121
return {"sum": 0, "product": 1}
2222

23-
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
23+
# sum = 0
24+
# for current_number in input_numbers:
25+
# sum += current_number
26+
27+
# product = 1
28+
# for current_number in input_numbers:
29+
# product *= current_number
2630

31+
sum = 0
2732
product = 1
2833
for current_number in input_numbers:
34+
sum += current_number
2935
product *= current_number
30-
3136
return {"sum": sum, "product": product}

Sprint-1/Python/find_common_items/find_common_items.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@ def find_common_items(
99
"""
1010
Find common items between two arrays.
1111
12-
Time Complexity:
13-
Space Complexity:
14-
Optimal time complexity:
12+
Time Complexity: Quadratic
13+
Space Complexity: ON)
14+
Optimal time complexity: O(N)
1515
"""
1616
common_items: List[ItemType] = []
17-
for i in first_sequence:
18-
for j in second_sequence:
19-
if i == j and i not in common_items:
20-
common_items.append(i)
17+
# for i in first_sequence:
18+
# for j in second_sequence:
19+
# if i == j and i not in common_items:
20+
# common_items.append(i)
21+
# return common_items
22+
23+
dictToCheck = {}
24+
25+
for item in first_sequence:
26+
dictToCheck[item] = True
27+
28+
for item in second_sequence:
29+
if item in dictToCheck:
30+
common_items.append(item)
31+
dictToCheck.pop(item)
32+
2133
return common_items
34+

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
77
"""
88
Find if there is a pair of numbers that sum to a target value.
99
10-
Time Complexity:
11-
Space Complexity:
12-
Optimal time complexity:
10+
Time Complexity:Quadratic
11+
Space Complexity:O(N)
12+
Optimal time complexity:O(N)
1313
"""
1414
for i in range(len(numbers)):
1515
for j in range(i + 1, len(numbers)):
1616
if numbers[i] + numbers[j] == target_sum:
1717
return True
1818
return False
19+
# // i am using my cake exqmple from js
20+
inventory_of_slices = {}
21+
22+
for slice in numbers:
23+
missing_piece = target_sum - slice
24+
if missing_piece in inventory_of_slices:
25+
return True
26+
27+
inventory_of_slices[slice] = True
28+
29+
return False

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
77
"""
88
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
99
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
10+
Time complexity:quadratic
11+
Space complexity:O(N)
12+
Optimal time complexity:O(N)
1313
"""
1414
unique_items = []
1515

16-
for value in values:
17-
is_duplicate = False
18-
for existing in unique_items:
19-
if value == existing:
20-
is_duplicate = True
21-
break
22-
if not is_duplicate:
23-
unique_items.append(value)
16+
# for value in values:
17+
# is_duplicate = False
18+
# for existing in unique_items:
19+
# if value == existing:
20+
# is_duplicate = True
21+
# break
22+
# if not is_duplicate:
23+
# unique_items.append(value)
2424

25-
return unique_items
25+
# return unique_items
26+
27+
28+
itemsWeChecked = {}
29+
for item in values:
30+
if item not in itemsWeChecked:
31+
unique_items.append(item)
32+
itemsWeChecked[item] = True
33+
34+
return unique_items

0 commit comments

Comments
 (0)