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 / removeZeroes.sh
Created May 3, 2023 01:14
@cassidoo's interview question from April 30, 2023
#!/usr/bin/env bash
set -eu -o pipefail
removeZeroes () {
IFS=" " read -ra array <<< "$@"
declare -a ltrim=("")
declare -a rtrim=("")
tmp=""
length="${#array[@]}"
@smartwatermelon
smartwatermelon / countSelfChars.sh
Created April 3, 2023 18:28
@cassidoo's interview question from April 2, 2023
#!/usr/bin/env bash
set -eu -o pipefail
# Define function to convert a number to English words
function num_to_words {
local ones=( '' one two three four five six seven eight nine )
local tens=( '' '' twenty thirty forty fifty sixty seventy eighty ninety )
local specials=( ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen )
local num=$1
@smartwatermelon
smartwatermelon / passDoor.sh
Created October 17, 2022 18:31
@cassidoo's interview question from October 17, 2022
#!/usr/bin/env bash
set -eu -o pipefail
# initialize vars
allDoors=() # array
# check input
ARGC="$#"
if [[ "$ARGC" -ne 2 ]]; then
echo "Please enter two parameters: number of doors, and number of passes."
@smartwatermelon
smartwatermelon / truncate
Created October 10, 2022 22:02
@cassidoo's interview question from October 10, 2022
MONTASIO:~ andrewrich$ N=3
MONTASIO:~ andrewrich$ STR="never gonna give you up"
MONTASIO:~ andrewrich$ for word in $STR; do echo "${word:0:$N}"; done
nev
gon
giv
you
up
@smartwatermelon
smartwatermelon / ordinalize.sh
Last active September 29, 2022 21:08
@cassidoo's interview question from September 25, 2022
#!/usr/bin/env bash
set -eu -o pipefail
# initialize vars
ordinal=''
# check input
ARGC="$#"
if [[ "$ARGC" -ne 1 ]]; then
echo "Please enter an integer number, e.g. 123"
@smartwatermelon
smartwatermelon / calculateGPA.sh
Created September 19, 2022 23:02
@cassidoo's interview question from September 18, 2022
#!/usr/bin/env bash
set -eu -o pipefail
# initialize vars
runningTotal="0"
# normalized grade names to point values
A="4" AMINUS="3.7"
BPLUS="3.3" B="3" BMINUS="2.7"
CPLUS="2.3" C="2" CMINUS="1.7"
@smartwatermelon
smartwatermelon / hideEmail.sh
Created July 21, 2022 05:09
@cassidoo's 20220717 interview question
#!/usr/bin/env bash
set -eu -o pipefail
# substitute asterisks for all but first and last characters of string
obscureString () {
str="$1"
ret="${str:0:1}$( printf "%0.s*" $( seq 1 $(( ${#str}-2 )) ) )${str: -1}"
echo $ret
}
@smartwatermelon
smartwatermelon / prevFib.sh
Created June 22, 2022 23:07
@cassidoo's 20220619 interview question
#!/usr/bin/env bash
set -eu -o pipefail
# return true if input is a perfect square
isPerfectSquare () {
square_root=$( bc <<< "sqrt ($1)" )
return $( [ $(( $square_root * $square_root )) == $1 ] )
}
@smartwatermelon
smartwatermelon / makeTree.sh
Last active December 14, 2021 23:11
@cassidoo's interview question from December 12, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# Given a file named merry-christmas.txt that has a single integer x in it, write a script
# to generate a Christmas tree with asterisks (*) and output it as happy-holidays.txt. The
# tree should have a height of x, and if merry-christmas.txt is empty, the height should
# be 25 asterisks tall.
IN_FILE="merry-christmas.txt"
OUT_FILE="happy-holidays.txt"
@smartwatermelon
smartwatermelon / wrap.sh
Created December 6, 2021 23:12
@cassidoo's interview question from December 5, 2021
#!/usr/bin/env bash
set -eu -o pipefail
# You have to order wrapping paper for presents. Given the length, width, and height of
# the boxes you need to wrap, return the number of square feet (or whatever units you
# want) of wrapping paper you need to order. Extra credit: allow for other shapes of
# presents and their dimensions!
if [ $# -ne 3 ]; then
echo "$(basename $0): enter three nonzero positive integers (length, width, depth)"