Spaces:
Runtime error
Runtime error
Daniel Varga commited on
Commit ·
dd041b8
1
Parent(s): 751b904
heads OR tails
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import plotly.graph_objects as go
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
# N sequences, each with n coinflips.
|
| 7 |
# for each sequence, determine the length of the longest constant 0 interval.
|
| 8 |
-
def
|
| 9 |
data = np.random.binomial(size=(N, n + 2), n=1, p=0.5)
|
| 10 |
# putting 1s at both ends as barriers:
|
| 11 |
data[:, 0] = 1
|
|
@@ -21,10 +22,40 @@ def histogram(N, n):
|
|
| 21 |
return np.array(ks)
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def visualize(x):
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
fig = go.Figure(data=[go.Histogram(x=x, marker=dict(color='#7A88CC'), nbinsx=x_range)])
|
| 27 |
-
fig.update_layout(title='
|
| 28 |
return fig
|
| 29 |
|
| 30 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
+
import numba
|
| 5 |
|
| 6 |
|
| 7 |
# N sequences, each with n coinflips.
|
| 8 |
# for each sequence, determine the length of the longest constant 0 interval.
|
| 9 |
+
def histogram_vectorized(N, n):
|
| 10 |
data = np.random.binomial(size=(N, n + 2), n=1, p=0.5)
|
| 11 |
# putting 1s at both ends as barriers:
|
| 12 |
data[:, 0] = 1
|
|
|
|
| 22 |
return np.array(ks)
|
| 23 |
|
| 24 |
|
| 25 |
+
@numba.njit
|
| 26 |
+
def length_of_longest(seq):
|
| 27 |
+
longest = 0
|
| 28 |
+
current = 1
|
| 29 |
+
for j in range(1, len(seq)):
|
| 30 |
+
if seq[j - 1] == seq[j]:
|
| 31 |
+
current += 1
|
| 32 |
+
else:
|
| 33 |
+
if current > longest:
|
| 34 |
+
longest = current
|
| 35 |
+
current = 1
|
| 36 |
+
if current > longest:
|
| 37 |
+
longest = current
|
| 38 |
+
return longest
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# N sequences, each with n coinflips.
|
| 42 |
+
# for each sequence, determine the length of the longest constant 0 interval.
|
| 43 |
+
@numba.njit
|
| 44 |
+
def histogram(N, n):
|
| 45 |
+
data = np.random.binomial(size=(N, n), n=1, p=0.5)
|
| 46 |
+
ks = np.empty(N)
|
| 47 |
+
for i in range(N):
|
| 48 |
+
ks[i] = length_of_longest(data[i])
|
| 49 |
+
return ks
|
| 50 |
+
|
| 51 |
+
|
| 52 |
def visualize(x):
|
| 53 |
+
if len(x) > 0:
|
| 54 |
+
x_range = int(np.max(x) - np.min(x) + 1)
|
| 55 |
+
else:
|
| 56 |
+
x_range = 1
|
| 57 |
fig = go.Figure(data=[go.Histogram(x=x, marker=dict(color='#7A88CC'), nbinsx=x_range)])
|
| 58 |
+
fig.update_layout(title='Longest row of same flips', xaxis_title='length k', yaxis_title='count')
|
| 59 |
return fig
|
| 60 |
|
| 61 |
|