diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2020-02-05 16:56:42 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2020-02-05 16:56:42 +0100 |
| commit | 35c2c6787fb87e27544521ea915a602afe09ac3f (patch) | |
| tree | d6f8c976cae73bac7f462f13cd89df79f28a94a2 | |
| parent | bf125b50ddbd7550c757c716d736bc65ea1f0a25 (diff) | |
| download | tdde44-master.tar.gz | |
| -rw-r--r-- | labb2/__pycache__/lib.cpython-36.pyc | bin | 0 -> 595 bytes | |||
| -rw-r--r-- | labb2/kod.py | 46 | ||||
| -rwxr-xr-x | labb2/lib.py | 2 | ||||
| -rw-r--r-- | upg2_1.py | 55 | ||||
| -rw-r--r-- | upg2_2.py (renamed from upg2.py) | 125 | ||||
| -rw-r--r-- | upg2_3.py | 68 | ||||
| -rw-r--r-- | upg3_1.py | 39 | ||||
| -rw-r--r-- | upg3_2.py | 38 |
8 files changed, 225 insertions, 148 deletions
diff --git a/labb2/__pycache__/lib.cpython-36.pyc b/labb2/__pycache__/lib.cpython-36.pyc Binary files differnew file mode 100644 index 0000000..5c325d1 --- /dev/null +++ b/labb2/__pycache__/lib.cpython-36.pyc diff --git a/labb2/kod.py b/labb2/kod.py index 752ba43..378225f 100644 --- a/labb2/kod.py +++ b/labb2/kod.py @@ -1,23 +1,25 @@ from lib import minimum_edit_distance -words = {} -for line in open("alphabetical.csv", "r").readlines(): - word, freq = line.split(",") - words[word] = int(freq) - - -def main(): +def load(): + words = {} + for line in open("alphabetical.csv", "r").readlines(): + word, freq = line.split(",") + words[word] = int(freq) + return words + +def main(words): + """Repeatedly ask the user for word and autocomplete/-correct it.""" word = "" while word != "q": word = input("Type word: ").lower() - print("Autocompletion finished: ", autocomplete(word)) - print("Sorted autocompletion: ", autocomplete_best(word)) - print("Best three: ", autocomplete_best(word, 3)) - print("Autocorrect: ", autocorrect(word)) + print("Autocompletion finished: ", autocomplete(words, word)) + print("Sorted autocompletion: ", autocomplete_best(words, word)) + print("Best three: ", autocomplete_best(words, word, 3)) + print("Autocorrect: ", autocorrect(words, word)) -def autocomplete(search_word): +def autocomplete(words, search_word): """Return autocomplete suggestions.""" for word in words.keys(): if word.startswith(search_word): @@ -25,21 +27,22 @@ def autocomplete(search_word): return None -def autocomplete_best(search_word, amount=None): +def autocomplete_best(words, search_word, amount=None): """Return the /amount/ most common autocompletions.""" matching_words = {word: freq for word, freq in words.items() if word.startswith(search_word)} - matching_words_sorted = {word: freq for word, freq in - sorted(matching_words.items(), - key=lambda item: item[1], - reverse=True)} + matching_words_sorted = sorted(matching_words.items(), + key=lambda item: item[1], # sort by freq + reverse=True) # sort most frequent first + matching_words_sorted_words = [word for word, freq in matching_words_sorted] + if amount: - return ", ".join(list(matching_words_sorted.keys())[:amount]) + return ", ".join(list(matching_words_sorted_words)[:amount]) else: - return ", ".join(list(matching_words_sorted.keys())) + return ", ".join(list(matching_words_sorted_words)) -def autocorrect(search_word): +def autocorrect(words, search_word): """Return the word with the smallest Levhenstein distance""" best = None for word, _ in words.items(): @@ -50,5 +53,4 @@ def autocorrect(search_word): best = (edit_distance, word) return best[1] - -main() +main(load()) diff --git a/labb2/lib.py b/labb2/lib.py index c2e12eb..d48096f 100755 --- a/labb2/lib.py +++ b/labb2/lib.py @@ -1,5 +1,5 @@ def minimum_edit_distance(s1, s2): - """Returnera minimum edit distance för strängarna s1 och s2).""" + """Returnera minimum edit distance för strängarna s1 och s2.""" # byt värde på s1 och s2 om s1 är den längre strängen if len(s1) > len(s2): s1, s2 = s2, s1 diff --git a/upg2_1.py b/upg2_1.py new file mode 100644 index 0000000..7a66bd0 --- /dev/null +++ b/upg2_1.py @@ -0,0 +1,55 @@ +def greeting(name): + return "Hej " + name + (", visste du att M är min favoritbokstav" if name[0] == "M" else "") + "!" + +def is_this_a_long_list(values): + return len(values) > 5 + +def get_grade(score): + if score > 80: + return "VG" + if score >= 50: + return "G" + return "U" + +def days_in_month(month): + return {"januari": 31, + "februari": 28, + "mars": 31, + "april": 30, + "maj": 31, + "juni": 30, + "juli": 31, + "augusti": 31, + "september": 30, + "oktober": 31, + "november": 30, + "december": 31, + }[month] + +def odd(value): + return value % 2 != 0 + +def get_integer_description(value): + if value % 2 == 0 and value > 0: + return 2 + if value % 2 != 0 and value > 0: + return 1 + if value == 0: + return 0 + if value % 2 != 0 and value < 0: + return -1 + if value % 2 == 0 and value < 0: + return -2 + return "yeet" + +def appraisal_factor(rare, good_condition): + price = 1 + if rare: + price += 0.25 + else: + price -= 0.25 + if good_condition: + price += 0.5 + else: + price -= 0.5 + return price @@ -1,59 +1,3 @@ -def greeting(name): - return "Hej " + name + (", visste du att M är min favoritbokstav" if name[0] == "M" else "") + "!" - -def is_this_a_long_list(values): - return len(values) > 5 - -def get_grade(score): - if score > 80: - return "VG" - if score >= 50: - return "G" - return "U" - -def days_in_month(month): - return {"januari": 31, - "februari": 28, - "mars": 31, - "april": 30, - "maj": 31, - "juni": 30, - "juli": 31, - "augusti": 31, - "september": 30, - "oktober": 31, - "november": 30, - "december": 31, - }[month] - -def odd(value): - return value % 2 != 0 - -def get_integer_description(value): - if value % 2 == 0 and value > 0: - return 2 - if value % 2 != 0 and value > 0: - return 1 - if value == 0: - return 0 - if value % 2 != 0 and value < 0: - return -1 - if value % 2 == 0 and value < 0: - return -2 - return "yeet" - -def appraisal_factor(rare, good_condition): - price = 1 - if rare: - price += 0.25 - else: - price -= 0.25 - if good_condition: - price += 0.5 - else: - price -= 0.5 - return price - def create_ten_list_while(): l = [] i = 0 @@ -188,72 +132,3 @@ def birthday(n, cur=365): if cur == 365: return 1 - (cur/365 * birthday(n, cur-1)) return cur/365 * birthday(n, cur-1) - -def replace_periods_with_newlines(string_value): - str_list = list(string_value) - for i in range(len(string_value)): - if str_list[i] == ".": - str_list[i] = "\n" - return "".join(str_list) - -def replace_char_in_string(original_string, search_character, replacement): - str_list = list(original_string) - for i in range(len(str_list)): - if str_list[i] == search_character: - str_list[i] = replacement - return "".join(str_list) - -def reverse_string_for(string_value): - s = "" - for c in string_value[::-1]: - s += c - return s - -def reverse_string_while(string_value): - str_list = list(string_value) - l = [] - i = len(str_list)-1 - while i >= 0: - l.append(str_list[i]) - i -= 1 - return "".join(l) - -def get_five_first(value_list): - l = [] - for i in range(5): - l.append(value_list[i]) - return l - -def get_nfirst(value_list, n): - l = [] - for i in range(n): - l.append(value_list[i]) - return l - -def get_all_less_than(values, cutoff): - return list(filter(lambda n: n < cutoff, values)) - -def get_all_even(values): - return list(filter(lambda n: n % 2 == 0, values)) - -def get_all_divisible(values, divisor): - return list(filter(lambda n: n % divisor == 0, values)) - -def multiply_for_each(values, multiplier): - for i in range(len(values)): - values[i] *= multiplier - return values - -def insert_at_asc_place(values, new_value): - for i, val in enumerate(values): - if new_value < val: - values.insert(i, new_value) - return values - values.append(new_value) - return values - -def sort_asc(values): - sort = [] - for val in values: - insert_at_asc_place(sort, val) - return sort diff --git a/upg2_3.py b/upg2_3.py new file mode 100644 index 0000000..619e852 --- /dev/null +++ b/upg2_3.py @@ -0,0 +1,68 @@ +def replace_periods_with_newlines(string_value): + str_list = list(string_value) + for i in range(len(string_value)): + if str_list[i] == ".": + str_list[i] = "\n" + return "".join(str_list) + +def replace_char_in_string(original_string, search_character, replacement): + str_list = list(original_string) + for i in range(len(str_list)): + if str_list[i] == search_character: + str_list[i] = replacement + return "".join(str_list) + +def reverse_string_for(string_value): + s = "" + for c in string_value[::-1]: + s += c + return s + +def reverse_string_while(string_value): + str_list = list(string_value) + l = [] + i = len(str_list)-1 + while i >= 0: + l.append(str_list[i]) + i -= 1 + return "".join(l) + +def get_five_first(value_list): + l = [] + for i in range(5): + l.append(value_list[i]) + return l + +def get_nfirst(value_list, n): + l = [] + for i in range(n): + l.append(value_list[i]) + return l + +def get_all_less_than(values, cutoff): + return list(filter(lambda n: n < cutoff, values)) + +def get_all_even(values): + return list(filter(lambda n: n % 2 == 0, values)) + +def get_all_divisible(values, divisor): + return list(filter(lambda n: n % divisor == 0, values)) + +def multiply_for_each(values, multiplier): + for i in range(len(values)): + values[i] *= multiplier + return values + +def insert_at_asc_place(values, new_value): + for i, val in enumerate(values): + if new_value < val: + values.insert(i, new_value) + return values + values.append(new_value) + return values + +def sort_asc(values): + sort = [] + for val in values: + insert_at_asc_place(sort, val) + return sort diff --git a/upg3_1.py b/upg3_1.py new file mode 100644 index 0000000..752c8e9 --- /dev/null +++ b/upg3_1.py @@ -0,0 +1,39 @@ +def key_exists(key, d): + return key in d + +def value_exists1(value, d): + return value in d.values() + +def add_to_dict(key, value, d): + d[key] = value + +def add_new_only_to_dict(key, value, d): + if not key in d: + d[key] = value + +def increment_dictionary_value1(key, d): + d[key] += 1 + +def increment_dictionary_value2(key, d): + if key in d: + d[key] += 1 + else: + d[key] = 1 + +def add_to_value_list1(key, value, d): + d[key].append(value) + +def return_value_list1(prefix, d): + res = [] + for k, v in d.items(): + if k.startswith(prefix): + res.append(v) + return res + +def value_exists2(value, d): + for _,v in d.items(): + if value == v: + return True + if type(v) == list: + return value in v + return False diff --git a/upg3_2.py b/upg3_2.py new file mode 100644 index 0000000..b216c1e --- /dev/null +++ b/upg3_2.py @@ -0,0 +1,38 @@ +from functools import reduce + +def sum_of_ints(values): + return reduce(lambda x,y: x + y if type(y) == int else x, values, 0) + +def flatten_list1(vals): + res = [] + for val in vals: + if type(val) == list: + res.extend(flatten_list1(val)) + else: + res.append(val) + return res + +def flatten_list2(vals): + return flatten_list1(vals) + +def get_first_column(matrix): + return [row[0] for row in matrix] + +def get_nth_column(n, matrix): + return [row[n-1] for row in matrix] + +def get_all_columns(matrix): + return [get_nth_column(i+1, matrix) for i in range(len(matrix[0]))] + +def scalar_product(vec1, vec2): + return sum(map(lambda pair: pair[0] * pair[1], zip(vec1, vec2))) + +def matrix_square(matrix): + new_matrix = [] + for row in matrix: + new_row = [] + for i in range(len(row)): + new_row.append(scalar_product(row, get_nth_column(i+1, matrix))) + new_matrix.append(new_row) + return new_matrix + |
