From: NilsForssen Date: Mon, 21 Feb 2022 07:37:15 +0000 (+0100) Subject: update X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=a5f89e05f895e47d78ddf32c460cc5cd6653bb77;p=TDDE44.git update --- diff --git a/laboration3/pokedex.py b/laboration3/pokedex.py new file mode 100644 index 0000000..947fe7a --- /dev/null +++ b/laboration3/pokedex.py @@ -0,0 +1,25 @@ +# pokedex.py +import requests +import json +import sys + +API_SERVER = "https://www.ida.liu.se/~TDDE44/pokeapi/api/v2/" + +def main(): + data = get_json_as_dict(API_SERVER + "pokemon/") + pokemons = data.get("results") + for pokemon in pokemons: + print(pokemon.get("name")) + + + +def get_json_as_dict(url): + r = requests.get(url) + assert r.status_code == 200, "Request unsuccessful" + + return r.json() + + +if __name__ == "__main__": + main() + \ No newline at end of file diff --git a/laboration3/uppgift_1.txt b/laboration3/uppgift_1.txt new file mode 100644 index 0000000..0f75ecc --- /dev/null +++ b/laboration3/uppgift_1.txt @@ -0,0 +1,21 @@ +UEsDBBQAAAAIAEdKR1REFzSfLgEAAFYDAAAaABwAbmlsZm8zNTktcHl1cHBnM18xL29yaWcucHlV +VAkAA8bVAGLG1QBidXgLAAEEehxpWQQA4fUFhVJNb4MwDL3zKyztAlpViR4rcd0v2E6oQhExU9TU +oCSl5d/P+eiAdms5BdvvPfvZb/A1DN+qc1BmmcQOjjg1eFXW2ZyfG5DFPgP+DLqzIZ8GRSCzWD0K +fcZUX+bh7wESogG0DU+bFwktpGxc30jVuih2RyBrjh6givEFiPDS9KSnp2gVhgHqXRCPwX9pFbUG +T0guEKqehJmaUFCujUjo98o79gK6W0Nfd1SGEGqLD8mFYPItmq/Z+/KJfVsxDEgyLufmfFzNimEw +2KnrjHbcZQX1Ifx0vYEjC8Q1Kocn3uLcoh+s3mukxFLwLBXE91yVWH8bKpZXwok/bmp3f1O+kXF9 +TKs2Ri8cMmvdpPJpeOG3GGoGKKvIOkEt5uMGvBUFCJLz1Y6viZZzfAhe3g9QSwME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A \ No newline at end of file diff --git a/laboration3/uppgift_2.txt b/laboration3/uppgift_2.txt new file mode 100644 index 0000000..d9f6878 --- /dev/null +++ b/laboration3/uppgift_2.txt @@ -0,0 +1,23 @@ +UEsDBBQAAAAIAGeTUFR8ZcPJqQEAAIgFAAAaABwAbmlsZm8zNTktcHl1cHBnM18yL29yaWcucHlV +VAkAA/IzDWLyMw1idXgLAAEEehxpWQQA4fUFrVPLboQwDLzzFZF6gS2tFnqrtJ/RE0JRBGYbKQSa +hO2qX984vAm0PTSHCOGxZ5yxH8hb2155ZchLEJRQEd3VtKkol0an4Y2JDqjg2kSvAbHHNIZcyNl9 +V40iXErAm8zIHjgBDNQYd8A5hIdXhGsutWGygBCBsYWNTMuDrI8XV8vFFJhOSfwd9KIrwYwB6fgT +X/WAz5yYA9m+2nyn+OGTZPnf3mTTNEJjsqi2aPkZ7pa27EHRFAWhwceytl1g91/pCoZWXGlDi0Z0 +tQxrZhS/H/Whmk/sogfNjAs2i8jOeXTMJs37yCVj8h90kjyR5CdKJsRAqdf92Z8aGbPc0dGZzPaQ +B7/pcM6W93iaEZBdDYoZQF0b95Ars+h88sUmrW1ByCBbF0wwRVvVlF1hwhsUSUzsnQ5VMbDaOkto +EfZOUcgXb/0cf9b6JDdqhMlyE0p3p3DciaRve6KbBexsq5M7rGtycnk/DO+IxpInrLh8JAwOj9T7 +QfVHxxTsTq5v7ODq4PzBdMzOc8xUTF4hFDBtR7SeAZuNMKzpbWHGJ8M3nlr4OP8WFPkb+g1QSwME +FAAAAAgAZ5NQVCC1EE+uAAAASAEAACQAHABuaWxmbzM1OS1weXVwcGczXzIvcHl1cHBndGVzdDNf +Mi5sb2dVVAkAA/IzDWLyMw1idXgLAAEEehxpWQQA4fUF41IGAgVv/6AgV+8QBZ/D04JDHBVCAwLc +Pd1CXIMUQLJcXCC+goKxnpGeoZWCoYFCQf7hJXnpekjiRjjEja0UjLCJm+BQb4pDvRkOcXMc4hZA +cVO4OBfYk8Ghvr6OCgH+h1v83IHWG5vqKWgYGhgoJKVmHN5WVqwJUeVSqnB4SZFCQVFqTmZuZh6Q +XaLg7u/iDdTlolBweKlCQGVJRn5eaUFBemZaCcgyRS4FBS4AUEsDBBQAAAAIAGeTUFTKWpAsegAA +AJQAAAAbABwAbmlsZm8zNTktcHl1cHBnM18yL2luZm8udHh0VVQJAAPyMw1i8jMNYnV4CwABBHoc +aVkEAOH1BUXMSw6DIBSF4TmrYAO03MtL2YxBHpbUB1EZuPvCqMkZ/Sf5grujpRQ5IuPIQFMYLBoL +ktQrnu3a85oOoUaS8hqp/0T/jaH1WsqS0z3hqzzkOPOSd7fSLairbjS4AWY5GydAiRECb+Og56gG +ORrt/ywrT5fEhO+OdOwHUEsBAh4DFAAAAAgAZ5NQVHxlw8mpAQAAiAUAABoAGAAAAAAAAQAAAKSB +AAAAAG5pbGZvMzU5LXB5dXBwZzNfMi9vcmlnLnB5VVQFAAPyMw1idXgLAAEEehxpWQQA4fUFUEsB +Ah4DFAAAAAgAZ5NQVCC1EE+uAAAASAEAACQAGAAAAAAAAQAAAKSB/QEAAG5pbGZvMzU5LXB5dXBw +ZzNfMi9weXVwcGd0ZXN0M18yLmxvZ1VUBQAD8jMNYnV4CwABBHocaVkEAOH1BVBLAQIeAxQAAAAI +AGeTUFTKWpAsegAAAJQAAAAbABgAAAAAAAEAAACkgQkDAABuaWxmbzM1OS1weXVwcGczXzIvaW5m +by50eHRVVAUAA/IzDWJ1eAsAAQR6HGlZBADh9QVQSwUGAAAAAAMAAwArAQAA2AMAAAAA \ No newline at end of file diff --git a/laboration3/uppgift_3.py b/laboration3/uppgift_3.py index dd442b7..5ee88a9 100644 --- a/laboration3/uppgift_3.py +++ b/laboration3/uppgift_3.py @@ -4,8 +4,7 @@ def fac_rec(n): if n == 0: return 1 else: - return fac_rec(n-1) - + return n * fac_rec(n-1) def fib(n): if n == 0: @@ -17,19 +16,20 @@ def fib(n): def pascal(row, col): - if col < 1 or col > row: - return 0 + if col == 0 or col == row: + return 1 elif row == 1: return 1 else: return pascal(row-1, col) + pascal(row-1, col - 1) - def keep_if_even(lst): if not lst: return list() if not lst[0] % 2: - return [lst[0], *keep_if_even(lst[1:])] + ret = [lst[0]] + ret.extend(keep_if_even(lst[1:])) + return ret else: return keep_if_even(lst[1:]) @@ -37,8 +37,9 @@ def keep_if_even(lst): def reverse_rec(lst): if not lst: return [] - return [lst[-1], *reverse_rec(lst[:-1])] - + ret = [lst[-1]] + ret.extend(reverse_rec(lst[:-1])) + return ret def keep_if_even_all(lst): if not lst: @@ -46,11 +47,14 @@ def keep_if_even_all(lst): even_inner = list() if isinstance(lst[0], list): even_inner = keep_if_even_all(lst[0]) + even_inner = [even_inner] #Ibland e man bara lat elif not lst[0] % 2: - return [lst[0], *keep_if_even_all(lst[1:])] - - return [*even_inner, *keep_if_even_all(lst[1:])] + ret = [lst[0]] + ret.extend(keep_if_even_all(lst[1:])) + return ret + even_inner.extend(keep_if_even_all(lst[1:])) + return even_inner def reverse_rec_all(lst): if not lst: @@ -58,11 +62,12 @@ def reverse_rec_all(lst): reverse_inner = list() if isinstance(lst[-1], list): reverse_inner = reverse_rec_all(lst[-1]) + reverse_inner = [reverse_inner] else: reverse_inner = [lst[-1]] - return [*reverse_inner, *reverse_rec(lst[:-1])] - + reverse_inner.extend(reverse_rec_all(lst[:-1])) + return reverse_inner def is_in_list(lst, elm): if not lst: @@ -87,16 +92,18 @@ def count_all(lst): def subst_all(lst, elm, new_value): - if not lst: + if len(lst) == 0: return list() - if isinstance(lst[0], list): - inner = subst_all([0], elm, new_value) + inner = subst_all(lst[0], elm, new_value) + inner = [inner] # Pallar inte skriva om snyggt s blir lite fulkod ibland men det funkar elif lst[0] == elm: inner = [new_value] - - return [*inner, *subst_all(lst[1:], elm, new_value)] - + else: + inner = [lst[0]] + print(inner, lst) + inner.extend(subst_all(lst[1:], elm, new_value)) + return inner def linear_search(key, seq): if not seq: @@ -109,7 +116,6 @@ def binary_search(key, seq): if seq[0] != key: return -1 return 0 - print(seq, key) mid = len(seq) // 2 @@ -126,6 +132,3 @@ def binary_search(key, seq): if res == -1: return res return res - - -print(binary_search(12, [1, 2, 3, 4, 5, 6, 6.5, 7, 9, 10, 11, 12])) diff --git a/laboration3/uppgift_3.txt b/laboration3/uppgift_3.txt new file mode 100644 index 0000000..845132f --- /dev/null +++ b/laboration3/uppgift_3.txt @@ -0,0 +1,29 @@ +UEsDBBQAAAAIANeSUFQEAVJL2QIAAIULAAAaABwAbmlsZm8zNTktcHl1cHBnM18zL29yaWcucHlV +VAkAA+YyDWLmMg1idXgLAAEEehxpWQQA4fUFrVZNj9owEL3zK6ZClZJu6BJ6Q6LHSr310lMURSY4 +1MI41A5s99937DiJnZjdoJYDkPl6b8bPA0v4ebkcWdXAl8XiQCuoSFlIWkYi3i4AX6wCAbsdrNtH +/ZK0uUoBqTFQrujEJeDTUGiVxrY0288ou7Zlu4j0EWADgYDwZL9uENuAX4gqCY9k/ZJAWfOBBj4Y +IlDL7jsG3QXFDHQ/TGyAX6UtAaQ4McIKumGdKL0UrCrojYqIq8aZW90AGiYQnKkmikdR2TqHj7Dx +gmEHWevKXfNn+qeh4hCNkbN0m8fxGA0/7ncbLGEPQqJRKmq0MauvrGXpEF+leW/rWI/KZlsMsqwd +xpPRFoTzh8drEpkQVCIjf+xMMaEaIkoatSNOTEA8lPOSQ1QwKQ5HZ8NTbv3L73tOxAEonImAPZEE +OGmcK/RfZNBTuyuF0VxmFrEFhryJROafTy+TNnfW8aBGJuczLhAgo/PiuwmZZ8iDt2SU0Yt62kJA +3z2NkMad1HaYTOFTYcaAOQlSOb8/zG8ECc/XNBs06sOZYI0YmIKThBGYaIW6cBvqg3A7j0qjjGzt +ttGyvopmvl7WD1xZTs9UNAqZeiD9TfUbc8JTr5ne8TSq46xHdd2r3mH6S0DQl+JG+JUOXXH7qxD+ +DX10J3Xn4IF3Z+fiTzIyZxst4QemEjwo0VBQJ8luBOozKPF6PDagYM+ZRGh0Vld+qg/A2uWFQ4ED +LqTqKk5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AApIEtAwAAbmlsZm8zNTktcHl1cHBnM18zL3B5dXBwZ3Rlc3QzXzMubG9nVVQFAAPmMg1idXgL +AAEEehxpWQQA4fUFUEsBAh4DFAAAAAgA15JQVNCLBH94AAAAlAAAABsAGAAAAAAAAQAAAKSBPwQA +AG5pbGZvMzU5LXB5dXBwZzNfMy9pbmZvLnR4dFVUBQAD5jINYnV4CwABBHocaVkEAOH1BVBLBQYA +AAAAAwADACsBAAAMBQAAAAA= \ No newline at end of file diff --git a/uppgift_1.txt b/uppgift_1.txt deleted file mode 100644 index 0f75ecc..0000000 --- a/uppgift_1.txt +++ /dev/null @@ -1,21 +0,0 @@ -UEsDBBQAAAAIAEdKR1REFzSfLgEAAFYDAAAaABwAbmlsZm8zNTktcHl1cHBnM18xL29yaWcucHlV -VAkAA8bVAGLG1QBidXgLAAEEehxpWQQA4fUFhVJNb4MwDL3zKyztAlpViR4rcd0v2E6oQhExU9TU -oCSl5d/P+eiAdms5BdvvPfvZb/A1DN+qc1BmmcQOjjg1eFXW2ZyfG5DFPgP+DLqzIZ8GRSCzWD0K -fcZUX+bh7wESogG0DU+bFwktpGxc30jVuih2RyBrjh6givEFiPDS9KSnp2gVhgHqXRCPwX9pFbUG -T0guEKqehJmaUFCujUjo98o79gK6W0Nfd1SGEGqLD8mFYPItmq/Z+/KJfVsxDEgyLufmfFzNimEw -2KnrjHbcZQX1Ifx0vYEjC8Q1Kocn3uLcoh+s3mukxFLwLBXE91yVWH8bKpZXwok/bmp3f1O+kXF9 -TKs2Ri8cMmvdpPJpeOG3GGoGKKvIOkEt5uMGvBUFCJLz1Y6viZZzfAhe3g9QSwMEFAAAAAgAR0pH -VEawZ9CvAAAAYAEAACQAHABuaWxmbzM1OS1weXVwcGczXzEvcHl1cHBndGVzdDNfMS5sb2dVVAkA -A8bVAGLG1QBidXgLAAEEehxpWQQA4fUF41IGAgVv/6AgV+8QBZ/D04JDHBVCAwLcPd1CXIMUQLJc -XCC+goKxnqGeoZWCoYFCQf7hJ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