Skip to content

Instantly share code, notes, and snippets.

View smartwatermelon's full-sized avatar
🏠
Working from home permanently

Andrew Rich smartwatermelon

🏠
Working from home permanently
View GitHub Profile
@smartwatermelon
smartwatermelon / zerosEndingFactorial.sh
Last active July 26, 2021 19:18
@cassidoo's interview question from July 25, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# Given a positive integer n, write a function that finds the number of zeros
# at the end of n! in base 10.
if [ $# -eq 0 ] || [ -n "$(printf '%s\n' "$1" | sed 's/[0-9]//g')" ]; then
echo "$(basename "$0"): enter a positive integer"
exit 1
fi
@smartwatermelon
smartwatermelon / inRange.sh
Last active July 21, 2022 05:19
@cassidoo's interview question from July 11, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# Given an IPv4 address and a netmask in CIDR notation, return a boolean
# specifying whether the IP address is inside the given range.
# requirements:
# # bash version >= 4
# # 'grepcidr' command (http://www.pc-tools.net/unix/grepcidr/)
@smartwatermelon
smartwatermelon / translateShift.sh
Created July 9, 2021 18:14
@cassidoo's interview question from July 4, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# Imagine your users are all typing slightly incorrectly, in that they shifted their
# hands one key to the right. Write a function that translates what they mean to say.
if [ $# -ne 1 ]; then
echo "$(basename $0): enter a mis-typed string, e.g. ';p; epe'"
exit 1
fi
@smartwatermelon
smartwatermelon / printArrow.sh
Created June 14, 2021 20:33
@cassidoo's Interview question of the week for June 13, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# Given a direction and a number of columns, write a function that outputs
# an arrow of asterisks
if [ $# -ne 2 ] ||
! ( ( [ $( echo $1 | tr [':upper:'] [':lower:'] ) == 'left' ] || [ $( echo $1 | tr [':upper:'] [':lower:'] ) == 'right' ] ) && [ -z "$(printf '%s\n' "$2" | sed 's/[0-9]//g')" ] ); then
echo "$(basename $0): enter 'left' or 'right' and a positive integer, e.g. 'left 4' or 'right 3'"
exit 1
@smartwatermelon
smartwatermelon / lonelyNumber.sh
Created June 7, 2021 16:55
@cassidoo's Interview question of the week for June 6, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# lonelyNumber.sh
# @cassidoo 2021-06-06
# Given three numbers, return their product. But, if one of the numbers is the same as
# another, it does not count: If two numbers are similar, return the lonely number. If all
# numbers are same, return 1.
if [ $# -ne 3 ] || ! ([[ $1 =~ ^[0-9]+$ ]] && [[ $2 =~ ^[0-9]+$ ]] && [[ $3 =~ ^[0-9]+$ ]]); then
@smartwatermelon
smartwatermelon / find_meeting.rb
Last active May 23, 2021 03:45
@cassidoo's Interview question of the week for May 16, 2021
#!/usr/bin/env ruby
=begin
This week’s question:
Given a list of people with their names and the time slots when they are available in a given week, and a meeting length, return the first meeting time available (if possible). Format the input however you’d like to receive it!
=end
=begin
Andrew Rich [email protected]
https://projectinsomnia.com/
=end
@smartwatermelon
smartwatermelon / sameDigits.sh
Created May 10, 2021 22:45
@cassidoo's Interview question of the week for May 10, 2021
#!/usr/bin/env bash
set -eu -o pipefail
if [ $# -eq 0 ] || [ -n "$(printf '%s\n' "$1" | sed 's/[0-9]//g')" ]; then
echo "$(basename $0): enter a positive integer"
exit 1
else
echo "$(basename $0): you entered $1"
fi
@smartwatermelon
smartwatermelon / htmlvalidator.sh
Last active October 19, 2020 23:11
htmlValidator: @cassidoo's 2020-10-19 newsletter interview question
#!/bin/bash
function htmlValidator ()
{
if xmllint --noout --nowarning - 2> /dev/null <<< "$1"; then
echo "valid";
else
echo "not valid";
fi
}
@smartwatermelon
smartwatermelon / rps.sh
Last active August 16, 2020 03:19
Rock, Paper, Scissors in bash, based on @cassidoo's interview question from November 24, 2019
#!/usr/bin/env bash
set -eu -o pipefail
# shellcheck source=/Users/andrewrich/etc/functions.sh
. "$HOME/etc/functions.sh"
readonly R="ROCK" P="PAPER" S="SCISSORS"
about () {
cat <<EOF
$( basename "$0" ) by Andrew Rich <[email protected]>
Based on @cassidoo's interview question from https://buttondown.email/cassidoo/archive/be-careful-with-your-words-once-they-are-said/
@smartwatermelon
smartwatermelon / logicgate.sh
Created May 15, 2020 22:50
Logic Gates in bash, based on @cassidoo's interview question from November 11, 2019
#!/usr/bin/env bash
set -eu -o pipefail
about () {
cat <<EOF
$( basename "$0" ) by Andrew Rich <[email protected]>
Based on @cassidoo's interview question from https://tinyletter.com/cassidoo/letters/be-curious-read-widely-try-new-things-what-people-call-intelligence-just-boils-down-to-curiosity-aaron-swartz
Definition and table of logic gates: https://whatis.techtarget.com/definition/logic-gate-AND-OR-XOR-NOT-NAND-NOR-and-XNOR
EOF