VehiclesFashionRecipesBlogsHuntTravelsSportFunHandmadeITEducation
Mini-Games
x

x
zakruti.com » IT - Software » freeCodeCamp.org
Jolly Jumper - The Daily Programmer #311

Jolly Jumper - The Daily Programmer #311

FBTwitterReddit

video description

Rating: 4.0; Vote: 1
This problem was found here: https://www.reddit.com/r/dailyprogrammer/comments/65vgkh/20170417_challenge_311_easy_jolly_jumper/ Cody Seibert on Chris: Some complicated stuff in what I do here, but I wanted to see if I could make a shorter version of this that performs better. At least I assume it performs better.
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

Comments and reviews: 3


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

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

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