This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write a Python program to calculate number of days between two dates. | |
| # (2014, 7, 2), (2014, 7, 11) | |
| import datetime as date | |
| def num_of_days(date_one, date_two): | |
| try: | |
| (year, month, day) = date_one | |
| new_date = date.datetime(year, month, day).timestamp() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function range(start, end, step = 1) { | |
| const rangeOfNums = [] | |
| if (step < 0) { | |
| for (let i = start; i >= end; i -= Math.abs(step)) { | |
| rangeOfNums.push(i); | |
| }; | |
| } else { | |
| for (let i = start; i <= end; i += step) { | |
| rangeOfNums.push(i); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // reverse an array that modifies the array | |
| function reverseArrayInPlace(arr) { | |
| let length = arr.length; | |
| for (let i = length - 1; i >= 0; i--) { | |
| arr.push(arr[i]); | |
| } | |
| arr.splice(0, length); | |
| return arr; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // convert from array to list with | |
| function arrayToList(arr) { | |
| // list format: value 1, rest => value 2, rest => value 3, rest => null | |
| let list; | |
| for (let i = arr.length; i >= 0; i--) { | |
| list = { | |
| value: arr[i], | |
| rest: i >= arr.length - 1 ? null : list, | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # TODO: python delete file and folders | |
| import os | |
| # create file | |
| html = open('demo.html', 'x') | |
| html.close() | |
| # write to new file | |
| html = open('demo.html', 'w') | |
| html.write('<html>\n<body>\n<p>\nHello World</p>\n</body>\n</html>') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // reversing a number | |
| let givenNum = 4552; | |
| let numInRev = 0; | |
| while (givenNum > 0) { | |
| const lastDigit = Math.floor(givenNum % 10); | |
| numInRev = numInRev * 10 + lastDigit; | |
| givenNum = Math.floor(givenNum / 10); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function reverseNumber(givenNum) { | |
| const numInArr = givenNum.toString().split(''); | |
| numInArr.reverse(); | |
| return +numInArr.join(''); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function checkForPalindrome(phrase) { | |
| const phraseInArr = phrase.split(''); | |
| const noWhiteSpace = phraseInArr.filter((phrase) => { | |
| return phrase !== ' '; // you can use regex to ignore characters | |
| }); | |
| const toUseLength = Math.floor(noWhiteSpace.length / 2); | |
| const firstSet = noWhiteSpace.slice(0, toUseLength).join('').toLowerCase(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // spread operators allows any iterable to be expanded | |
| // it can serve as a means of adding arrays (concatenation) | |
| const arr1 = [1, 3, 4] | |
| const arr2 = [10, 13, 5] | |
| const bothArr = [...arr1, ...arr2] | |
| // you can use this to update an array with | |
| // another array without leading to a multidimensional array | |
| let arr1 = [1, 3, 4] | |
| const arr2 = [10, 13, 5] |