Commits

ronald martinez committed 9a4b519

init apps

Comments (0)

Files changed (6)

+class ISBN(object):
+
+    _database = []
+
+    @property
+    def database(self):
+        return self._database
+
+    def __init__(self, database=None):
+
+        if database:
+            self.__read_file(database)
+
+    def check(self, value):
+
+        is_correct = False
+        value = value.strip()
+
+        if len(value) > 9 or len(value) < 19:
+
+            has_x = True if value[-1].lower() == 'x' else False
+            values = []
+            status = False
+
+            for n in value:
+                if n.isdigit():
+                    values.append(int(n))
+
+            if len(values) == 9:
+                if has_x:
+                    values.append(10)
+                    status = True
+                else:
+                    status = False
+
+            elif len(values) == 10:
+                if has_x:
+                    status = False
+                else:
+                    status = True
+
+            if status:
+
+                sub1 = []
+                for x, y in enumerate(values):
+                    sub1.append(y + sum(values[:x]))
+
+                sub2 = []
+                for x, y in enumerate(sub1):
+                    sub2.append(y + sum(sub1[:x]))
+
+                last_value = sub2[-1]
+
+                t = float(last_value) / 11
+
+                if last_value == 0 or t.is_integer():
+                    is_correct = True
+
+        return is_correct
+
+    def __read_file(self, ins):
+
+        ins = open(ins, 'r')
+
+        for line in ins:
+            line = line.replace('\n', '')
+            self._database.append(line.strip())
+
+
+isbn = ISBN(
+    database='input-control-isbn.txt'
+)
+
+#print isbn.check('0-13-162959-X')
+
+for value in isbn.database:
+    print value, 'es correcto' if isbn.check(value) else 'es incorrecto'
+class Figure(object):
+
+    _database = []
+
+    @property
+    def database(self):
+        return self._database
+
+    def __init__(self, database=None):
+
+        if database:
+            self.__read_file(database)
+
+    def grid(self, deep):
+
+        _grid = []
+
+        for i in range(1, deep):
+
+            _i = i
+
+            i = _i + sum(range(1, _i - 1))
+            j = _i + sum(range(1, _i))
+
+            diff = j - i
+
+            if diff > 0:
+
+                t2 = range(i + 1, j)
+
+                if t2:
+
+                    t2.insert(0, i)
+                    t2.insert(len(t2), j)
+
+                    _grid.append(t2)
+
+                else:
+                    _grid.append([i, j])
+            else:
+                _grid.append([i])
+
+        return _grid
+
+    def isolation(self, points):
+
+        g = []
+        data = []
+
+        points.sort()
+
+        _minn = min(points)
+        _maxx = max(points)
+
+        _grid = self.grid(10)
+
+        for p in _grid:
+
+            short_points = []
+
+            for p2 in points:
+
+                if p2 in p:
+
+                    short_points.append(p2)
+
+            g = g + short_points
+
+            if g:
+
+                if short_points:
+                    _min = min(short_points)
+                    _max = max(short_points)
+                else:
+                    _min = 0
+                    _max = 0
+
+                data.append([p, (_min, _max)])
+
+                if _maxx in g:
+                    break
+
+        return dict(data=data, minn=_minn, maxx=_maxx)
+
+    def find(self, points):
+
+        _points = points
+        is_figure = False
+        is_par = (float(len(points)) / 2).is_integer()
+        size_points = len(points)
+        figure = None
+
+        if is_par:
+            if size_points == 4:
+
+                figure = 'parallelogram'  # can be parallelogram
+            elif size_points == 6:
+
+                figure = 'hexagon'
+        else:
+            figure = 'triangule'
+
+        # triangule: 3 sides
+        # parallelogram: 4 sides
+        # hexagon: 6 sides
+        # triangule: inpar
+        # parallelogram: cuadrado, is_rombo, is_romboide, rectangulo
+        # hexagon:
+
+        _isolation = self.isolation(points)
+
+        data = _isolation.get('data')
+
+        i = 0
+        n = 0
+
+        tot = 0
+        tot_left = 0
+
+        top = True
+        is_rombo = False
+
+        ok_middle = False
+        ok_button = False
+
+        triangule_one_side = False
+
+        for points, vertices in data:
+
+            if figure == 'parallelogram':
+
+                if i == 0:
+
+                    tot_left = points.index(vertices[0])
+
+                    if vertices[0] == vertices[1]:
+                        is_rombo = True
+                    else:
+                        tot_right = points.index(vertices[1])
+                        tot = tot_right - tot_left
+
+                if is_rombo:
+
+                    if top:
+
+                        if (i == 0) or not any(vertices):
+                            n += 1
+                        else:
+
+                            if points[tot_left] == vertices[0] and \
+                                    points[tot_left + n] == vertices[1]:
+                                ok_middle = True
+                            top = False
+
+                    else:
+
+                        fin = n * 2
+
+                        if i >= fin:
+                            if i == fin:
+
+                                if vertices[0] == vertices[1]:
+                                    middle_button = tot_left + n
+
+                                    if points[middle_button] == vertices[0]:
+                                        ok_button = True
+                                        break
+                                    else:
+                                        ok_button = False
+                                        break
+                            else:
+                                ok_button = False
+                                break
+
+                else:
+
+                    if i == tot:
+                        middle = points[tot_right]
+
+                        if middle in vertices:
+
+                            left = points[tot_right - tot]
+                            right = points[tot_right + tot]
+
+                            if (left in vertices) or (right in vertices):
+                                is_figure = True
+
+            elif figure == 'hexagon':
+
+                if i == 0:
+
+                    tot_left = points.index(vertices[0])
+                    tot_right = points.index(vertices[1])
+                    tot = tot_right - tot_left
+
+                if top:
+
+                    if (i == 0) or not any(vertices):
+                        n += 1
+                    else:
+
+                        left = points[tot_right - tot]
+                        right = points[tot_right + tot]
+
+                        if (left in vertices) and (right in vertices):
+                            ok_middle = True
+
+                        top = False
+
+                fin = n * 2
+
+                if i >= fin:
+                    if i == fin:
+
+                        if points[tot_right] == vertices[0] and \
+                                points[tot_right + tot] == vertices[1]:
+                            ok_button = True
+
+            elif figure == 'triangule':
+
+                if i == 0:
+                    if vertices[0] == vertices[1]:
+                        triangule_one_side = True
+                        tot_left = points.index(vertices[0])
+                    else:
+                        triangule_one_side = False
+                        tot_right = points.index(vertices[1])
+                        tot_left = points.index(vertices[0])
+
+                        tot = tot_right - tot_left
+
+                if top:
+                    if (i == 0) or not any(vertices):
+                        n += 1
+                    else:
+                        top = False
+
+                if not triangule_one_side:
+
+                    if i >= tot:
+
+                        if i == tot:
+
+                            if vertices[0] == vertices[1]:
+                                if points[tot_right] == vertices[0]:
+                                    ok_button = True
+                        else:
+                            ok_button = False
+
+                else:
+
+                    if i >= n:
+                        if i == n:
+
+                            left = points[tot_left]
+                            right = points[tot_left + n]
+
+                            if left == vertices[0] and right == vertices[1]:
+                                ok_button = True
+
+                        else:
+
+                            ok_button = False
+
+            i += 1
+
+        if figure == 'parallelogram':
+            if is_rombo and ok_middle and ok_button:
+                is_figure = True
+        elif figure == 'hexagon':
+            if ok_middle and ok_button:
+                is_figure = True
+        elif figure == 'triangule':
+            if ok_button:
+                is_figure = True
+
+        message = ' '.join([str(x) for x in _points])
+
+        if is_figure:
+            message = '%s are the vertices of a %s' % (
+                message,
+                figure
+            )
+        else:
+            message = '%s are not the vertices of a %s' % (
+                message,
+                'acceptable figure'
+            )
+
+        return message
+
+    def __read_file(self, ins):
+
+        ins = open(ins, 'r')
+
+        for line in ins:
+            line = line.replace('\n', '')
+            self._database.append(line.strip())
+
+
+figure = Figure(
+    database='input-find-figure.txt'
+)
+
+#points = [1, 2, 3]
+#print figure.find(points)
+
+for value in figure.database:
+    points = [int(x) for x in value.split(' ')]
+    print figure.find(points)

