Skip to content

Commit

Permalink
Nuevo iterador y carga de archivo
Browse files Browse the repository at this point in the history
  • Loading branch information
dcorrealdisc committed Dec 11, 2020
1 parent f729e04 commit 0fff78c
Show file tree
Hide file tree
Showing 12 changed files with 1,612 additions and 143 deletions.
6 changes: 6 additions & 0 deletions .vim/coc-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.venvPath":"/Users/dcorreal/Documents/Develop/Python/Envs/"
}
21 changes: 19 additions & 2 deletions DISClib/ADT/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
"""


def newList(datastructure='SINGLE_LINKED', cmpfunction=None):
def newList(datastructure='SINGLE_LINKED',
cmpfunction=None,
key=None,
filename=None):
"""Crea una lista vacia
Args:
Expand All @@ -48,7 +51,7 @@ def newList(datastructure='SINGLE_LINKED', cmpfunction=None):
Exception
"""
try:
lst = lt.newList(datastructure, cmpfunction)
lst = lt.newList(datastructure, cmpfunction, key, filename)
return lst
except Exception as exp:
error.reraise(exp, 'TADList->newList: ')
Expand Down Expand Up @@ -341,3 +344,17 @@ def subList(lst, pos, numelem):
return lt.subList(lst, pos, numelem)
except Exception as exp:
error.reraise(exp, 'List->subList: ')


def iterator(lst):
""" Retorna un iterador para la lista.
Args:
lst: La lista a iterar
Raises:
Exception
"""
try:
return lt.iterator(lst)
except Exception as exp:
error.reraise(exp, 'List->Iterator: ')
56 changes: 47 additions & 9 deletions DISClib/DataStructures/arraylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import config
from DISClib.Utils import error as error
import csv
assert config

"""
Expand All @@ -40,7 +41,7 @@
"""


def newList(cmpfunction=None):
def newList(cmpfunction, key, filename):
"""Crea una lista vacia.
Args:
Expand All @@ -51,12 +52,24 @@ def newList(cmpfunction=None):
Raises:
"""
new_list = {'elements': [],
'size': 0,
'type': 'ARRAY_LIST',
'cmpfunction': cmpfunction
}
return (new_list)
newlist = {'elements': [],
'size': 0,
'type': 'ARRAY_LIST',
'cmpfunction': cmpfunction,
'key': key
}

if(cmpfunction is None):
newlist['cmpfunction'] = defaultfunction
else:
newlist['cmpfunction'] = cmpfunction

if (filename is not None):
input_file = csv.DictReader(open(filename, encoding="utf8"),
delimiter=',')
for line in input_file:
addLast(newlist, line)
return (newlist)


def addFirst(lst, element):
Expand Down Expand Up @@ -273,7 +286,7 @@ def insertElement(lst, element, pos):
error.reraise(exp, 'arraylist->insertElement: ')


def isPresent(lst, element):
def isPresent(lst, e):
""" Informa si el elemento element esta presente en la lista.
Informa si un elemento está en la lista.
Expand All @@ -295,7 +308,7 @@ def isPresent(lst, element):
keyexist = False
for keypos in range(1, size+1):
info = lst['elements'][keypos-1]
if (lst['cmpfunction'](element, info) == 0):
if (lst['cmpfunction'](e[lst['key']], info[lst['key']]) == 0):
keyexist = True
break
if keyexist:
Expand Down Expand Up @@ -364,6 +377,7 @@ def subList(lst, pos, numelem):
sublst = {'elements': [],
'size': 0,
'type': 'ARRAY_LIST',
'key': lst['key'],
'cmpfunction': lst['cmpfunction']}
elem = pos-1
cont = 1
Expand All @@ -375,3 +389,27 @@ def subList(lst, pos, numelem):
return sublst
except Exception as exp:
error.reraise(exp, 'arraylist->subList: ')


def iterator(lst):
""" Retorna un iterador para la lista.
Args:
lst: La lista a iterar
Raises:
Exception
"""
try:
if(lst is not None):
for pos in range(0, lst['size']):
yield lst['elements'][pos]
except Exception as exp:
error.reraise(exp, 'arraylist->Iterator')


