back to eggert home
back to home

assign 2

don't you love it when you have to learn two programming languages for one assignment?

lisp (elisp, really)

`notes.txt` questions

you can do some funny lisp playground stuff in emacs' `*scratch*` buffer, opened like so

  1. give an elisp expression/function that computes \(\left(2^{607-1}\right)\left(2^{607}-1\right)\)- you can do this with the `expt` function and friends
  2. then, write an expression that computes how many bits would be needed to store this number
    for example, you need 3 bits to represent 4, and 7 bits to represent 100
    you can do this with a log and ceiling function, should be quite straightforward
  3. what does `C-h k C-h k` do? why?
  4. what about `C-h k M-SPC`?
  5. go to the source code for the function `M-SPC` calls by going to its help page and pressing enter or clicking on the file source name
    the source for `M-SPC` should be implemented in terms of a more general function
    what's that general function? how can you call it?

"we have `what-line` at home" (main part)

take a look at emacs' `what-line` command (run with `M-x what-line`) and notice that it outputs the current line number, but not how many lines are in the file total

how about we fix that? make a file `gps-line.el` that contains a lisp function `what-line` that outputs the following

``` Line [x]/[n] ```

where `[x]` should just be the line you're currently at and `[n]` is the total number of lines in the buffer
visit `what-line`'s source code to see how it works!

to count the total number of lines, you should just get the buffer and count the number of newline characters
due to this specification, your function should be able to say things like `Line 1/0` when called on some text that doesn't end in a newline

to give you an idea of what this file might look like, here's the general structure of my `gps-line.el`

```lisp (defun gps-line() (interactive) (message "do some stuff here")) ```

if you look at the source code for `what-line`, you'll see that there's some special logic for "narrowed buffers" or whatever the hell
i would recommend implementing support for that just in case, since the code is pretty copy and paste-able for that

you should be able to load the file and the function within by typing `M-:` (really `M-shift-;`) and running this: ```lisp (load "gps-line.el") ``` or you can follow this guide to load it in on startup

python

honestly wonder what proportion of a 35l class knows python already

but anyways there's three versions of python on the seasnet servers that are of relevance to this part
any questions that require running code or whatever, i'd recommend you do them on there

`notes.txt` questions part 1

consider the following script randline (WINTER 24 VERSION, MAY BE OUTDATED)

  1. what happens when you run this and why? ```bash ./randline.py /dev/null # or any other empty file ```
  2. this script is built for python 2- what happens when it's run with python 3? like this: ```bash python3 randline.py ```

main part

code up a python 3 script `shuf.py` that mimics the default `shuf` command
it should implement the following options:

these descriptions aren't comprehensive- consult the man pages or just run it yourself to see how things actually work

as of winter 24, you can only import from `argparse`, `string`, and the non-`optparse` modules already imported in the `randline` script

sanity checks

my friends and i found a ton of edge cases that you might wanna test, since the default `shuf` has some behavior that might differ a bit from `argparse`

```bash shuf -e asdf -r # should output "asdf" forever shuf asdf -i 6-10 # should error, since 6-10 was already given as an input range shuf thing.txt what.txt # ditto, except this time the input file was already given shuf -i -1-10 # this errors, you should only handle nonnegative integers for -i shuf -e -asdf # you'd think this'd print "-asdf". nope! it errors ```

`notes.txt` questions part 2

  1. what happens when your script is invoked with python 2 instead of python 3?
  2. according to python, 3.11 is significantly faster than older versions; let's test it out! time your shuf script like so: ```bash time [executable] < /usr/share/unicode/ucd/BidiTest.txt > /dev/null ``` where `[executable]` can be one of three things: for each command, time it three times, and report the median of the sum of the user & system times
  3. well? is 3.11 faster? if your program can't run on the given pre-3.11 version, explain why
    also document the cpu model and os this crap was run on by running `lscpu` and looking at `/etc/os-release` respectively

what to turn in