input-control-isbn.txt

+0-89237-010-6
+0-8306-3637-4      
+  0-06-017758-6
+    Esto es basura
+1-56884-030-6
+    0-8230-2571-3
+    0-345-31386-0
+    0-671-88858-7
+    0-8104-5687-7
+    0-671-74119-5
+    0-812-52030-0
+    0-345-24865-1-150
+0-452-26740-4
+    0-13-139072-2
+    0-1315-2447-X

input-find-figure.txt

+1 2 3
+11 13 29 31
+26 11 13 24
+4 5 9 13 12 7
+1 2 3 4 5
+47
+11 13 23 25

input-spelling-check.txt

+i
+is
+has
+have
+be
+my
+more
+contest
+me
+too
+if
+award
+#
+me
+aware
+m
+contest
+hav
+oo
+or
+i
+fi
+mre
+#

spelling_check.py

+
+
+words = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
+
+
+class SpellCheck(object):
+
+    _dictionary_words = []
+    _control_words = []
+
+    def __init__(self, database=None):
+
+        if database:
+            self.__read_file(database)
+
+    @property
+    def control_words(self):
+        return self._control_words
+
+    @property
+    def dictionary_words(self):
+        return self._dictionary_words
+
+    def add_value_control_word(self):
+        pass
+
+    def add_value_dictionary_word(self):
+        pass
+
+    def search(self, word, dictionary):
+
+        if word and dictionary:
+
+            if word not in dictionary:
+
+                return_values = []
+                word_size = len(word)
+
+                for x in dictionary:
+
+                    if len(x) >= (word_size - 1) and len(x) <= (word_size + 1):
+                        for x in word:
+
+                            value = word.replace(x, '', 1)
+
+                            if (value in dictionary) and \
+                                    (not value in return_values):
+                                return_values.append(value)
+
+                            for append_word in words:
+                                value = word.replace(x, x + append_word, 1)
+
+                                if (value in dictionary) and \
+                                        (not value in return_values):
+                                    return_values.append(value)
+
+                            for reeplace_word in words:
+                                value = word.replace(x, reeplace_word, 1)
+                                if (value in dictionary) and \
+                                        (not value in return_values):
+                                    return_values.append(value)
+
+                return word, return_values
+            else:
+                return '%s is correct' % word
+
+    def __read_file(self, ins):
+
+        first_part = True
+
+        ins = open(ins, 'r')
+
+        for line in ins:
+            line = line.replace('\n', '')
+
+            if line == '#':
+                first_part = False
+                continue
+
+            if first_part:
+                self._dictionary_words.append(line)
+            else:
+                self._control_words.append(line)
+
+
+speel_check = SpellCheck(
+    database='input-spelling-check.txt'
+)
+
+control_words = speel_check.control_words  # could be a list
+dictionary_words = speel_check.dictionary_words  # could be a list
+
+# dictionary_control = ('at', 'phat', 'haet', 'i',
+#'is', 'has', 'have', 'award',
+#'pe', 'ha', 'ate', 'ht', 'th', 'hati', 'bat', 'hht')
+
+# control_words = ('m', 'have', 'aware', 'p', 'is')
+
+# you can use speel_check alone!
+# speel_check.search('#any word', dictionary_control)
+
+for word in control_words:
+    print speel_check.search(word, dictionary_words)