Y'all are surprising me with your programming opinions

Then again, JavaScript or whatever apparently has a Math.max function, while C doesn't

@wallhackio Are you referring to yourself or to me or to us all?

@vaporeon_ I'm referring to anyone who writes code for money instead of writing code for fun

@vaporeon_ this is not a slight on Holly. i hope that is clear. she does code golf so you know she actually likes coding for the hell of it

@wallhackio @vaporeon_ oh yeah all this goes out the window when you're golfing. sometimes it's fun to let loose and write some horseshit

@monorail @vaporeon_ my worst code practices occur when i hack a solution to a challenging data structures/algorithms problem

@wallhackio How can you have bad code practices when there's no such thing as good Java code, it's all terrible

@vaporeon_ I've done DS&A problems in JavaScript and written some nightmarish oneliners doing that

@vaporeon_ I know how this is gonna go. I will show it to you and then you'll be like, wait this is cool I like it :)

@vaporeon_ here is another hack with a "beautiful" one liner:

Problem Statement
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].

Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:

  • 0 <= j <= nums[i] and
  • i + j < n

Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

Example

  • Input: nums = [2,3,1,1,4]
  • Output: 2
  • Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Code

const jump = (nums, i = 0, gorp = {}) => {
if (i === nums.length - 1) return 0;
if (i > nums.length - 1 || nums[i] === 0) return Infinity;
if (gorp[i] !== undefined) return gorp[i];

let min = Infinity;
for (let j = 1, res; j <= nums[i]; ++j) res = 1 + jump(nums, i + j, gorp), min = res < min ? res : min;

return gorp[i] = min;
};

@vaporeon_ oooooh I could have done for (var min = Infinity, j = 1, res; j <= nums[i]; ++j) res = 1 + jump(nums, i + j, gorp), min = res < min ? res : min;

javascript variables declared with var and not block scoped, so min is available outside of the for loop teehee

@wallhackio @vaporeon_ if you look into the semantic diffurences between for with let and var scoped variables, this is not surpurrising; the furmer is surpurrisingly complicated. const allows fur some optimizations over let though iirc

@aescling @vaporeon_ with let the algo ran the test cases ~700ms and with var it shaved ~100ms

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.