Thanks @alfmikula, it saves properly and has even been gemified. I will soon push it to gemcutter and maybe put a homesite on Rubyforge, not sure.
It can be found at http://github.com/Trevoke/SGFParser for now, get the code while it’s hot!
One can’t actually move a remote branch, but you can copy a branch and delete a branch, so…
Copy oldbranch in repo to newbranch.
git push {repo} {oldbranch}:heads/{newbranch}
Ex: git push origin foobranch:barbranch
renames foobranch to barbranch
Remove a remote branch: it’s all about the colon:
git push <remote_repo> :heads/<branch>
Example: git push origin :heads/some-branch removes some-branch from the remote repo (apparently git push origin :some-branch works as well).
This works for removing a tag as well: git push origin :sometag
Look, Ma, these are my baby steps in algorithms!
# list is the elements to be permuted
# y is the number of results desired
# z is the number of elements per result
# equalizer keeps track of who got used how many times
def constrained_permutations list, y, z
list.uniq! # Never trust the user. We want no repetitions.
equalizer = {}
list.each { |element| equalizer[element] = 0 }
results = []
# Do this until we get as many results as desired
while results.size < y
pool = []
puts pool
least_used = equalizer.each_value.min
# Find how used the least used element was
while pool.size < z
# Do this until we have enough elements in this resultset
element = nil
while element.nil?
# If we run out of "least used elements", then we need to increment
# our definition of "least used" by 1 and keep going.
element = list.shuffle.find do |x|
!pool.include?(x) && equalizer[x] == least_used
end
least_used += 1 if element.nil?
end
equalizer[element] += 1
# This element has now been used one more time.
pool << element
end
results << pool
end
return results
end
constrained_permutations [0,1,2,3,4,5,6], 6, 2
=> [[4, 0], [1, 3], [2, 5], [6, 0], [2, 5], [3, 6]]
constrained_permutations [0,1,2,3,4,5,6], 6, 2
=> [[4, 5], [6, 3], [0, 2], [1, 6], [5, 4], [3, 0]]
I don’t really have a better name for this. It’s also not completely clean, but it works. I had, almost a year ago (362 days ago), written a blog post about
lexicographic permutations. That was about permutations of elements within one array.
Someone on ruby-forum asked about permutations between multiple arrays. I
found something in C#, which I was happy to transcribe to Ruby and tweak a little.
def array_permutations array, index=0
# index is 0 by default : start at the beginning, more elegant.
return array[-1] if index == array.size - 1 # Return last element if at end.
result = []
array[index].each do |element| # For each array
array_permutations(array, index + 1).each do |x| # Permute permute permute
result << "#{element}, #{x}"
end
end
return result
end
So, we get this:
first = ['one', 'two'] second = ['three', 'four'] third = 'five', 'six'] result = array_permutations [first, second, third] => ["one, three, five", "one, three, six", "one, four, five", "one, four, six", "two, three, five", "two, thre e, six", "two, four, five", "two, four, six"]
Magic!
——
Edit – of course, my solution is hackish, and someone came up with a quicker and more elegant solution:
def fancy_array_permutation array
return array[0] if array.size == 1
first = array.shift
return first.product( fancy_array_permutation(array) ).map {|x| x.flatten.join(" ")}
end
This gives the same result as above.
I came across this beauty:
def same_modality? list
check = list[0][0]
list.each do |i|
return false if check != i[0]
end
return true
end
This was “necessary” because I got an array of one-element arrays back, and I wanted to check whether or not that one element was the same across the array.
Three seconds of thinking made me realize that just maybe, I could do this:
list.uniq.size == 1
I -like- Ruby.