Skip to content

Instantly share code, notes, and snippets.

View aarohmankad's full-sized avatar
🎯
Focusing

Aaroh Mankad aarohmankad

🎯
Focusing
View GitHub Profile
#!/bin/bash
users=($(cat /etc/passwd | awk -F":" '{print $1}'))
uids=($(cat /etc/passwd | awk -F":" '{print $3}'))
for i in "${!users[@]}"; do
groups=($(cat /etc/group | grep "^${users[i]}" | awk -F":" '{print $4}'))
echo ${users[i]} ${uids[i]} $groups
done
{
"payload": [{
"name": "Aquaman",
"stars": 5,
}, {
"name": "Christopher Robin",
"stars": 4.5,
}]
}
double add(double a, double b) {
return a + b;
}
int main() {
assert(add(3, 4) == 7);
return 0;
}
function add(a, b) {
return a + b;
}
let sum = add(3, 4);
if (sum !== 7) {
console.log("add(3, 4) failed. Expected: 7, Actual: " + sum);
}
# Functions.
# These are like aliases, but can take arguments
# All functions are in ~/.bash_functions for modularity
if [ -f ~/.bash_functions ]; then
. ~/.bash_functions
fi
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
else
if [ -f $1 ] ; then
# NAME=${1%.*}
# mkdir $NAME && cd $NAME
case $1 in
*.tar.bz2) tar xvjf ./$1 ;;
alias extract='tar xvzf'
@aarohmankad
aarohmankad / handle_errors.cpp
Created December 7, 2017 19:09
An example of handling some simple edge cases
vector<pair<int, int>> findCells(const vector<pair<int, int>>& gameBoard, const int FLAG_CODE) {
if (gameBoard.empty()) {
return vector<pair<int, int>> {};
}
vector<pair<int, int>> cells;
for (pair<int, int> cell : gameBoard) {
if (cell.second == FLAG_CODE) {
cells.push_back(cell);
@aarohmankad
aarohmankad / comments.cpp
Last active December 7, 2017 00:52
A good example showing why to leave comments in your code
int hash(string input) {
int output = 5381;
// Less collisions than simply adding ASCII characters of value
// simple method: hash("tab") = hash("bat")
// this method: hash("tab") != hash("bat")
for (char c : input) {
output += (output << 5) + c;
}
@aarohmankad
aarohmankad / abstraction.cpp
Last active December 28, 2017 19:34
An example of code abstraction
vector<Cell> findCells(const vector<Cell>& gameBoard, const int FLAG_CODE) {
vector<Cell> cells;
for (Cell cell : gameBoard) {
if (cell.status == FLAG_CODE) {
cells.push_back(cell);
}
}
return cells;