diff --git a/algorithms/js/CheckIfWordEqualsSummationOfTwoWords.js b/algorithms/js/CheckIfWordEqualsSummationOfTwoWords.js new file mode 100644 index 00000000..5ff4fd28 --- /dev/null +++ b/algorithms/js/CheckIfWordEqualsSummationOfTwoWords.js @@ -0,0 +1,10 @@ +let firstWord = "acb", secondWord = "cba", targetWord = "cdb"; +console.log(findSum(firstWord), findSum(secondWord), findSum(targetWord)); +console.log((findSum(firstWord)+findSum(secondWord)) === findSum(targetWord) ? 'true' : 'false'); + + +function findSum(input) { + let sum = 0; + input.split('').map(x => sum+=(x.charCodeAt(0)-97)); + return sum; +} \ No newline at end of file diff --git a/algorithms/js/IncrementalMemoryLeak.js b/algorithms/js/IncrementalMemoryLeak.js new file mode 100644 index 00000000..655794bd --- /dev/null +++ b/algorithms/js/IncrementalMemoryLeak.js @@ -0,0 +1,13 @@ +let memory1 = 8, memory2 = 11, i; +let arr = [memory1, memory2]; +let maxIndex = arr[0] >= arr[1] ? 0 : 1; +let maxCount = Math.max(memory1, memory2); + +for(i = 1; i <= maxCount; i++) { + if (i > arr[maxIndex]) + break; + arr[maxIndex] = arr[maxIndex] - i; + maxIndex = arr[0] >= arr[1] ? 0 : 1; +} + +console.log(i, ...arr); \ No newline at end of file diff --git a/algorithms/js/MaximumNumberOfWordsYouCanType.js b/algorithms/js/MaximumNumberOfWordsYouCanType.js new file mode 100644 index 00000000..b87971e1 --- /dev/null +++ b/algorithms/js/MaximumNumberOfWordsYouCanType.js @@ -0,0 +1,13 @@ +const text = "code leet code leet code"; +const brokenChars = "lt"; + +let textCopy = text; +let outputArray = []; + +brokenChars.split('').forEach(x => { + outputArray = textCopy.split(' ').map(y => { + return y.indexOf(x) <= 0 ? y : '~'; + }) +}); + +console.log(outputArray.filter(x => x !== '~').join(' ')); \ No newline at end of file diff --git a/algorithms/js/MaximumValueAfterInsertion.js b/algorithms/js/MaximumValueAfterInsertion.js new file mode 100644 index 00000000..b6c657f5 --- /dev/null +++ b/algorithms/js/MaximumValueAfterInsertion.js @@ -0,0 +1,23 @@ +const n = "71324", x = 1; +let tempValue = (n-0); +let tempDigit = 0; + +let finalNumber = 0; +let flagInserted = 0; + +let moduloNumber = Math.pow(10, n.length-1); +while (moduloNumber > 0.1) { + tempDigit = Math.floor(tempValue / moduloNumber); + tempValue = tempValue % moduloNumber; + moduloNumber = moduloNumber / 10; + if (x > tempDigit && flagInserted === 0) { + flagInserted = 1; + finalNumber = (finalNumber*100)+(x*10)+tempDigit; + } + else { + finalNumber = (finalNumber*10)+tempDigit; + } +} +if(flagInserted === 0) + finalNumber = (finalNumber * 10) + x; +console.log(finalNumber); \ No newline at end of file diff --git a/algorithms/js/MinimumIntervalToIncludeEachQuery.js b/algorithms/js/MinimumIntervalToIncludeEachQuery.js new file mode 100644 index 00000000..bcc5a2b0 --- /dev/null +++ b/algorithms/js/MinimumIntervalToIncludeEachQuery.js @@ -0,0 +1,16 @@ +let intervals = [[1,4],[2,4],[3,6],[4,4]], queries = [2,3,4,5]; +let result = []; + +for(let i = 0; i < queries.length; i++) { + //queries[i] is the query where we need to find the minimum interval it could fit in. + let min = 9999999; + for (let j = 0; j < intervals.length; j++) { + if(intervals[j][0] <= queries[i] && queries[i] <= intervals[j][1]) { + let interval = intervals[j][1] - intervals[j][0] + 1; + min = min ? Math.min(min, interval) : interval; + console.log('For: ', queries[i], ' Found ', intervals[j] , ' with min: ', min); + } + } + result[i] = min != 9999999 ? min : -1; +} +console.log(result); \ No newline at end of file diff --git a/algorithms/js/SumOfDigitsOfStringAfterConvert.js b/algorithms/js/SumOfDigitsOfStringAfterConvert.js new file mode 100644 index 00000000..dd73cf2f --- /dev/null +++ b/algorithms/js/SumOfDigitsOfStringAfterConvert.js @@ -0,0 +1,20 @@ +let start = performance.now(); +let input = "zbax"; +let k = 2; + +var combinedSum = ""; +input.split('').forEach(x => (combinedSum += x.charCodeAt(0) - 96)); +console.log('combinedSum: ', combinedSum); +console.log(convertStringToSum(combinedSum, k)); + +function convertStringToSum(number, count) { + if (count <= 0) + return number; + else { + var sum = 0; + number.toString().split('').map(x => sum+=(x-0)); + return convertStringToSum(sum, count - 1); + } +} +let end = performance.now(); +console.log(`Execution time: ${end - start} ms`); \ No newline at end of file diff --git a/algorithms/python-practice/helloworld.py b/algorithms/python-practice/helloworld.py new file mode 100644 index 00000000..36881e75 --- /dev/null +++ b/algorithms/python-practice/helloworld.py @@ -0,0 +1,6 @@ +def describe_pet(animal_type, pet_name): + """Display information about a pet.""" + print(f"\nI have a {animal_type}.") + print(f"My {animal_type}'s name is {pet_name.title()}.") + +describe_pet(pet_name='harry', animal_type='hamster') \ No newline at end of file