I'm trying to solve the "knapsack" problem using Java and recursion
Tag : java , By : fukas78
Date : March 29 2020, 07:55 AM
Any of those help What is happening in your code is that when it gets to the last else statement, it is not removing the initial value that was put in. I made a small change to your code that may get you the results you are looking for. First, I had the recursive function return an int, which would be the capacity: public static int recknapSack(int capacity, int[] items, int branch) {
return capacity;
else {
int firstItem; // this the first item, it will either be discarded (not included) or placed in the sack array (included)
firstItem = items[0];
items = Arrays.copyOfRange(items, 1, items.length); // for either recursive branch: remove the first item from the list
recknapSack(capacity, items, 1); // call recursive function, left branch, where item is discarded and not placed in sack
// prepare for right branch, where item is placed in sack
capacity -= firstItem; // subtract the left most item weight from from capacity
int temp = pos;
sack[pos++] = firstItem; // place the item in the sack
System.out.println("First item " + firstItem);
int ret = recknapSack(capacity, items, 2); // recursive right branch call, item is placed in sack, weight subtracted from capacity
if(ret != 0)
{
System.out.println("Removing " + sack[temp] + " at position " + (temp));
sack[temp] = 0;
pos = temp;
}
}
|
perl Kafka::Producer, "Kafka::Exception::Producer", "code", -1000, "message", "Invali
Tag : perl , By : user143729
Date : March 29 2020, 07:55 AM
To fix this issue I was able to solve by wrapping my string with utf8::downgrade. Somehow it was taking my string as utf-8, and kafka producer, had check for need non utf-8 string. My working code: my $arcs_val = join( ',', @arc_a );
my $hadoop_str = $testid . ',' . $gcda_file_name . ',' . $arcs_val;
utf8::downgrade($hadoop_str);
try {
my $topic = $inputs[0];
my $partition = $inputs[1];
my $response = $producer->send(
$topic, # topic
$partition, # partition
$hadoop_str
);
|
Is "readers-writers" just "producer–consumer" with multiple consumers?
Date : March 29 2020, 07:55 AM
This might help you Readers-Writers implies that the Readers do not modify the underlying state, thus many can simultaneously access it; however because a Writer can freely modify the state, no Reader can simultaneously access it. Producer-Consumer is a common synchronization problem with two accessors: one that replenishes a resource and one that consumes it. You can’t have multiple producers or consumers simultaneously accessing it. The confusion may arise because there are many (restricted) implementations which use busy-waiting (er, transactional memory) to wrestle better performance out of this patten.
|
How to solve SQL "Unknown column 'c' in 'field list'" in JAVA while there is actually no syntax problem with i
Tag : java , By : Vitalik
Date : March 29 2020, 07:55 AM
will help you First of all, you should use prepared statements to avoid SQL injection. Your problem is that you forgot the quotes in your string columns, so your sintax is wrong. This way it should work: statement.executeUpdate("INSERT INTO customer(customer_id,age,first_name,last_name,phone_nr,passport_number)"
+ " VALUES(2000,"+customer.getAge()+",'"+customer.getFirst_name()+"', '"+
customer.getLast_name()+ "', '" + customer.getPhone_nr() +
"', '" +customer.getPassport_number() +"');");
|
producer-multi consumer problem, consumers finishes work before producer is done
Date : March 29 2020, 07:55 AM
To fix this issue You may apply Thread Event object as communication mechanism. import queue
import threading
import sys
import time
q = queue.Queue(maxsize=0)
class Producer():
def __init__(self, q):
self.q = q
print("Producer")
sys.stdout.flush()
def generate_item(self, event):
t_name = threading.current_thread().name
for i in range(1, 10):
print(f"{t_name} generated item {i}")
sys.stdout.flush()
self.q.put(i)
time.sleep(0.5)
print(f'*** producer {t_name} finished work')
sys.stdout.flush()
event.set()
class Consumer():
def __init__(self, q):
print("Consumer")
sys.stdout.flush()
self.q = q
def consume_item(self, event):
t_name = threading.current_thread().name
while True:
if not self.q.empty():
try:
val = self.q.get(timeout=0.5)
except queue.Empty:
if event.is_set():
break
else:
print(f"{t_name} consumed item : {val}")
sys.stdout.flush()
elif event.is_set():
break
print(f'*** consumer {t_name} finished work')
sys.stdout.flush()
producer = Producer(q)
consumer = Consumer(q)
e = threading.Event()
threads = []
threads.append(threading.Thread(target=producer.generate_item, args=(e,), name="thread1"))
threads.append(threading.Thread(target=consumer.consume_item, args=(e,), name="thread2"))
threads.append(threading.Thread(target=consumer.consume_item, args=(e,), name="thread3"))
threads.append(threading.Thread(target=consumer.consume_item, args=(e,), name="thread4"))
for thread in threads:
thread.start()
#
# for thread in threads:
# thread.join()
Producer
Consumer
thread1 generated item 1
thread2 consumed item : 1
thread1 generated item 2
thread3 consumed item : 2
thread1 generated item 3
thread4 consumed item : 3
thread1 generated item 4
thread4 consumed item : 4
thread1 generated item 5
thread2 consumed item : 5
thread1 generated item 6
thread2 consumed item : 6
thread1 generated item 7
thread4 consumed item : 7
thread1 generated item 8
thread4 consumed item : 8
thread1 generated item 9
thread2 consumed item : 9
*** producer thread1 finished work
*** consumer thread3 finished work
*** consumer thread2 finished work
*** consumer thread4 finished work
|