Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active August 29, 2015 14:08
Show Gist options
  • Save kulicuu/86e55ccb5a9d33b1610b to your computer and use it in GitHub Desktop.
Save kulicuu/86e55ccb5a9d33b1610b to your computer and use it in GitHub Desktop.
array manipulation exercises
# some array exercises around the codility exercise
c= console.log
# functions:
# 0- check if order is non decreasing
# 1- check if array can be made non-decreasing with just one swap. (or less)
# 2- getAll repeated element substrings (one black sheep allowed). this may not be helpful for finding (1), but it's a good exercise in itself
# possible algos approaches for finding (1)
# - if array small then could just go through all combos blindly and check if order is good. slightly better to only go through those swaps which transfer large to back of array and small to front.
checkIs_nonDecreasing= (a)->
fennec= off
for i in [1 .. (a.length - 1)]
if a[i] < a[i - 1]
fennec= on
if fennec is off then return true else return false
checkHasElement= (a, e)->
itHas= false
i= 0
while (itHas is false) and (i < a.length)
if a[i] is e then itHas= true
i++
return itHas
getAll_repeatedElements= (a)-> # just returns elements that are repeated, and their indices
popTart= []
kast= (element, indices)->
element: element
indices: indices
elementsAlreadyFoundRepeating= []
for i in [0 .. (a.length - 2)]
if (checkHasElement(elementsAlreadyFoundRepeating, a[i])) is false
cursor= a[i]
indices= [i]
for j in [(i + 1) .. (a.length - 1)]
if a[j] is cursor
if not(checkHasElement(elementsAlreadyFoundRepeating, cursor)) then elementsAlreadyFoundRepeating.push cursor
indices.push j
if (indices.length > 1)
kate= new kast cursor, indices
popTart.push kate
return popTart
getAll_repeatedElementSubstrings__allowOne_blackSheep= (a)->
paow= (iL)-> #iL: indicesList
# the point of paow is to return a list of lists of indices where each list has transitive connectedness
listOfNeighborsLists= []
# given an indices list, return all substrings (sets where there is some path every element to every other element, edges being defined by interval 1 between indices)
for i in [0 .. (iL.length - 1)]
# want to find elements 'bordering' this one, and add them to a neighbors list, where every element has at least some transitive route to every other .
# but first want to check if any of those immediate neighbors are already in a list, then can add to that list
# so, first, find the immediate neighbors:
immedNeighbors= []
for j in [0 .. (iL.length - 1)]
if Math.abs(iL[i] - iL[j]) is 1
immedNeighbors.push iL[j]
if immedNeighbors.length > 0
already= false
for neighborsList in listOfNeighborsLists
for member in immedNeighbors
if (checkHasElement(neighborsList, member))
already= true
for member2 in immedNeighbors
if checkHasElement(neighborsList, member2) is false
neighborsList.push member2
# firsst assume their issntalready one
if already is false
listOfNeighborsLists.push immedNeighbors
stabel= getAll_repeatedElements(a)
for kate in stabel
swapCheck_algo0= (a)->
c "you found an empty swapcheck function on algo0 and invoked it with #{a}"
testCases= [
[1, 2, 3]
[3, 3, 4]
[5, 3, 1]
[1, 3, 5, 3, 7]
]
for item in testCases
c "case: #{item}, checkIs nonDecreasing: #{checkIs_nonDecreasing(item)}"
swapCheck_algo0 item
c "array #{item} has number 2: #{checkHasElement(item, 2)}"
c "all repeated elements of #{item}: ", getAll_repeatedElements(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment