From 1c6bb26ee3fb81dcbc8c72c5af2117e17d8accf7 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Tue, 20 Oct 2020 22:58:37 +0200 Subject: [PATCH] Add exercise --- Exercise 7/1.py | 17 ++++++++++ Exercise 7/12.py | 34 +++++++++++++++++++ Exercise 7/14.py | 74 ++++++++++++++++++++++++++++++++++++++++++ Exercise 7/1c.py | 52 +++++++++++++++++++++++++++++ Exercise 7/5.py | 28 ++++++++++++++++ Exercise 7/__init__.py | 0 6 files changed, 205 insertions(+) create mode 100644 Exercise 7/1.py create mode 100644 Exercise 7/12.py create mode 100644 Exercise 7/14.py create mode 100644 Exercise 7/1c.py create mode 100644 Exercise 7/5.py create mode 100644 Exercise 7/__init__.py diff --git a/Exercise 7/1.py b/Exercise 7/1.py new file mode 100644 index 0000000..7f3f112 --- /dev/null +++ b/Exercise 7/1.py @@ -0,0 +1,17 @@ +import numpy as np +import matplotlib.pyplot as plt + +def areOrthagonal(list1,list2): + vec1 = np.array(list1) + vec2 = np.array(list2) + return np.dot(vec1,vec2) == 0 + +def createColumnArray(): + return np.arange(1,16).reshape(3,5).transpose() + + +if __name__ == "__main__": + print(areOrthagonal([0,1],[1,0])) + print(areOrthagonal([1,0],[1,0])) + print() + print(createColumnArray()) \ No newline at end of file diff --git a/Exercise 7/12.py b/Exercise 7/12.py new file mode 100644 index 0000000..29f59d1 --- /dev/null +++ b/Exercise 7/12.py @@ -0,0 +1,34 @@ +import re + +def find_substring_indexes(str1, str2): + matches = re.compile(f'(?=({str1}))', re.IGNORECASE).finditer(str2) + return [match.span()[0] for match in matches] + +def sub_string_matches(str1, str2, str3): + matchIndexes = find_substring_indexes(str1, str2) + + offset = 0 + newString = str2 + for i in range(len(matchIndexes)): + + realIndex = matchIndexes[i] + offset + + try: + if len(str3) - len(str1) - (matchIndexes[i+1] - matchIndexes[i]) > 0: + reverseOffset = len(str3) - len(str1) - (matchIndexes[i+1] - matchIndexes[i]) + else: + reverseOffset = 0 + except IndexError: + reverseOffset = 0 + pass + + newString = newString[:realIndex] + str3 + newString[realIndex + len(str1) - reverseOffset:] + + offset += len(str3) - len(str1) + reverseOffset + return newString + +if __name__ == "__main__": + print(find_substring_indexes('iS', "Is this the real life? Is this just fantasy?")) + print(find_substring_indexes(str1 = "oo", str2 = "Never let you go let me go. Never let me go ooo")) + print(sub_string_matches(str1 = "iS", str2 = "Is this the real life? Is this just fantasy?", str3 = "cool")) + print(sub_string_matches(str1 = "oo", str2 = "Never let you goooo let me goo. Never let me goo oooo", str3 = "cool")) \ No newline at end of file diff --git a/Exercise 7/14.py b/Exercise 7/14.py new file mode 100644 index 0000000..44c0858 --- /dev/null +++ b/Exercise 7/14.py @@ -0,0 +1,74 @@ +from typing import Union + +class Piece: + def __init__(self, char): + self.type = char.lower() + self.isWhite = char.isupper() + + def __str__(self): + return self.type.upper() if self.isWhite else self.type + +class Board: + def __init__(self, boardString, size=5): + pieces = [Piece(char) if char!='.' else None for char in boardString] + self.rows = [pieces[i:i+size] for i in range(0, len(pieces), size)] + + def __str__(self): + return ''.join([''.join([str(piece) if piece!=None else '.' for piece in row])for row in self.rows]) + + def print(self): + for row in self.rows: + for piece in row: + print(piece if piece!=None else '.', end='') + print() + + def getPiece(self, x, y) -> Union[Piece, None]: + x -= 1 + y = len(self.rows) - y + return self.rows[y][x] + + def getLegalMoves(self, x, y): + try: + piece = self.getPiece(x, y) + assert piece.type == 'p' + + frontDirection = 1 if piece.isWhite else -1 + + def checkIfStartPosition(x,y): + if piece.isWhite and y == 2 and self.getPiece(x,y+2) == None: + return [(x,y+2)] + elif (not piece.isWhite) and y == 4 and self.getPiece(x,y-2) == None: + return [(x,y-2)] + return [] + + def checkInFrontOf(x, y, frontDirection): + if not self.getPiece(x, y+frontDirection): + return [(x, y+frontDirection)] + return [] + + def checkDiagonalOf(x, y, frontDirection): + moves = [] + for xToCheck in [-1, 1]: + pieceToCheck = self.getPiece(x+xToCheck, y+frontDirection) + if pieceToCheck != None and pieceToCheck.isWhite != piece.isWhite: + moves.append((x+xToCheck, y+frontDirection)) + return moves + + moves = checkInFrontOf(x,y,frontDirection) + if moves != []: + moves += checkIfStartPosition(x,y) + moves += checkDiagonalOf(x,y,frontDirection) + return moves + + except AssertionError: + print('Piece rules not implemented yet') + +if __name__ == "__main__": + board = Board('rkn.r.p.....P..PP.PPB.K..') + board.print() + print() + print(board.getPiece(5, 2)) + print(board.getPiece(2, 1)) + print() + print(board.getLegalMoves(4, 2)) + print(board.getLegalMoves(2, 4)) diff --git a/Exercise 7/1c.py b/Exercise 7/1c.py new file mode 100644 index 0000000..ac72eb5 --- /dev/null +++ b/Exercise 7/1c.py @@ -0,0 +1,52 @@ +import numpy as np +import matplotlib.pyplot as plt + +def EulerCromer( tmax, x0, y0, v0, u0, m, tau): + # tmax er tiden jorden bruker rundt solen + # x0 og y0 er startbetingelser for jordens posisjon + # v0 og u0 er starbetingelser for farten til jorden + # m er massen til jorden og tau er steglengden. + + N = int(round(tmax/tau)) #np.zeros(N) lager en liste bestående av bare 0ere av lengde N + x = np.zeros(N) + y = np.zeros(N) + u = np.zeros(N) + v = np.zeros(N) + radiuser = np.zeros(N) + + # startbetingelser + u[0] = u0 + v[0] = v0 + x[0] = x0 + y[0] = y0 + radiuser[0] = np.sqrt((x[0]) ** 2 + (y[0]) ** 2) + + for n in range(1, N): + u[n] = u[n - 1] - 4 * np.pi ** 2 * x[n - 1] * tau / (radiuser[n - 1] ** 3) + v[n] = v[n - 1] - 4 * np.pi ** 2 * y[n - 1] * tau / (radiuser[n - 1] ** 3) + x[n] = x[n - 1] + u[n] * tau + y[n] = y[n - 1] + v[n] * tau + radiuser[n] = np.sqrt((x[n]) ** 2 + (y[n]) ** 2) + + + return x, y # posisjons- og farts-lister + +# startbetingelser: +x0 = 1 # Tenk deg at solen er i origo og at jorden starter i posisjon(1,0) +y0 = 0 +u0 = 0 # startfarten i x-retning er 0 +v0 = 2*3.1415623 # startfarten i y-retning er 2*pi +m = 1 / 333480 # dette er massen til Jorden i forhold til massen til Solen +tmax = 1 # Omløpstiden rundt Solen er 1(år) +tau = 0.01 # denne skrittlengden er såpass liten at plottet blir fint nok + +x1, y1 = EulerCromer(tmax, x0, y0, v0, u0, m, tau) + +# Plotter banen til planeten rundt sola +plt.figure() +plt.plot(x1, y1) +circle = plt.Circle((0, 0), radius=0.06, fc='yellow') +plt.gca().add_patch(circle) +plt.xlabel(r'x [AU]') +plt.ylabel(r'y [AU]') +plt.show() \ No newline at end of file diff --git a/Exercise 7/5.py b/Exercise 7/5.py new file mode 100644 index 0000000..7e344b5 --- /dev/null +++ b/Exercise 7/5.py @@ -0,0 +1,28 @@ +from re import search + +def check_equal(str1, str2): + for char in range(len(str1)): + if str1[char] != str2[char]: return False + return True + +def reversed_word(word): + return ''.join([word[len(word) - 1 - i] for i in range(len(word))]) + +def check_palindrome(string): + return string == reversed_word(string) + +def contains_string(str1, str2): + match = search(pattern=str2, string=str1) + return match.span()[0] if match != None else -1 + +if __name__ == "__main__": + print(check_equal('hei', 'hello')) + print(check_equal('hello', 'hello')) + print() + print(reversed_word('star desserts')) + print() + print(check_palindrome('agnes i senga')) + print(check_palindrome('hello')) + print() + print(contains_string('pepperkake', 'per')) + print(contains_string('pepperkake', 'ola')) diff --git a/Exercise 7/__init__.py b/Exercise 7/__init__.py new file mode 100644 index 0000000..e69de29