I'm pushing jobs like this:
Sidekiq::Client.push('class' => StripeDataWorker, 'args' => [account.id, 'w', 'BalanceTransaction'], 'queue' => 'w')
But they just sit in Enqueued and never get processed:
This happens to be on Heroku. I've restarted sidekiq many times and other jobs on other non-pushed queues are running without issue.
Here is my sidekiq.yml file:
:concurrency: 28
:queues:
- [fast, 10]
- [default, 5]
- [slow, 1]
- [generate, 2]
- [a, 3]
- [b, 3]
- [c, 3]
- [d, 3]
- [e, 3]
- [f, 3]
- [g, 3]
- [h, 3]
- [i, 3]
- [j, 3]
- [k, 3]
- [l, 3]
- [m, 3]
- [n, 3]
- [o, 3]
- [p, 3]
- [q, 3]
- [r, 3]
- [s, 3]
- [t, 3]
- [u, 3]
- [v, 3]
- [w, 3]
- [x, 3]
- [y, 3]
- [z, 3]
Related
I want to generate all pairs of all the numbers of a range 1..N in all orders i.e. [X,Y] and [Y,X]
I found these here : http://kti.ms.mff.cuni.cz/~bartak/prolog/combinatorics.html
comb(0,_,[]).
comb(N,[X|T],[X|Comb]):-N>0,N1 is N-1,comb(N1,T,Comb).
comb(N,[_|T],Comb):-N>0,comb(N,T,Comb).
comb_rep(0,_,[]).
comb_rep(N,[X|T],[X|RComb]):-N>0,N1 is N-1,comb_rep(N1,[X|T],RComb).
comb_rep(N,[_|T],RComb):-N>0,comb_rep(N,T,RComb).
?- findall(E1,comb_rep(2,[1,2,3],E1),C1),findall(E2,comb(2,[3,2,1],E2),C2),append(C1,C2,C),write(C).
[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3],[3,2],[3,1],[2,1]]
There have to be an easier way ?
Also i would prefer to do it as GENERATOR, rather than a FULLY rendered list (like the ex above)
I suspect I am missing something because this does so much less than the code you posted, but this seems to create the pairs how you ask:
limit_pairs(N, [X, Y]) :-
between(1, N, X),
between(1, N, Y).
e.g.
?- limit_pairs(3, P).
P = [1, 1]
P = [1, 2]
P = [1, 3]
P = [2, 1]
P = [2, 2]
P = [2, 3]
P = [3, 1]
P = [3, 2]
P = [3, 3]
This is the original link for the problem in hackerrank: https://www.hackerrank.com/challenges/the-birthday-bar/problem
I have been fighting with this problem in Ruby and I don't know why my counter always returns 1. This is the solution. I hope you can help me to understand what I'm making wrong.
s = [1, 2, 1, 3, 2]
d = 3
m = 2
def birthday(s, d, m)
array = []
cont = 0
sum = 0
m.times {array.push(s.shift)}
(m-1).times do
array.each {|i| sum = sum + i}
if sum == d
cont += 1
end
array.shift
array.push(s.shift)
end
return cont
end
birthday(s, d, m)
Though the following does not answer your question directly, it is a Ruby-like way of solving the problem, especially by making use of the methods Enumerable#each_cons and Enumerable#count.
def birthday(s, d, m)
s.each_cons(m).count { |a| a.sum == d }
end
s = [1, 2, 1, 3, 2]
d = 3
m = 2
birthday(s, d, m)
#=> 2 ([1, 2] and [2, ])
s = [2, 2, 1, 3, 2]
d = 4
m = 2
birthday(s, d, m)
#=> 2 ([2, 2] and [1, 3])
s = [2, 4, 3, 2, 1, 2, 6, 1]
d = 9
m = 3
birthday(s, d, m)
#=> 4 ([2, 4, 3], [4, 3, 2], [1, 2, 6] and [2, 6, 1])
Notice from the doc that when each_cons is used without a block it returns an enumerator:
s = [1, 2, 1, 3, 2]
d = 3
m = 2
enum = s.each_cons(m)
#=> #<Enumerator: [1, 2, 1, 3, 2]:each_cons(2)>
enum will generate elements and pass them to count until there are no more to generate, at which time it raises a StopIteration exception:
enum.next #=> [1, 2]
enum.next #=> [2, 1]
enum.next #=> [1, 3]
enum.next #=> [3, 2]
enum.next #=> StopIteration (iteration reached an end) <exception>
We can write1:
enum.count { |a| a.sum == d }
#=> 2
After enum generates the first value ([1, 2]) the block variable a is assigned its value:
a = enum.next
#=> [1, 2]
and the block calculation is performed. As
a.sum == d
#=> [1, 2].sum == 3 => true
the count is incremented (from zero) by one. enum then passes each of its remaining values to count and the process is repeated. When, for example, [1, 3].sum == 3 => false is executed, the count is not incremented.
1. Note that since I just stepped through all the elements of enum, enum.next would generate another StopIteration exception. To execute enum.count { |a| a.sum == d } I therefore must first redefine the enumerator (enum = s.each_cons(m)) or Enumerator#rewind it: enum.rewind.
I find a lot of reference about removing duplicates in ruby but I cannot find how to create duplicate.
If I have an array like [1,2,3] how can I map it to an array with dubbed items? [1,1,2,2,3,3]
Is there a method?
Try this one
[1, 2, 3].flat_map { |i| [i, i] }
=> [1, 1, 2, 2, 3, 3]
Here's yet another way, creating the array directly with Array#new :
array = [1, 2, 3]
repetitions = 2
p Array.new(array.size * repetitions) { |i| array[i / repetitions] }
# [1, 1, 2, 2, 3, 3]
According to fruity, #ursus's answer, #ilya's first two answers and mine have comparable performance. transpose.flatten is slower than any of the others.
#Ursus answer is the most clean, there are possible solutions:
a = [1, 2, 3]
a.zip(a).flatten
#=> [1, 1, 2, 2, 3, 3]
Or
a.inject([]) {|a, e| a << e << e} # a.inject([]) {|a, e| n.times {a << e}; a}
=> [1, 1, 2, 2, 3, 3]
Or
[a, a].transpose.flatten # ([a] * n).transpose.flatten
=> [1, 1, 2, 2, 3, 3]
Try this:
[1, 2, 3] * 2
=> [1, 2, 3, 1, 2, 3]
You might want it sorted:
([1, 2, 3] * 2).sort
=> [1, 1, 2, 2, 3, 3]
Say, I have an array:
a = [1,2]
and
n = 3
I want output like this:
[[1, 1, 1], [1, 1, 2], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 1], [2, 2, 2]]
This are all possible combinations of length n of elements from array a.
Most importantly I'm using ruby 1.8.7
a.repeated_combination(n).to_a
Please test in detail before use:
x = [1,0]
n = 3
def perm(a, n)
l = a.length
(l**n).times do |i|
entry = []
o = i
n.times do
v = o % l
entry << a[v]
o /= l
end
yield(i, entry)
end
end
perm(x, n) do |i, entry|
puts "#{i} #{entry.reverse.inspect}"
end
prints
0 [0, 0, 0]
1 [0, 0, 1]
2 [0, 1, 0]
3 [0, 1, 1]
4 [1, 0, 0]
5 [1, 0, 1]
6 [1, 1, 0]
7 [1, 1, 1]
I haven't been able to discard repetition permutations.For example, I want to get
?-permutation([1,2,1],L).
L = [1, 2, 1] ;
L = [1, 1, 2] ;
L = [2, 1, 1] ;
.
but i am getting
?-permutation([1,2,1],L).
L = [1, 2, 1] ;
L = [1, 1, 2] ;
L = [2, 1, 1] ;
L = [2, 1, 1] ;
L = [1, 1, 2] ;
L = [1, 2, 1] ;
.
I think the simpler way could be
perm_no_rep(L, P) :-
setof(P, permutation(L, P), Ps),
member(P, Ps).
it's almost ok:
?- perm_no_rep([1,2,1],P).
P = [1, 1, 2] ;
P = [1, 2, 1] ;
P = [2, 1, 1].
beware that will be factorial(Num_Distinct_Elements_in_L) = length(Ps) i.e. possibly very large lists.