From 35c2c6787fb87e27544521ea915a602afe09ac3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 5 Feb 2020 16:56:42 +0100 Subject: =?UTF-8?q?Labbtillf=C3=A4lle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- labb2/__pycache__/lib.cpython-36.pyc | Bin 0 -> 595 bytes labb2/kod.py | 46 ++++--- labb2/lib.py | 2 +- upg2.py | 259 ----------------------------------- upg2_1.py | 55 ++++++++ upg2_2.py | 134 ++++++++++++++++++ upg2_3.py | 68 +++++++++ upg3_1.py | 39 ++++++ upg3_2.py | 38 +++++ 9 files changed, 359 insertions(+), 282 deletions(-) create mode 100644 labb2/__pycache__/lib.cpython-36.pyc delete mode 100644 upg2.py create mode 100644 upg2_1.py create mode 100644 upg2_2.py create mode 100644 upg2_3.py create mode 100644 upg3_1.py create mode 100644 upg3_2.py diff --git a/labb2/__pycache__/lib.cpython-36.pyc b/labb2/__pycache__/lib.cpython-36.pyc new file mode 100644 index 0000000..5c325d1 Binary files /dev/null and b/labb2/__pycache__/lib.cpython-36.pyc differ 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.py b/upg2.py deleted file mode 100644 index dea50d4..0000000 --- a/upg2.py +++ /dev/null @@ -1,259 +0,0 @@ -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 - while i <= 10: - l.append(i) - i += 1 - return l - -def create_ten_list_for(): - l = [] - for i in range(0, 10+1): - l.append(i) - return l - -def create_zero_to_number_list_while(number): - l = [] - i = 0 - while i <= number: - l.append(i) - i += 1 - return l - -def create_zero_to_number_list_for(number): - l = [] - for i in range(0, number+1): - l.append(i) - return l - -def create_number_to_number_list_while(num1, num2): - l = [] - i = num1 - while i <= num2: - l.append(i) - i += 1 - return l - -def create_number_to_number_list_for(num1, num2): - l = [] - for i in range(num1, num2+1): - l.append(i) - return l - -import functools -def get_max_inline(integer_list): - while True: - return functools.reduce(lambda x,y: x if x > y else y, integer_list) - -def get_max_while(integer_list): - best = integer_list[0] - i = 1 - while i < len(integer_list): - best = integer_list[i] if integer_list[i] > best else best - i += 1 - return best - -def get_max_for(integer_list): - best = integer_list[0] - for num in integer_list[1:]: - best = num if num > best else best - return best - -def get_min(integer_list): - return functools.reduce(lambda x, y: x if x < y else y, integer_list) - -def word_in_list_while(words, word): - i = 0 - while i < len(words): - if word == words[i]: - return True - i += 1 - return False - -def word_in_list_for(words, word): - for w in words: - if word == w: - return True - return False - -def count_integers_while(value_list): - amount = 0 - i = 0 - while i < len(value_list): - if type(value_list[i]) == int: - amount += 1 - i += 1 - return amount - -def count_integers_for(value_list): - amount = 0 - for val in value_list: - if type(val) == int: - amount += 1 - return amount - -def count_integers_inline(value_list): - return functools.reduce(lambda x,y: x + 1 if type(y) == int else x, value_list, 0) - -def average_while(values): - s = 0 - i = 0 - while i < len(values): - s += values[i] - i += 1 - return s / len(values) - -def average_for(values): - s = 0 - for val in values: - s += val - return s / len(values) - -def population(pop_a, rate_a, pop_b, rate_b): - if pop_a == pop_b: - return -1 - if rate_a <= rate_b: - return -1 - years = 0 - while not pop_a > pop_b: - years += 1 - pop_a *= 1 + (rate_a / 100) - pop_b *= 1 + (rate_b / 100) - return years - -def population_2(pop_a, rate_a, pop_b, rate_b): - if pop_a > pop_b: - return population(pop_b, rate_b, pop_a, rate_a) - return population(pop_a, rate_a, pop_b, rate_b) - -def birthday(n, cur=365): - if cur == (365-n): - return 1 - 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_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 diff --git a/upg2_2.py b/upg2_2.py new file mode 100644 index 0000000..6a0b849 --- /dev/null +++ b/upg2_2.py @@ -0,0 +1,134 @@ +def create_ten_list_while(): + l = [] + i = 0 + while i <= 10: + l.append(i) + i += 1 + return l + +def create_ten_list_for(): + l = [] + for i in range(0, 10+1): + l.append(i) + return l + +def create_zero_to_number_list_while(number): + l = [] + i = 0 + while i <= number: + l.append(i) + i += 1 + return l + +def create_zero_to_number_list_for(number): + l = [] + for i in range(0, number+1): + l.append(i) + return l + +def create_number_to_number_list_while(num1, num2): + l = [] + i = num1 + while i <= num2: + l.append(i) + i += 1 + return l + +def create_number_to_number_list_for(num1, num2): + l = [] + for i in range(num1, num2+1): + l.append(i) + return l + +import functools +def get_max_inline(integer_list): + while True: + return functools.reduce(lambda x,y: x if x > y else y, integer_list) + +def get_max_while(integer_list): + best = integer_list[0] + i = 1 + while i < len(integer_list): + best = integer_list[i] if integer_list[i] > best else best + i += 1 + return best + +def get_max_for(integer_list): + best = integer_list[0] + for num in integer_list[1:]: + best = num if num > best else best + return best + +def get_min(integer_list): + return functools.reduce(lambda x, y: x if x < y else y, integer_list) + +def word_in_list_while(words, word): + i = 0 + while i < len(words): + if word == words[i]: + return True + i += 1 + return False + +def word_in_list_for(words, word): + for w in words: + if word == w: + return True + return False + +def count_integers_while(value_list): + amount = 0 + i = 0 + while i < len(value_list): + if type(value_list[i]) == int: + amount += 1 + i += 1 + return amount + +def count_integers_for(value_list): + amount = 0 + for val in value_list: + if type(val) == int: + amount += 1 + return amount + +def count_integers_inline(value_list): + return functools.reduce(lambda x,y: x + 1 if type(y) == int else x, value_list, 0) + +def average_while(values): + s = 0 + i = 0 + while i < len(values): + s += values[i] + i += 1 + return s / len(values) + +def average_for(values): + s = 0 + for val in values: + s += val + return s / len(values) + +def population(pop_a, rate_a, pop_b, rate_b): + if pop_a == pop_b: + return -1 + if rate_a <= rate_b: + return -1 + years = 0 + while not pop_a > pop_b: + years += 1 + pop_a *= 1 + (rate_a / 100) + pop_b *= 1 + (rate_b / 100) + return years + +def population_2(pop_a, rate_a, pop_b, rate_b): + if pop_a > pop_b: + return population(pop_b, rate_b, pop_a, rate_a) + return population(pop_a, rate_a, pop_b, rate_b) + +def birthday(n, cur=365): + if cur == (365-n): + return 1 + if cur == 365: + return 1 - (cur/365 * birthday(n, cur-1)) + return cur/365 * birthday(n, cur-1) 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 + -- cgit v1.2.1