## Algorithm for competing cells of 0s and 1s. Can it be made faster?

I’m working on a practice algorithm problem, stated as follows:

There are eight houses represented as cells. Each day, the houses compete with adjacent ones. 1 represents an “active” house and 0 represents an “inactive” house. If the neighbors on both sides of a given house are either both active or both inactive, then that house becomes inactive on the next day. Otherwise it becomes active. For example, if we had a group of neighbors [0, 1, 0] then the house at [1] would become 0 since both the house to its left and right are both inactive. The cells at both ends only have one adjacent cell so assume that the unoccupied space on the other side is an inactive cell.

Even after updating the cell, you have to consider its prior state when updating the others so that the state information of each cell is updated simultaneously.

The function takes the array of states and a number of days and should output the state of the houses after the given number of days.

``Examples:      input: states = [1, 0, 0, 0, 0, 1, 0, 0], days = 1     output should be [0, 1, 0, 0, 1, 0, 1, 0]     input: states = [1, 1, 1, 0, 1, 1, 1, 1], days = 2     output should be [0, 0, 0, 0, 0, 1, 1, 0] ``

My solution:

``def cell_compete(states, days):     for _ in range(days):         prev_cell, next_cell, index = 0, 0, 0         while index < len(states):             if index < len(states) - 1:                 next_cell = states[index + 1]             elif index == len(states) - 1:                 next_cell = 0             if next_cell == prev_cell:                 prev_cell = states[index]                 states[index] = 0             else:                 prev_cell = states[index]                 states[index] = 1             index += 1     return states ``

I originally thought to take advantage of the fact they are 0s and 1s only and to use bitwise operators, but couldn’t quite get that to work.

Can I improve the efficiency of this algorithm? Any ideas on the “optimal” solution?

Appreciate any feedback, thanks!

by: Eyasin9
Created: —
Viewed: 253

by: HugeGorilla
Created: —
Viewed: 252

## How to run Ubuntu faster?

I installed Ubuntu 19.04 and it’s running a lot slower than Windows 7. I have 7.5Gib on an AMD Athlon ii x2 250 processor, and 64 bit.

I still have Windows 7 on my pc and I went back and Windows still runs a lot faster than Ubuntu. Please help!

by: CrushSEO
Created: —
Viewed: 179

## 1000 Worldwide traffic cheap and faster delivery for \$1

by: seogabriel
Created: —
Category: Traffic
Viewed: 87

## How to find a search algorithm that is faster than O(n) time

Assume a 1-indexed array A[1…n], containing integers, where A[1] ≤ A[n]. The contents of the array have the following property : for 1 ≤ i < n, |A[i] − A[i + 1]| ≤ 1.

Develop an efficient recursive algorithm to find j such that A[j] = z, where A[1] ≤ z ≤ A[n]. The algorithm must be more efficient that O(n).

## VideoCapture.read() using OpenCV. Why is USB Camera faster than CSI Camera? (NO GPU)

I am using NanoPi Duo 2 for the real-time image acquisition project.

I have noticed a significant performance (speed) difference between using CSI camera and USB camera.

The performance difference is as follows, Time for OpenCV VideoCapture.read()

``CSI_OV5640_Camera = ~0.04s (40 ms) USB_Logitech_HD_C270 = ~0.009 (9 ms) ``

As far as I know, I understand that NanoPi Duo2 does not have any GPU and CSI camera will be handled by CPU (Same as USB Camera).

Using `\$ htop` both CSI and USB cameras shows 100% @ one of 4 cores.

For background,

OpenCV 3.4.6 Build Output

``Video I/O  - libv4l/libv4l2    NO  - v4l/v4l2          linux/videodev2.h ``

\$ v4l2-ctl –get-fmt-video

``Format Video Capture:         Width/Height      : 640/480         Pixel Format      : 'YV12'         Field             : Any         Bytes per Line    : 960         Size Image        : 460800         Colorspace        : Default         Transfer Function : Default         YCbCr Encoding    : Default         Quantization      : Default         Flags             : ``

One interesting thing Performance of using `numpy ndarray` from CSI and USB are ALSO different

``pyzbar.decode() calculation CSI_OV5640_Camera = ~0.43s (430 ms) USB_Logitech_HD_C270 = ~0.19s (190 ms) ``

I am thinking that encoding of the VideoCapture.read() are different but all I see is same sized (640×480, 3 colors) numpy ndarray with similar looking values.

Thank you for reading my question.

## Effect of killing Gimp to increase faster closing of program

Despite my saving changes to images, Gimp insists on opening a dialog every time I want to leave.

What would be the effect of killing Gimp to exit pronto?

Thanks.

## What is the smallest value of n such that an algorithm running at 100*n^2 operates faster than 2^n ? [How to figure out without brute force]

Okay, so I needed to find the smallest value of n such that algorithm 100*n^2 is faster than 2^n.

[what I have tried]

So, I instantly thought ‘0’. But, I then realized it can’t be 0, 0 implies that there are no digits being entered in the algorithm, it also implies that the program does not run or terminate.

I typed in 2^32 and got a number over 4 billion. Okay, this is good, I’m finding numbers that have 100*n^2 being faster than 2^n.

I halved that, n = 20.

I kept inserting values counting down until I got to n = 15.

I also counted up from n = 10, the answer is undoubtedly n = 15, but I have a problem . . . .I solved this using brute force and that isn’t good. What if I was given a bigger number and a larger bredth of numbers?

[What I need]

I need a way of finding the value instantaneously by only doing the math, I tried using logarithms, but my answer was wrong, my knowledge of logs is a bit rusty and I need a little help.

Think of it as a student trying to solve a question on an SAT or having a timer for a test.