mirror of
https://gitlab.com/djdietrick/docs
synced 2026-05-02 23:20:53 -04:00
Added language specific interview prep sections
This commit is contained in:
46
docs/.vitepress/cache/deps/@theme_index.js
vendored
46
docs/.vitepress/cache/deps/@theme_index.js
vendored
@@ -9,31 +9,31 @@ import {
|
|||||||
} from "./chunk-3YS4HNIT.js";
|
} from "./chunk-3YS4HNIT.js";
|
||||||
|
|
||||||
// node_modules/vitepress/dist/client/theme-default/index.js
|
// node_modules/vitepress/dist/client/theme-default/index.js
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/fonts.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/fonts.css";
|
||||||
|
|
||||||
// node_modules/vitepress/dist/client/theme-default/without-fonts.js
|
// node_modules/vitepress/dist/client/theme-default/without-fonts.js
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/vars.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/vars.css";
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/base.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/base.css";
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/utils.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/utils.css";
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/custom-block.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/custom-block.css";
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-code.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-code.css";
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-code-group.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-code-group.css";
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-doc.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-doc.css";
|
||||||
import "C:/git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-sponsor.css";
|
import "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/styles/components/vp-sponsor.css";
|
||||||
import VPBadge from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPBadge.vue";
|
import VPBadge from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPBadge.vue";
|
||||||
import Layout from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/Layout.vue";
|
import Layout from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/Layout.vue";
|
||||||
import { default as default2 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPBadge.vue";
|
import { default as default2 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPBadge.vue";
|
||||||
import { default as default3 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPImage.vue";
|
import { default as default3 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPImage.vue";
|
||||||
import { default as default4 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPButton.vue";
|
import { default as default4 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPButton.vue";
|
||||||
import { default as default5 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPHomeHero.vue";
|
import { default as default5 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPHomeHero.vue";
|
||||||
import { default as default6 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPHomeFeatures.vue";
|
import { default as default6 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPHomeFeatures.vue";
|
||||||
import { default as default7 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPHomeSponsors.vue";
|
import { default as default7 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPHomeSponsors.vue";
|
||||||
import { default as default8 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPDocAsideSponsors.vue";
|
import { default as default8 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPDocAsideSponsors.vue";
|
||||||
import { default as default9 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPSponsors.vue";
|
import { default as default9 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPSponsors.vue";
|
||||||
import { default as default10 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamPage.vue";
|
import { default as default10 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamPage.vue";
|
||||||
import { default as default11 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamPageTitle.vue";
|
import { default as default11 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamPageTitle.vue";
|
||||||
import { default as default12 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamPageSection.vue";
|
import { default as default12 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamPageSection.vue";
|
||||||
import { default as default13 } from "C:/git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamMembers.vue";
|
import { default as default13 } from "/Users/djdietrick/Git/docs/node_modules/vitepress/dist/client/theme-default/components/VPTeamMembers.vue";
|
||||||
|
|
||||||
// node_modules/vitepress/dist/client/shared.js
|
// node_modules/vitepress/dist/client/shared.js
|
||||||
var inBrowser = typeof document !== "undefined";
|
var inBrowser = typeof document !== "undefined";
|
||||||
|
|||||||
16
docs/.vitepress/cache/deps/_metadata.json
vendored
16
docs/.vitepress/cache/deps/_metadata.json
vendored
@@ -1,31 +1,31 @@
|
|||||||
{
|
{
|
||||||
"hash": "3c715ca3",
|
"hash": "b31dd5f2",
|
||||||
"configHash": "9ebaad5b",
|
"configHash": "9af315cb",
|
||||||
"lockfileHash": "ebeae15b",
|
"lockfileHash": "951d497c",
|
||||||
"browserHash": "0b0ad317",
|
"browserHash": "14d9b0dd",
|
||||||
"optimized": {
|
"optimized": {
|
||||||
"vue": {
|
"vue": {
|
||||||
"src": "../../../../node_modules/vue/dist/vue.runtime.esm-bundler.js",
|
"src": "../../../../node_modules/vue/dist/vue.runtime.esm-bundler.js",
|
||||||
"file": "vue.js",
|
"file": "vue.js",
|
||||||
"fileHash": "7cf78117",
|
"fileHash": "6eb7142a",
|
||||||
"needsInterop": false
|
"needsInterop": false
|
||||||
},
|
},
|
||||||
"vitepress > @vue/devtools-api": {
|
"vitepress > @vue/devtools-api": {
|
||||||
"src": "../../../../node_modules/@vue/devtools-api/dist/index.js",
|
"src": "../../../../node_modules/@vue/devtools-api/dist/index.js",
|
||||||
"file": "vitepress___@vue_devtools-api.js",
|
"file": "vitepress___@vue_devtools-api.js",
|
||||||
"fileHash": "98f5c738",
|
"fileHash": "f77c0dac",
|
||||||
"needsInterop": false
|
"needsInterop": false
|
||||||
},
|
},
|
||||||
"vitepress > @vueuse/core": {
|
"vitepress > @vueuse/core": {
|
||||||
"src": "../../../../node_modules/@vueuse/core/index.mjs",
|
"src": "../../../../node_modules/@vueuse/core/index.mjs",
|
||||||
"file": "vitepress___@vueuse_core.js",
|
"file": "vitepress___@vueuse_core.js",
|
||||||
"fileHash": "2d266902",
|
"fileHash": "4488c276",
|
||||||
"needsInterop": false
|
"needsInterop": false
|
||||||
},
|
},
|
||||||
"@theme/index": {
|
"@theme/index": {
|
||||||
"src": "../../../../node_modules/vitepress/dist/client/theme-default/index.js",
|
"src": "../../../../node_modules/vitepress/dist/client/theme-default/index.js",
|
||||||
"file": "@theme_index.js",
|
"file": "@theme_index.js",
|
||||||
"fileHash": "2eb1f2ce",
|
"fileHash": "f16ffef2",
|
||||||
"needsInterop": false
|
"needsInterop": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
18
docs/interview/ds/heap.md
Normal file
18
docs/interview/ds/heap.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Heap
|
||||||
|
|
||||||
|
A heap is a specialized tree-based data structure that satisfies the heap property: in a max-heap, for any given node I, the value of I is greater than or equal to the values of its children, while a min-heap has the value of I less than or equal to its children. Heaps are commonly implemented as arrays.
|
||||||
|
|
||||||
|
Common applications include:
|
||||||
|
- Priority queues
|
||||||
|
- Heap sort algorithm
|
||||||
|
- Finding kth largest/smallest elements
|
||||||
|
- Implementing efficient graph algorithms (Dijkstra's)
|
||||||
|
|
||||||
|
## Time Complexities
|
||||||
|
|
||||||
|
- Insert element: O(log n)
|
||||||
|
- Delete element: O(log n)
|
||||||
|
- Get min/max element: O(1)
|
||||||
|
- Build heap from array: O(n)
|
||||||
|
- Heapify (fix heap property): O(log n)
|
||||||
|
- Search for element: O(n)
|
||||||
181
docs/interview/languages/javascript.md
Normal file
181
docs/interview/languages/javascript.md
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
# JavaScript
|
||||||
|
|
||||||
|
## Arrays
|
||||||
|
|
||||||
|
### Creating
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let a = [];
|
||||||
|
let a = [1, 2, 3];
|
||||||
|
let a = Array(3).fill(0); // [0,0,0]
|
||||||
|
let a = Array.from({ length: 5 }, (_, i) => i + 1); // [1,2,3,4,5]
|
||||||
|
let a = [...Array(5).keys()]; // [0,1,2,3,4]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Accessing
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const a = [1, 2, 3];
|
||||||
|
console.log(a[0]); // 1
|
||||||
|
console.log(a.at(-1)); // 3 (last element)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Manipulating
|
||||||
|
|
||||||
|
#### Adding
|
||||||
|
```javascript
|
||||||
|
const a = [];
|
||||||
|
a.push(1); // Add to end
|
||||||
|
a.unshift(2); // Add to beginning
|
||||||
|
a.splice(1, 0, 3); // Insert 3 at index 1
|
||||||
|
[...a, 4]; // Create new array with 4 added
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Removing
|
||||||
|
```javascript
|
||||||
|
const arr = ['bread', 'butter', 'eggs', 'milk'];
|
||||||
|
arr.pop(); // returns and removes 'milk'
|
||||||
|
arr.shift(); // returns and removes 'bread'
|
||||||
|
arr.splice(1, 1); // removes 'butter'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Slicing
|
||||||
|
```javascript
|
||||||
|
const a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster'];
|
||||||
|
a.slice(2, 5); // ['bacon', 'tomato', 'ham']
|
||||||
|
a.slice(-3); // ['tomato', 'ham', 'lobster']
|
||||||
|
a.slice(2); // ['bacon', 'tomato', 'ham', 'lobster']
|
||||||
|
[...a]; // Create a copy of an array
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Sorting
|
||||||
|
```javascript
|
||||||
|
const nums = [3, 1, 3, 2, 5];
|
||||||
|
nums.sort(); // [1, 2, 3, 3, 5]
|
||||||
|
nums.reverse(); // [5, 3, 3, 2, 1]
|
||||||
|
[...nums].sort((a, b) => b - a); // Sort descending, creates new array
|
||||||
|
|
||||||
|
const items = [
|
||||||
|
{ name: 'john', grade: 'A', age: 15 },
|
||||||
|
{ name: 'jane', grade: 'B', age: 12 },
|
||||||
|
{ name: 'dave', grade: 'B', age: 10 }
|
||||||
|
];
|
||||||
|
items.sort((a, b) => a.age - b.age);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Filtering
|
||||||
|
```javascript
|
||||||
|
const odds = [1, 2, 3, 4, 5].filter(x => x % 2 === 1);
|
||||||
|
const evens = Array.from({ length: 20 }, (_, i) => i + 1).filter(x => x % 2 === 0);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Strings
|
||||||
|
|
||||||
|
Strings in JavaScript are immutable but have many built-in methods.
|
||||||
|
|
||||||
|
### Helper Functions
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const str = 'spam';
|
||||||
|
'I saw spamalot!'.includes(str); // true
|
||||||
|
'I saw The Holy Grail!'.includes(str); // false
|
||||||
|
|
||||||
|
'12345'.repeat(5); // '1234512345123451234512345'
|
||||||
|
|
||||||
|
['John', 'Peter', 'Vicky'].join('#'); // 'John#Peter#Vicky'
|
||||||
|
'Hello, world!'.endsWith('!'); // true
|
||||||
|
'Hello, world!'.startsWith('H'); // true
|
||||||
|
'Hello, world!'.substring(0, 5); // 'Hello'
|
||||||
|
'Hello, world!'.slice(-6); // 'world!'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Objects
|
||||||
|
|
||||||
|
### Creating
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const obj = {};
|
||||||
|
const obj = { color: 'green', points: 5 };
|
||||||
|
const obj = Object.create(null);
|
||||||
|
const obj = new Object();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Accessing
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
obj.color;
|
||||||
|
obj['color'];
|
||||||
|
obj?.points; // Optional chaining
|
||||||
|
const { color, points } = obj; // Destructuring
|
||||||
|
|
||||||
|
obj.age = 30;
|
||||||
|
Object.assign(obj, { height: 180 }); // Merge properties
|
||||||
|
```
|
||||||
|
|
||||||
|
### Removing
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
delete obj.age;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Iterating
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
Object.keys(obj).forEach(key => {
|
||||||
|
// do something
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.values(obj).forEach(value => {
|
||||||
|
// do something
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.entries(obj).forEach(([key, value]) => {
|
||||||
|
// do something
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Object Methods
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const keys = Object.keys(obj);
|
||||||
|
const values = Object.values(obj);
|
||||||
|
const entries = Object.entries(obj);
|
||||||
|
const merged = Object.assign({}, obj1, obj2);
|
||||||
|
const clone = { ...obj };
|
||||||
|
|
||||||
|
// Prevent modifications
|
||||||
|
Object.freeze(obj);
|
||||||
|
Object.seal(obj);
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sets and Maps
|
||||||
|
|
||||||
|
### Set
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const set = new Set();
|
||||||
|
set.add(1);
|
||||||
|
set.has(1); // true
|
||||||
|
set.delete(1);
|
||||||
|
set.clear();
|
||||||
|
|
||||||
|
const unique = [...new Set([1, 2, 2, 3, 3])]; // [1, 2, 3]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Map
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const map = new Map();
|
||||||
|
map.set('key', 'value');
|
||||||
|
map.get('key');
|
||||||
|
map.has('key');
|
||||||
|
map.delete('key');
|
||||||
|
map.clear();
|
||||||
|
|
||||||
|
// Object to Map
|
||||||
|
const map = new Map(Object.entries(obj));
|
||||||
|
|
||||||
|
// Map to Object
|
||||||
|
const obj = Object.fromEntries(map);
|
||||||
|
```
|
||||||
|
|
||||||
209
docs/interview/languages/python.md
Normal file
209
docs/interview/languages/python.md
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
# Python
|
||||||
|
|
||||||
|
## Arrays
|
||||||
|
|
||||||
|
### Creating
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = []
|
||||||
|
a = [1,2,3]
|
||||||
|
a = list(range(1,11)) # 1 ... 10
|
||||||
|
a = [x for x in [3, 4, 5, 6, 7] if x > 5]
|
||||||
|
a = [0] * 3 # [0,0,0]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Accessing
|
||||||
|
|
||||||
|
```python
|
||||||
|
a = [1,2,3]
|
||||||
|
print(a[0]) # 1
|
||||||
|
print(a[-1]) # 3
|
||||||
|
```
|
||||||
|
|
||||||
|
### Manipulating
|
||||||
|
|
||||||
|
#### Adding
|
||||||
|
```python
|
||||||
|
a = []
|
||||||
|
a.append(1)
|
||||||
|
a.extend([9, 11, 13])
|
||||||
|
a += [6,7,8]
|
||||||
|
a.insert(0, 2) # Insert 2 at index 0
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Removing
|
||||||
|
```python
|
||||||
|
li = ['bread', 'butter', 'eggs', 'milk']
|
||||||
|
li.pop() # returns and removes 'milk'
|
||||||
|
li.pop(2) # returns and removes 'eggs'
|
||||||
|
del li[0] # removes 'bread'
|
||||||
|
li.remove('butter')
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Slicing
|
||||||
|
```python
|
||||||
|
# a_list[start:end]
|
||||||
|
# a_list[start:end:step]
|
||||||
|
a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
|
||||||
|
a[2:5] # ['bacon', 'tomato', 'ham']
|
||||||
|
a[-3:] # ['tomato', 'ham', 'lobster']
|
||||||
|
a[-5:-2] # ['egg', 'bacon', 'tomato']
|
||||||
|
a[:4] # ['spam', 'egg', 'bacon', 'tomato']
|
||||||
|
a[2:] # ['bacon', 'tomato', 'ham', 'lobster']
|
||||||
|
a[0:6:2] # ['spam', 'bacon', 'ham']
|
||||||
|
a[6:0:-2] # ['lobster', 'tomato', 'egg']
|
||||||
|
a[::-1] # ['lobster', 'ham', 'tomato', 'bacon', 'egg', 'spam']
|
||||||
|
a[:] # Create a copy of an array
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Sorting
|
||||||
|
```python
|
||||||
|
li = [3, 1, 3, 2, 5]
|
||||||
|
li.sort() # [1, 2, 3, 3, 5]
|
||||||
|
li.reverse() # [5, 3, 3, 2, 1]
|
||||||
|
sorted(li) # returns a sorted copy of li
|
||||||
|
|
||||||
|
li = [
|
||||||
|
('john', 'A', 15),
|
||||||
|
('jane', 'B', 12),
|
||||||
|
('dave', 'B', 10)
|
||||||
|
]
|
||||||
|
sorted(li, key=lambda x: x[2])
|
||||||
|
sorted(li, cmp=lambda x,y: 1 if x<y else -1) # Negative return means second number is "less than"
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Filtering
|
||||||
|
```python
|
||||||
|
a = list(filter(lambda x : x % 2 == 1, range(1, 20)))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Strings
|
||||||
|
|
||||||
|
Strings act a lot like arrays/lists in Python, so many of the methods still apply.
|
||||||
|
|
||||||
|
### Helper Functions
|
||||||
|
|
||||||
|
```python
|
||||||
|
s = 'spam'
|
||||||
|
s in 'I saw spamalot!' # True
|
||||||
|
s not in 'I saw The Holy Grail!' # True
|
||||||
|
|
||||||
|
s = '12345' * 5 # '1234512345123451234512345'
|
||||||
|
|
||||||
|
"#".join(["John", "Peter", "Vicky"]) # 'John#Peter#Vicky'
|
||||||
|
"Hello, world!".endswith("!") # True
|
||||||
|
"Hello, world!".startswith("H") # True
|
||||||
|
```
|
||||||
|
|
||||||
|
## Dictionaries
|
||||||
|
|
||||||
|
### Creating
|
||||||
|
|
||||||
|
```python
|
||||||
|
d = {}
|
||||||
|
d = {'color': 'green', 'points', 5}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Accessing
|
||||||
|
|
||||||
|
```python
|
||||||
|
d['color']
|
||||||
|
d.get('points') # Returns None or value of points
|
||||||
|
d.get('points', 0) # Default value
|
||||||
|
|
||||||
|
d['age'] = 30
|
||||||
|
```
|
||||||
|
|
||||||
|
### Removing
|
||||||
|
|
||||||
|
```python
|
||||||
|
del d['age']
|
||||||
|
```
|
||||||
|
|
||||||
|
### Iterating
|
||||||
|
|
||||||
|
```python
|
||||||
|
for key, value in d.items():
|
||||||
|
# do something
|
||||||
|
|
||||||
|
for key in d.keys():
|
||||||
|
# do something
|
||||||
|
|
||||||
|
for value in d.values():
|
||||||
|
# do something
|
||||||
|
```
|
||||||
|
|
||||||
|
### Zipping
|
||||||
|
|
||||||
|
```python
|
||||||
|
group_1 = ['kai', 'abe', 'ada', 'gus', 'zoe']
|
||||||
|
group_2 = ['jen', 'eva', 'dan', 'isa', 'meg']
|
||||||
|
pairings = {name:name_2 for name, name_2 in zip(group_1, group_2)}
|
||||||
|
# {'kai': 'jen', 'abe': 'eva', 'ada': 'dan', 'gus': 'isa', 'zoe': 'meg'}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sorting
|
||||||
|
|
||||||
|
#### By Key
|
||||||
|
```python
|
||||||
|
my_dict = {'b': 2, 'a': 1, 'c': 3}
|
||||||
|
sorted_dict_by_keys = sorted(my_dict.items())
|
||||||
|
# [('a', 1), ('b', 2), ('c', 3)]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### By Value
|
||||||
|
`dict.items()` returns a tuple of the key value pairs for each iteration, so we can use sorted and use the value of the pair to return a list of the sorted pairs.
|
||||||
|
```python
|
||||||
|
my_dict = {'b': 2, 'a': 1, 'c': 3}
|
||||||
|
sorted_dict_by_values = sorted(my_dict.items(), key=lambda item: item[1])
|
||||||
|
# [('a', 1), ('b', 2), ('c', 3)]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### OrderedDict
|
||||||
|
OrderedDicts preserve the order in which the items were added, whereas normal dicts do not guarantee the order upon iteration. So you can sort the dictionary by key or value, and then pass that into OrderedDict to have a dict-like structure that preserves the inserted order.
|
||||||
|
|
||||||
|
```python
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
my_dict = {'b': 2, 'a': 4, 'c': 3}
|
||||||
|
sorted_dict_by_keys = OrderedDict(sorted(my_dict.items()))
|
||||||
|
for key, value in sorted_dict_by_keys:
|
||||||
|
# do something
|
||||||
|
```
|
||||||
|
|
||||||
|
## Heap
|
||||||
|
|
||||||
|
`heapq` is a module that implements a heap structure on a list of items. It's helpful for finding kth largest/smallest elements and priority queues.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import heapq
|
||||||
|
|
||||||
|
li = [10, 20, 15, 30, 40]
|
||||||
|
|
||||||
|
heapq.heapify(li)
|
||||||
|
# [10, 20, 15, 30, 40]
|
||||||
|
|
||||||
|
# Appending an element
|
||||||
|
heapq.heappush(h, 5)
|
||||||
|
|
||||||
|
# Pop the smallest element from the heap
|
||||||
|
min = heapq.heappop(h)
|
||||||
|
|
||||||
|
# Push a new element (5), then pop the smallest element and return
|
||||||
|
min = heapq.heappushpop(h, 5)
|
||||||
|
|
||||||
|
# Pop the smallest element, and then push new element (5)
|
||||||
|
min = heapq.heapreplace(h, 5)
|
||||||
|
|
||||||
|
# Find the 3 largest elements
|
||||||
|
maxi = heapq.nlargest(3, h)
|
||||||
|
# [40, 30, 20]
|
||||||
|
|
||||||
|
# Find the 3 smallest elements
|
||||||
|
min = heapq.nsmallest(3, h)
|
||||||
|
# [10, 15, 20]
|
||||||
|
|
||||||
|
# Merge heaps
|
||||||
|
h3 = list(heapq.merge(h1, h2))
|
||||||
|
```
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
{"text": "Strings", "link": "/interview/ds/string"},
|
{"text": "Strings", "link": "/interview/ds/string"},
|
||||||
{"text": "Graphs", "link": "/interview/ds/graph"},
|
{"text": "Graphs", "link": "/interview/ds/graph"},
|
||||||
{"text": "Trees", "link": "/interview/ds/tree"},
|
{"text": "Trees", "link": "/interview/ds/tree"},
|
||||||
|
{"text": "Heap", "link": "/interview/ds/heap"},
|
||||||
{"text": "Patterns", "link": "/interview/ds/patterns"}
|
{"text": "Patterns", "link": "/interview/ds/patterns"}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -49,5 +50,12 @@
|
|||||||
{"text": "Facade", "link": "/interview/patterns/structure/facade"}
|
{"text": "Facade", "link": "/interview/patterns/structure/facade"}
|
||||||
]}
|
]}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "Languages Specific",
|
||||||
|
"items": [
|
||||||
|
{"text": "Python", "link": "/interview/languages/python"},
|
||||||
|
{"text": "Javascript", "link": "/interview/languages/javascript"}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
Reference in New Issue
Block a user