Commit 828ef83c authored by Loic Huder's avatar Loic Huder
Browse files

Merging local modifs with remote from @thollarf

parents 5e70280c 29b47f03
......@@ -4,6 +4,9 @@ form key=val where key is p1, p2 or p3, and val is a float.
"""
import re
import logging
logging.basicConfig(level=logging.INFO)
def compute_stats(file_name):
"""
......@@ -26,6 +29,40 @@ def compute_stats(file_name):
return (number, total_sum, total_sum/float(number))
def check_format_re(file_name, default_values):
"""Check the file has the right format
:param file_name: (str) the input file name (text)
:param default_values: (array of float) the default values for
p1, p2, and p3 respectivelly
:returns: the read values
"""
with open(file_name) as handle:
for line_num, line in enumerate(handle):
vals = [None, None, None]
logging.debug("line = {}".format(line.strip()))
for si, sv in re.findall(r"p(\d)\s*=\s*(\d\.\d+)", line):
try:
idx = int(si) - 1
except ValueError as e:
logging.warning(f'file {file_name} line {line_num}: wrong format: p{si}')
continue
if idx > 2:
logging.warning(f'file {file_name} line {line_num}: p{si} out of bounds')
continue
try:
val = float(sv)
except ValueError as e:
logging.warning(f'file {file_name} line {line_num}: wrong value: p{si}')
continue
vals[idx] = val
# checking for missing item
for i,v in enumerate(vals):
if v is None:
logging.warning(f'file {file_name} line {line_num}: missing: p{i+1}, setting default value')
vals[i] = default_values[i-1]
def check_format(file_name, cols_names_set):
"""
check the file has the right format.
......@@ -68,3 +105,8 @@ sums = []
for file_name in file_names:
check_format(file_name, cols_names_set)
# using version with re, and type check
print("USING VERSION WITH RE AND TYPE CHECK")
for file_name in file_names:
check_format_re(file_name, [100, 200, 300])
......@@ -561,6 +561,90 @@
"https://en.wikipedia.org/wiki/Hash_table"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"integer remove from the list: 5\n",
"shuffled list: [4, 19, 14, 1, 8, 17, 15, 2, 3, 12, 0, 6, 16, 9, 11, 10, 7, 13, 18]\n"
]
}
],
"source": [
"from random import shuffle, randint\n",
"\n",
"n = 20\n",
"i = randint(0, n-1)\n",
"print('integer remove from the list:', i)\n",
"l = list(range(n))\n",
"l.remove(i)\n",
"shuffle(l)\n",
"print('shuffled list: ', l)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## DIY: back to the \"find the removed element\" problem\n",
"\n",
" - Could the problem be solved using set ? \n",
" - What is the complexity of this solution ? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A possible solution : "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{5}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"full_set = set(range(n))\n",
"changed_set = set(l)\n",
"full_set - changed_set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Complity :\n",
"\n",
" - line 1: n insertions --> O(n)\n",
" - line 2 : n insertions --> O(n)\n",
" - line 3: one traversal O(n), with one lookup at each time (O(1) -> O(n) \n",
" \n",
" -> Complixity of the whole algorithm : O(n)\n",
" \n",
"# Note "
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -1085,7 +1169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.2+"
}
},
"nbformat": 4,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment