Bouncing balls, again (IV)
Euler problem 14
Today’s problem was to find the longest chain and my solution looks like this:
function main() {
let biggestLength = 0;
let biggestValue = 0;
for (let i = 1; i < 1_000_000; i++) {
const chain = takeAll(collatz(i));
if (chain.length > biggestLength) {
biggestLength = chain.length;
biggestValue = i
}
}
console.log(biggestValue, biggestLength)
}
This isn’t exactly what I want to write. I want to write something closer to this:
const a = range(1, 1_000_000)
.map(collatz)
.map(takeAll)
.sort(longestToShortest)[0][0]
But if I do this my poor laptop runs out of memory and Javascript exits and I don’t get an answer. can see why- this asks the machine to keep everything in memory. I think maybe a way around this is a generator that returns the biggest length seen so far. I can’t quite see the solution to that, though, because I need to control the value feeding into the generator (the n that collatz is starting at) and the output (the length of the array).
Missing Semester Homework 2
Question 1
Read
man ls
and write anls
command that lists files in the following manner
- Includes all files, including hidden files
euler/014 main
❯ ls -a
. .. main.ts
- Sizes are listed in human readable format (e.g. 454M instead of 454279954)
❯ ls -lh
total 672M
-rw-r--r-- 1 nicole nicole 145M Feb 18 2020 'Broot: An Innovative Way To View Your Linux File System [eTtarjSJ0yU].mp4'
-rw-r--r-- 1 nicole nicole 105M Feb 3 2020 'Broot Is A Better Way To Navigate Directories [lGD-wsQO_lI].mp4'
-rw-r--r-- 1 nicole nicole 74M Aug 27 16:20 'I'\''M GOING TO USE THE NNN FILE BROWSER! 😮 [U2n5aGqou9E].mp4'
-rw-r--r-- 1 nicole nicole 252M Nov 17 2020 'NNN file manager and plugin system (revisited) [-knZwdd1ScU].mp4'
-rw-r--r-- 1 nicole nicole 98M Aug 28 03:02 'nnn file manager basics - Linux [il2Fm-KJJfM].mp4'
- Files are ordered by recency
❯ ls -alt
total 687700
drwx------ 39 nicole nicole 4096 Nov 15 13:36 ..
drwxr-xr-x 2 nicole nicole 4096 Nov 14 19:58 .
-rw-r--r-- 1 nicole nicole 102596284 Aug 28 03:02 'nnn file manager basics - Linux [il2Fm-KJJfM].mp4'
-rw-r--r-- 1 nicole nicole 76583213 Aug 27 16:20 'I'\''M GOING TO USE THE NNN FILE BROWSER! 😮 [U2n5aGqou9E].mp4'
-rw-r--r-- 1 nicole nicole 264003848 Nov 17 2020 'NNN file manager and plugin system (revisited) [-knZwdd1ScU].mp4'
-rw-r--r-- 1 nicole nicole 151358168 Feb 18 2020 'Broot: An Innovative Way To View Your Linux File System [eTtarjSJ0yU].mp4'
-rw-r--r-- 1 nicole nicole 109641797 Feb 3 2020 'Broot Is A Better Way To Navigate Directories [lGD-wsQO_lI].mp4'
Output is colorized
❯ ls --color
Applications Arduino battery.sh coding-math dead.letter Desktop Documents Downloads Music Pictures Public Templates Videos workspace
Question 2
Write bash functions
marco
andpolo
that do the following. Whenever you executemarco
the current working directory should be saved in some manner, then when you executepolo
, no matter what directory you are in,polo
shouldcd
you back to the directory where you executedmarco
. For ease of debugging you can write the code in a filemarco.sh
and (re)load the definitions to your shell by executingsource marco.sh
.
Marco:
#! /usr/bin/env bash
echo $(pwd) >~/.marco-save
Polo:
#! /usr/bin/env bash
file=~/.marco-save
if [ ! -e "$file" ]; then
echo "Run marco first!"
exit 1
fi
cd $(cat ~/.marco-save)
Question 3
Say you have a command that fails rarely. In order to debug it you need to capture its output but it can be time consuming to get a failure run. Write a bash script that runs the following script until it fails and captures its standard output and error streams to files and prints everything at the end. Bonus points if you can also report how many runs it took for the script to fail.
#! /usr/bin/env bash
if [ "$#" -ne 1 ]; then
echo "Pass the program to run"
fi
COUNTER=1
while true; do
CODE=$(bash "$1")
if [ "$?" -ne 0 ]; then
echo "Took $COUNTER runs"
exit 0
fi
COUNTER=$((COUNTER + 1))
done
#! /usr/bin/env bash
if [ "$#" -ne 1 ]; then
echo "Pass the program to run"
fi
COUNTER=1
while true; do
CODE=$(bash "$1")
if [ "$?" -ne 0 ]; then
echo "Took $COUNTER runs"
exit 0
fi
COUNTER=$((COUNTER + 1))
done
Question 4
As we covered in the lecture
find
’s-exec
can be very powerful for performing operations over the files we are searching for. However, what if we want to do something with all the files, like creating a zip file? As you have seen so far commands will take input from both arguments and STDIN. When piping commands, we are connecting STDOUT to STDIN, but some commands liketar
take inputs from arguments. To bridge this disconnect there’s thexargs
command which will execute a command using STDIN as arguments. For examplels | xargs rm
will delete the files in the current directory.Your task is to write a command that recursively finds all HTML files in the folder and makes a zip with them. Note that your command should work even if the files have spaces (hint: check
-d
flag forxargs
).
If you’re on macOS, note that the default BSD find
is different from the one included in GNU coreutils. You can use -print0
on find
and the -0
flag on xargs
. As a macOS user, you should be aware that command-line utilities shipped with macOS may differ from the GNU counterparts; you can install the GNU versions if you like by using brew.
find -name "*.html" -print0 | xargs -0 -P 4 -n 1 gzip
End of day
Today was a great day! I had a chance to share, be social, and do some work on things I’m interested in.
Worked on Today
- I took another shot at 3D bouncing balls, this time the size is logarithmic so it feels slightly closer to 3D? https://creative.nicole.computer/2024-11-14-bouncing-ball-iv/
- I worked problem 14 for Euler. Kind of eh on this solution but I was thinking about how to make it more generic and I am definitely working at my edge thinking about generators that take generators and predicates and perform transformations: https://github.com/nicolecomputer/euler/blob/main/014/main.ts
- I did the homework for missing semester Lecture 2: https://notes.nicole.computer/daily/2024-11-15#missing-semester-homework-2
- I also had a chance to pair
- With @ChiWilliamson DNS config
- With @grkw on Bash scripting!
- Aaaaaalso a quick trip to microcenter to stick more RAM in my laptop (She has 24 Gb now!)
- I’m thinking about a comment that Nick Bergson-Shilcock made that “many programmers at RC rediscover their spark through creative programming” which has me thinking about the power of play
Tomorrow
This weekend I need to rest. I think I’m going to pick on the days and intentionally not program (or do less). I’ve had 2 nights since I’ve been at RC where I’ve needed 10+ hours of sleep and I think a little break might help make me more effective heading into next week.