
Jolly Jumper - The Daily Programmer #311
video description
Arrays don't perform very well with adding/removing items.
function isJollyJumper(nums) -
// nums.keys() returns an Array Iterator with keys for nums array (0... length -1)
// [...] is the spread operator, turning an iterator into a regular array
// .slice(1) removes the first item (0) from the array and returns the new array (shift would return the removed item)
const set = new Set([...nums.keys()].slice(1));
// Array.every checks that the given callback returns true for every item in the array
// nums.slice(0, -1) returns nums with the last item removed
return nums.slice(0, -1).every((num, index) =>
// Set.delete() returns whether or not an item was removed, so it also checks if Set.has() (or, in this case, had)
set.delete(Math.abs(num - nums[index + 1]))
);
-
Date: 2022-03-14
Related videos
Comments and reviews: 3
Gurmeet
python solution for the question, if it could be made more pythonic then do tell....
def is_jolly(arr):
s=[i for i in range(1,len(arr))]
for i in range(len(arr)-1):
if abs(arr[i]-arr[i+1]) in s:
s.remove(abs(arr[i]-arr[i+1]))
else:
return False
if not s:
return True
return False
print(is_jolly([1,4,2,3]))
reply
python solution for the question, if it could be made more pythonic then do tell....
def is_jolly(arr):
s=[i for i in range(1,len(arr))]
for i in range(len(arr)-1):
if abs(arr[i]-arr[i+1]) in s:
s.remove(abs(arr[i]-arr[i+1]))
else:
return False
if not s:
return True
return False
print(is_jolly([1,4,2,3]))
reply
Amf
I dont know why nobody notices this but the algorithm is false, the input is from the problem statement sample and it was supposed to be a JOLLY however on paper he says jolly when he tried to code it he reached a conclusion that it is NOT JOLLY instead of there is something wrong with the algorithm
let me know if i missed something
reply
I dont know why nobody notices this but the algorithm is false, the input is from the problem statement sample and it was supposed to be a JOLLY however on paper he says jolly when he tried to code it he reached a conclusion that it is NOT JOLLY instead of there is something wrong with the algorithm
let me know if i missed something
reply
KozmicLuis
In clojure (lisp for the JVM):
(defn is-jolly-jumper? [numbers]
(let [pair-differences (->> numbers (partition 2 1) (map #(Math/abs (apply - %))) set)
range-set (->> numbers (count 1) set)]
(= pair-differences range-set)))
reply
In clojure (lisp for the JVM):
(defn is-jolly-jumper? [numbers]
(let [pair-differences (->> numbers (partition 2 1) (map #(Math/abs (apply - %))) set)
range-set (->> numbers (count 1) set)]
(= pair-differences range-set)))
reply
Add a review, comment
Other channel videos















