diff --git a/.gitignore b/.gitignore
index edc7dfb0379b2098a3da19cefea7998bbd876814..99dc3fb80575d6f3a8b8d1ecde3b3bf93b908250 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
*~
*.pyc
*.pyo
+__pycache__
.idea
diff --git a/fuzzing/fuzz_find_smallest_positive.py b/fuzzing/fuzz_find_smallest_positive.py
index 346c410e908db9d556ef1ba32baa2c6cf947ce8b..6cea2c0b139740291ffcb61cfa501d250f0f6c74 100644
--- a/fuzzing/fuzz_find_smallest_positive.py
+++ b/fuzzing/fuzz_find_smallest_positive.py
@@ -1,6 +1,7 @@
from math import inf
from random import randrange, choice
+
SUITE_TEMPLATE = '''
from math import inf
from unittest import TestCase
@@ -37,7 +38,7 @@ def generate_padding(smallest_positive):
def generate_random_test(number):
- smallest_positive = choice((randrange(VALUE_LIMIT), inf))
+ smallest_positive = choice((randrange(1, VALUE_LIMIT), inf))
inputs = [smallest_positive] # TODO
return TEST_TEMPLATE.format(number=number, inputs=inputs, expected=smallest_positive)
diff --git a/main.py b/main.py
index 17cd025d7e12203ea9ae443b65e15ea3d08ef416..605d4a844f927c28dae9032de7a94478276310b8 100644
--- a/main.py
+++ b/main.py
@@ -2,42 +2,59 @@ from math import inf
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
-from kivy.properties import NumericProperty, StringProperty
+from kivy.properties import NumericProperty, StringProperty, ListProperty
class Field(BoxLayout):
- label_text = StringProperty()
+ index = NumericProperty()
+ entry = StringProperty()
+
+
+class Fields(BoxLayout):
+ entries = ListProperty([]) # elements are strings (some may not be valid numbers)
+
+ def rebuild(self):
+ while len(self.children) > len(self.entries):
+ self.remove_widget(self.children[0])
+ while len(self.children) < len(self.entries):
+ self.add_widget(Field(index=len(self.children)))
+
+ def on_entries(self, _, __):
+ self.rebuild()
+ for field, entry in zip(reversed(self.children), self.entries):
+ field.entry = entry
class SmallestPositiveApp(App):
- smallest_positive = NumericProperty(inf)
+ entries = ListProperty([]) # elements are strings (some may not be valid numbers)
+ result = NumericProperty(inf)
def add_field(self):
- container = self.root.ids.fields
- container.add_widget(Field(label_text=f'Field #{len(container.children)}: '))
+ self.entries.append('')
def remove_field(self):
- container = self.root.ids.fields
- if len(container.children) > 0:
- container.remove_widget(container.children[0])
+ try:
+ self.entries.pop()
+ except IndexError:
+ pass
@staticmethod
- def _find_smallest_positive(values):
+ def _find_smallest_positive(entries):
smallest_positive = inf
- for value in values:
- if 0 < value < smallest_positive:
- smallest_positive = value
- return smallest_positive
-
- def update(self):
- container = self.root.ids.fields
- values = []
- for field in container.children:
+ for entry in entries:
try:
- values.append(float(field.ids.input.text))
+ value = float(entry)
+ if 0 < value < smallest_positive:
+ smallest_positive = value
except ValueError:
pass
- self.smallest_positive = SmallestPositiveApp._find_smallest_positive(values)
+ return smallest_positive
+
+ def on_entries(self, _, __):
+ self.result = SmallestPositiveApp._find_smallest_positive(self.entries)
+
+ def set_entry(self, index, entry):
+ self.entries[index] = entry
if __name__ == '__main__':
diff --git a/smallestpositive.kv b/smallestpositive.kv
index 2ea9ff20a05bb1d215e651cb30212201d9476132..dd39ccf52d050f39b104f068ef0c5ab9fb15da2c 100644
--- a/smallestpositive.kv
+++ b/smallestpositive.kv
@@ -1,8 +1,7 @@
BoxLayout:
orientation: 'vertical'
- BoxLayout:
- id: fields
- orientation: 'vertical'
+ Fields:
+ entries: app.entries
size_hint: (1.0, 6.0)
BoxLayout:
orientation: 'horizontal'
@@ -15,18 +14,19 @@ BoxLayout:
font_size: sp(24)
on_press: app.remove_field()
Label:
- id: result
- text: f'Smallest Positive: {app.smallest_positive}'
+ text: f'Smallest Positive: {app.result}'
font_size: sp(24)
+<Fields>:
+ orientation: 'vertical'
+
<Field>:
orientation: 'horizontal'
Label:
- text: root.label_text
+ text: f'Field #{root.index}: '
font_size: sp(24)
TextInput:
- id: input
multiline: False
write_tab: False
font_size: sp(24)
- on_text: app.update()
+ on_text: app.set_entry(root.index, self.text)