def defaultfunction(id1, id2):
if id1 > id2:
return 1
elif id1 < id2:
return -1
return 0
23 changes: 20 additions & 3 deletions DISClib/DataStructures/liststructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"""


def newList(datastructure='SINGLE_LINKED', cmpfunction=None):
def newList(datastructure, cmpfunction, key, filename):
"""Crea una lista vacia.
Args:
Expand All @@ -48,9 +48,9 @@ def newList(datastructure='SINGLE_LINKED', cmpfunction=None):
"""
try:
if (datastructure == "ARRAY_LIST"):
lt = alt.newList(cmpfunction)
lt = alt.newList(cmpfunction, key, filename)
else:
lt = slt.newList(cmpfunction)
lt = slt.newList(cmpfunction, key, filename)
return lt
except Exception as exp:
error.reraise(exp, 'list->newList: ')
Expand Down Expand Up @@ -383,3 +383,20 @@ def subList(lst, pos, numelem):
return slt.subList(lst, pos, numelem)
except Exception as exp:
error.reraise(exp, 'List->subList: ')


def iterator(lst):
""" Retorna un iterador para la lista.
Args:
lst: La lista a iterar
Raises:
Exception
"""
try:
if (lst['type'] == 'ARRAY_LIST'):
return alt.iterator(lst)
else:
return slt.iterator(lst)
except Exception as exp:
error.reraise(exp, 'List->Iterator: ')
55 changes: 47 additions & 8 deletions DISClib/DataStructures/singlelinkedlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import config
from DISClib.DataStructures import listnode as node
from DISClib.Utils import error as error
import csv
assert config

"""
Expand All @@ -39,7 +40,7 @@
"""


def newList(cmpfunction=None):
def newList(cmpfunction, key, filename):
"""Crea una lista vacia.
Se inicializan los apuntadores a la primera y ultima posicion en None.
Expand All @@ -53,12 +54,22 @@ def newList(cmpfunction=None):
Raises:
"""
new_list = {'first': None,
'last': None,
'size': 0,
'type': 'SINGLE_LINKED',
'cmpfunction': cmpfunction}
return new_list
newlist = {'first': None,
'last': None,
'size': 0,
'key': key,
'type': 'SINGLE_LINKED'}

if(cmpfunction is None):
newlist['cmpfunction'] = defaultfunction
else:
newlist['cmpfunction'] = cmpfunction

if (filename is not None):
input_file = csv.DictReader(open(filename))
for line in input_file:
addLast(newlist, line)
return newlist


def addFirst(lst, element):
Expand Down Expand Up @@ -356,7 +367,8 @@ def isPresent(lst, element):
node = lst['first']
keyexist = False
for keypos in range(1, size+1):
if (lst['cmpfunction'](element, node['info']) == 0):
if (lst['cmpfunction'](element[lst['key']],
node['info'][lst['key']]) == 0):
keyexist = True
break
node = node['next']
Expand Down Expand Up @@ -433,6 +445,7 @@ def subList(lst, pos, numelem):
'last': None,
'size': 0,
'type': 'SINGLE_LINKED',
'key': lst['key'],
'cmpfunction': lst['cmpfunction']}
cont = 1
loc = pos
Expand All @@ -444,3 +457,29 @@ def subList(lst, pos, numelem):
return sublst
except Exception as exp:
error.reraise(exp, 'singlelinkedlist->subList: ')


def iterator(lst):
""" Retorna un iterador para la lista.
Args:
lst: La lista a iterar
Raises:
Exception
"""
try:
if(lst is not None):
current = lst['first']
while current is not None:
yield current['info']
current = current['next']
except Exception as exp:
error.reraise(exp, 'singlelinkedlist->Iterator')


def defaultfunction(id1, id2):
if id1 > id2:
return 1
elif id1 < id2:
return -1
return 0
3 changes: 2 additions & 1 deletion Test/list/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
file_path = os.path.join(os.path.dirname(__file__), '../..')
file_path = os.path.join(os.path.dirname(__file__), '../../')
file_dir = os.path.dirname(os.path.realpath('__file__'))
sys.path.insert(0, os.path.abspath(file_path))
data_path = os.path.join(os.path.dirname(__file__), '../Data/')
Loading

0 comments on commit 0fff78c

Please sign in to comment.