Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
.venv
__pycache__
package-lock.json
Sprint-1/JavaScript/package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,34 @@
* "product": 30 // 2 * 3 * 5
* }
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: O(N) the previous two loops are swapped into one loop
* Space Complexity: O(1) 2 variables - sum and product
* Optimal Time Complexity: O(N)
*
* @param {Array<number>} numbers - Numbers to process
* @returns {Object} Object containing running total and product
*/
export function calculateSumAndProduct(numbers) {
let sum = 0;
for (const num of numbers) {
sum += num;
}
// let sum = 0;
// for (const num of numbers) {
// sum += num;
// }

let product = 1;
for (const num of numbers) {
product *= num;
}
// let product = 1;
// for (const num of numbers) {
// product *= num;
// }

return {
sum: sum,
product: product,
};
// product and sum can be combined to one loop
let sum = 0;
let product = 1;

for (const num of numbers) {
sum += num;
product *= num;
}
return {
sum: sum,
product: product,
};
}
30 changes: 24 additions & 6 deletions Sprint-1/JavaScript/findCommonItems/findCommonItems.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
/**
* Finds common items between two arrays.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: Logarythmic? No Quadratic
*
// nested so includes loops over each item in first arr
// nested loops are not efficient and used filter and inlcudes
//array.inlcudes method goes item by item
* Space Complexity: O(N)
* Optimal Time Complexity: o(N)?
*
* @param {Array} firstArray - First array to compare
* @param {Array} secondArray - Second array to compare
* @returns {Array} Array containing unique common items
*/
export const findCommonItems = (firstArray, secondArray) => [
...new Set(firstArray.filter((item) => secondArray.includes(item))),
];
export const findCommonItems = (firstArray, secondArray) => {
// ...new Set(firstArray.filter((item) => secondArray.includes(item))),
const dictToCheck = {};
const common = [];

for (const item of firstArray) {
dictToCheck[item] = true;
}

for (const item of secondArray) {
if (dictToCheck[item]) {
doubled.push(item);
Comment thread
katarzynakaz marked this conversation as resolved.
Outdated
dictToCheck[item] = false;
}
}
return common;
};
36 changes: 25 additions & 11 deletions Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
/**
* Find if there is a pair of numbers that sum to a given target value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity:Quadratic time
* nested loop so inefficient comparing of each i j to target
* Space Complexity: O(N)
* Optimal Time Complexity: O(N)
*
* @param {Array<number>} numbers - Array of numbers to search through
* @param {number} target - Target sum to find
* @returns {boolean} True if pair exists, false otherwise
*/

export function hasPairWithSum(numbers, target) {
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target) {
return true;
}
}
}
return false;
// for (let i = 0; i < numbers.length; i++) {
// for (let j = i + 1; j < numbers.length; j++) {
// if (numbers[i] + numbers[j] === target) {
// return true;
// }
// }
// }

const inventory = {};

for (const num of numbers) {
const needed = target - num;

if (inventory[needed]) {
return true;
}

inventory[num] = true;
}
return false;
}
58 changes: 33 additions & 25 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
/**
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
*
* Time Complexity:
* Space Complexity:
* Optimal Time Complexity:
* Time Complexity: Quadratic
* nested loop
* Space Complexity: O(N)
* Optimal Time Complexity:O(N)
*
* @param {Array} inputSequence - Sequence to remove duplicates from
* @returns {Array} New sequence with duplicates removed
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];
const uniqueItems = [];

for (
let currentIndex = 0;
currentIndex < inputSequence.length;
currentIndex++
) {
let isDuplicate = false;
for (
let compareIndex = 0;
compareIndex < uniqueItems.length;
compareIndex++
) {
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
uniqueItems.push(inputSequence[currentIndex]);
}
}
// for (
// let currentIndex = 0;
// currentIndex < inputSequence.length;
// currentIndex++
// ) {
// let isDuplicate = false;
// for (
// let compareIndex = 0;
// compareIndex < uniqueItems.length;
// compareIndex++
// ) {
// if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
// isDuplicate = true;
// break;
// }
// }
// if (!isDuplicate) {
// uniqueItems.push(inputSequence[currentIndex]);
// }
// }
const itemsWeChecked = {};
for (const item of inputSequence) {
if (!itemsWeChecked[item]) {
uniqueItems.push(item);
itemsWeChecked[item] = true;
}
}

return uniqueItems;
return uniqueItems;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
"sum": 10, // 2 + 3 + 5
"product": 30 // 2 * 3 * 5
}
Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: O(N)
Space Complexity: O(1)
Optimal time complexity:O(N)
"""
# Edge case: empty list
if not input_numbers:
return {"sum": 0, "product": 1}

sum = 0
for current_number in input_numbers:
sum += current_number
# sum = 0
# for current_number in input_numbers:
# sum += current_number

# product = 1
# for current_number in input_numbers:
# product *= current_number

sum = 0
product = 1
for current_number in input_numbers:
sum += current_number
product *= current_number

return {"sum": sum, "product": product}
27 changes: 20 additions & 7 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ def find_common_items(
"""
Find common items between two arrays.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: Quadratic
Space Complexity: ON)
Optimal time complexity: O(N)
"""
common_items: List[ItemType] = []
for i in first_sequence:
for j in second_sequence:
if i == j and i not in common_items:
common_items.append(i)
# for i in first_sequence:
# for j in second_sequence:
# if i == j and i not in common_items:
# common_items.append(i)
# return common_items

dict_to_check = {}

for item in first_sequence:
dict_to_check[item] = True

Comment thread
katarzynakaz marked this conversation as resolved.
for item in second_sequence:
if item in dict_to_check:
common_items.append(item)
dict_to_check.pop(item)

Comment thread
katarzynakaz marked this conversation as resolved.
return common_items

34 changes: 26 additions & 8 deletions Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
"""
Find if there is a pair of numbers that sum to a target value.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity:Quadratic
Space Complexity:O(N)
Optimal time complexity:O(N)
"""
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[i] + numbers[j] == target_sum:
return True
return False
# for i in range(len(numbers)):
# for j in range(i + 1, len(numbers)):
# if numbers[i] + numbers[j] == target_sum:
# return True
# return False
# using my cake exqmple from js
# inventory_of_slices = {}

# for slice in numbers:
# missing_piece = target_sum - slice
# if missing_piece in inventory_of_slices:
# return True

# inventory_of_slices[slice] = True
inventory = {}

for number in numbers:
missing = target_sum - number
if missing in inventory:
return True

inventory[number] = True
return False
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

End of line is missing

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, github highlights this error

33 changes: 21 additions & 12 deletions Sprint-1/Python/remove_duplicates/remove_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
"""
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.

Time complexity:
Space complexity:
Optimal time complexity:
Time complexity:quadratic
Space complexity:O(N)
Optimal time complexity:O(N)
"""
unique_items = []

for value in values:
is_duplicate = False
for existing in unique_items:
if value == existing:
is_duplicate = True
break
if not is_duplicate:
unique_items.append(value)
# for value in values:
# is_duplicate = False
# for existing in unique_items:
# if value == existing:
# is_duplicate = True
# break
# if not is_duplicate:
# unique_items.append(value)

return unique_items
# return unique_items


items_we_checked = {}
for item in values:
if item not in items_we_checked:
unique_items.append(item)
items_we_checked[item] = True

return unique_items