alphapapa's recent activity

  1. Comment on Programming Challenge - It's raining! in ~comp

    alphapapa
    (edited )
    Link Parent
    The explanations help, but why not just name the variables more descriptively? e.g. instead of saying "t is the time variable," why not just name it time? Instead of "load is basically spill," why...

    The explanations help, but why not just name the variables more descriptively? e.g. instead of saying "t is the time variable," why not just name it time? Instead of "load is basically spill," why not name it spillover?

    And instead of, "Create new list 'b' with the same length as 'a' and fill it with zeros," why not give b a descriptive name and explain the purpose? Everyone who knows Python can see that it's making a new list and filling it with zeroes, but what's the purpose of doing that?

    Do you see what I mean? :)

    BTW, the comments and code would be easier to follow if you put the comments in the code rather than beside it. It helps visualize the structure and break the code into parts, like headings in a long document.

    One Python-related suggestion: for i in range(len(x)) is not idiomatic Python. Python isn't C. :) Try for lake in lakes, and look up Iterators. ;)

    1 vote
  2. Comment on Programming Challenge - It's raining! in ~comp

    alphapapa
    Link Parent
    It would be much easier to understand the code if the variables were named descriptively.

    It would be much easier to understand the code if the variables were named descriptively.

    4 votes
  3. Comment on Programming Challenge - It's raining! in ~comp

    alphapapa
    Link
    Here's a way to do it in Emacs Lisp: (defstruct lake quantity capacity) (defun rainfall (rainfall &rest lakes) "Return number of rainfall periods required to apply RAINFALL to LAKES until no...

    Here's a way to do it in Emacs Lisp:

    (defstruct lake
      quantity capacity)
    
    (defun rainfall (rainfall &rest lakes)
      "Return number of rainfall periods required to apply RAINFALL to LAKES until no capacity remains."
      (cl-loop for remaining-capacity = (apply #'fill-lakes rainfall 0 lakes)
               collect (-map #'copy-lake lakes) into periods
               until (<= remaining-capacity 0)
               finally return (list :hours (length periods)
                                    :periods periods
                                    :remaining-capacity remaining-capacity)))
    
    (defun fill-lakes (rainfall overflow &rest lakes)
      "Return remaining capacity in LAKES after distributing RAINFALL and OVERFLOW."
      (-let* (((lake . lakes) lakes)
              (remaining-capacity (- (lake-capacity lake) (lake-quantity lake)))
              (new-water (+ rainfall overflow))
              (overflow (max 0 (- new-water remaining-capacity))))
        (cl-incf (lake-quantity lake) (- new-water overflow))
        (cl-decf remaining-capacity (- new-water overflow))
        (if lakes
            (+ (apply #'fill-lakes rainfall overflow lakes) remaining-capacity)
          remaining-capacity)))
    
    (rainfall 1
              (make-lake :capacity 1 :quantity 0)
              (make-lake :capacity 3 :quantity 0)
              (make-lake :capacity 5 :quantity 0))
    ;; =>
    ;; (list :hours 3
    ;;       :periods ((#s(lake 1 1)
    ;;                    #s(lake 1 3)
    ;;                    #s(lake 1 5))
    ;;                 (#s(lake 1 1)
    ;;                    #s(lake 3 3)
    ;;                    #s(lake 2 5))
    ;;                 (#s(lake 1 1)
    ;;                    #s(lake 3 3)
    ;;                    #s(lake 5 5)))
    ;;       :remaining-capacity 0)
    
    5 votes