Commit 7734d778 authored by Geir Ola Lillestøl Tvinnereim's avatar Geir Ola Lillestøl Tvinnereim
Browse files

LF oving 10 lagt ut

parent 4583c99a
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# oppgave a\n",
"def get_chapters(filename, chapter_delimiter):\n",
" f = open(filename)\n",
" text = f.read()\n",
" f.close()\n",
" \n",
" chapters = text.split(chapter_delimiter)\n",
" del chapters[0]\n",
" return chapters\n",
"\n",
"\n",
"# oppgave b\n",
"def count_words(string_list, word):\n",
" word = word.lower()\n",
" word_counts = []\n",
" for string in string_list:\n",
" word_counts.append(string.lower().count(word))\n",
" return word_counts\n",
"\n",
"\n",
"# oppgave c\n",
"def create_numbers_to(number):\n",
" return list(range(1, number+1))\n",
"\n",
"\n",
"# oppgave d\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"def analyze_book(filename, chapter_delimiter, word):\n",
" chapters = get_chapters(filename, chapter_delimiter)\n",
" word_counts = count_words(chapters, word)\n",
" chapter_numbers = create_numbers_to(len(word_counts))\n",
" \n",
" plt.plot(chapter_numbers, word_counts)\n",
" plt.title('Antall ganger \"' + word + '\" dukker opp per kapittel i \"' + filename + '\"')\n",
" plt.xlabel('Kapittel')\n",
" plt.ylabel('Antall \"' + word + '\"')\n",
" plt.axis([1, len(chapters), 0, max(word_counts) + 3])\n",
" plt.show()\n",
"\n",
"\n",
"# oppgave e\n",
"def analyze_words(filename, chapter_delimiter, words):\n",
" for word in words:\n",
" analyze_book(filename, chapter_delimiter, word)\n",
"\n",
" \n",
"# testkode til oppgave e (denne er oppgitt)\n",
"words = ['rabbit', 'caterpillar', 'sister', 'cat', 'queen', 'turtle', 'hatter']\n",
"analyze_words('alice_in_wonderland.txt', 'CHAPTER', words)\n",
"\n",
"\n",
"# eksempel oppgave f\n",
"words = ['peter', 'peter pan', 'captain', 'crocodile', 'noodler', 'Tinker Bell', 'Queen Mab']\n",
"analyze_words('peter_pan.txt', 'Chapter', words)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"%matplotlib inline\n",
"import math\n",
"\n",
"print(\"Oppgave a:\\n\")\n",
"x_verdier = []\n",
"for i in range(0, 301):\n",
" x_verdier.append(i/10)\n",
"print(x_verdier)\n",
"\n",
"print(\"\\nOppgave b:\\n\")\n",
"y_verdier = []\n",
"for x in x_verdier:\n",
" y_verdier.append(math.sin(x))\n",
"print (y_verdier)\n",
"# Kunne også skrevet: y_verdier = [math.sin(x) for x in x_verdier]\n",
"\n",
"print(\"\\nOppgave c:\\n\")\n",
"plt.plot(x_verdier, y_verdier, 'r-')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def recursive_sum(n):\n",
" if n == 1:\n",
" return 1\n",
" return n + recursive_sum(n - 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"b)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def merge_sum(liste):\n",
" if len(liste)==1:\n",
" return liste[0]\n",
" mid = len(liste)//2\n",
" return merge_sum(liste[:mid])+merge_sum(liste[mid:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"c)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def find_smallest_element(liste, mini = float('inf')):\n",
" if (liste[0] <= mini):\n",
" mini = liste[0]\n",
" if (len(liste) <2):\n",
" return mini\n",
" else:\n",
" return(find_smallest_element(liste[1:len(liste)],mini))\n",
" \n",
"#alternativt\n",
"def find_smallest_element(liste):\n",
" if len(liste) == 1:\n",
" return liste[0]\n",
" else:\n",
" min = find_smallest_element(liste[1:])\n",
" if liste[0] < min:\n",
" return liste[0]\n",
" else:\n",
" return min"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"d)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def binary_search(numbers, element):\n",
" if len(numbers) == 1 and element != numbers[0]:\n",
" return -float('inf')\n",
" middle_index = int(len(numbers)/2)\n",
" middle_element = numbers[middle_index]\n",
" \n",
" if element == middle_element:\n",
" return middle_index\n",
" elif element > middle_element:\n",
" return binary_search(numbers[middle_index:], element) + middle_index\n",
" else:\n",
" return binary_search(numbers[:middle_index], element)\n",
" \n",
"#alternativt\n",
"\n",
"def binary_search(liste, verdi, imin, imax):\n",
" if(imax < imin):\n",
" return False\n",
" else:\n",
" imid = (imin+imax)//2 # Midtpunktet\n",
" if (verdi<liste[imid]):\n",
" return binary_search(liste,verdi,imin,imid-1)\n",
" elif (verdi>liste[imid]):\n",
" return binary_search(liste,verdi,imid+1,imax)\n",
" else:\n",
" return imid"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def load_board(filename):\n",
" # Fungerer på filer som er formatert som eksempelet nedenfor.\n",
" # feks:\n",
" #006905010970012305020004860503800020000000000080001907054100070207450093060703100\n",
" # Om du vil teste programmet kan du bruke A som gitt under.\n",
" #A = '006905010970012305020004860503800020000000000080001907054100070207450093060703100'\n",
" \n",
" f = open(filename,'r')\n",
" A = f.read()\n",
" B = [[int(A[i+9*j]) for i in range(9)] for j in range(9)]\n",
" return B\n",
"def save_board(filename,A):\n",
" B = ''\n",
" for i in range(9):\n",
" for j in range(9):\n",
" B = B + str(A[i][j])\n",
" \n",
" f = open(filename,'w')\n",
" for element in B:\n",
" f.write(element)\n",
" print(\"Brettet er lagret.\")\n",
"def print_board(A):\n",
" print(\"\")\n",
" print(\" 0 1 2 3 4 5 6 7 8 \")\n",
" print(\" +-------+-------+-------+\")\n",
" for i in range(9):\n",
" print(i,\"|\", A[i][0], A[i][1], A[i][2],\"|\", A[i][3], A[i][4], A[i][5], \"|\", A[i][6], A[i][7], A[i][8], \"|\")\n",
" if ((i+1) % 3 == 0):\n",
" print(\" +-------+-------+-------+\")\n",
"def get_square(A,x,y): # x = [0,1,2], y = [0,1,2]\n",
" B = [0 for j in range(9)]\n",
" for i in range(3):\n",
" for j in range(3):\n",
" B[i+3*j] = A[x*3+i][y*3+j]\n",
" return B\n",
"def get_horizontal(A,x): # x = [0,...,8]\n",
" B = [0 for i in range(9)]\n",
" for i in range(9):\n",
" B[i] = A[x][i]\n",
" return B\n",
"def get_vertical(A,y): # x = [0,...,8]\n",
" B = [0 for i in range(9)]\n",
" for j in range(9):\n",
" B[j] = A[j][y]\n",
" return B\n",
" \n",
"def get_input(A,filename):\n",
" print(\"Skriv 'avbryt' for å avslutte.\")\n",
" print(\"Skriv 'save' for å lagre og avslutte.\")\n",
" print(\"Skriv 'do' for å utføre et trekk.\")\n",
" print(\"Skriv 'undo' for å slette et trekk.\")\n",
" \n",
" choise = input()\n",
" if choise == 'save':\n",
" save_board(filename,A)\n",
" if choise == 'save' or choise == 'avbryt':\n",
" return -2,-1,-1\n",
" if choise == 'do' or choise == 'undo':\n",
" x = int(input(\"Skriv inn x-koordinat(0-8): \"))\n",
" y = int(input(\"Skriv inn y-koordinat(0-8): \"))\n",
" nn = -1\n",
" if choise =='do':\n",
" nn = int(input(\"Skriv inn et tall(1-9): \"))\n",
" return nn,x,y\n",
" \n",
" return nn,x,y\n",
"def is_finished(A):\n",
" for row in A:\n",
" for element in row:\n",
" if element == 0:\n",
" return 0\n",
" print(\"\\nGratulerer, du vant!\\n\")\n",
" return 1\n",
"def do_move(A,nn,x,y):\n",
" A1 = [[0 for i in range(9)] for j in range(9)]\n",
" for i in range(9):\n",
" for j in range(9):\n",
" A1[i][j] = A[i][j]\n",
" A1[x][y] = nn\n",
" return A1\n",
"def undo_move(A,x,y):\n",
" A1 = [[0 for i in range(9)] for j in range(9)]\n",
" for i in range(9):\n",
" for j in range(9):\n",
" A1[i][j] = A[i][j]\n",
" A1[x][y] = 0\n",
" return A1\n",
"def main():\n",
" filename = input(\"Skriv inn navnet på filen med brettet: \")\n",
" A = load_board(filename)\n",
" nn = 0\n",
" while 1:\n",
" print_board(A)\n",
" nn,x,y = get_input(A,filename)\n",
" if nn == -2:\n",
" return\n",
" elif nn == -1:\n",
" A = undo_move(A,x,y)\n",
" else:\n",
" A = do_move(A,nn,x,y)\n",
" if is_finished(A):\n",
" return\n",
" \n",
"main()\n",
"print('Spillet er avsluttet.')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Løsningsforslag"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"import numpy as np\n",
"\n",
"# Oppgave a\n",
"array = np.arange(0.0, 30.0, 0.1)\n",
"sin_array = np.sin(array)\n",
"plt.plot(array, sin_array)\n",
"plt.show()\n",
"\n",
"\n",
"# Oppgave b\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"def double_sine_plot(freq_mult):\n",
" array = np.arange(0.0, 15.0, 0.1)\n",
" sin_array = np.sin(array)\n",
" other_sin_array = np.sin(array*freq_mult)\n",
" \n",
" plt.plot(array, array*0, \"k-\")\n",
" plt.plot(array, sin_array, \"c:\")\n",
" plt.plot(array, other_sin_array, \"y:\")\n",
" plt.plot(array, sin_array + other_sin_array, \"r-\")\n",
" plt.title('To sinus-kurver plusset sammen')\n",
" plt.show()\n",
" \n",
"double_sine_plot(2)\n",
"\n",
"def double_sine_plot(freq_mult, amp_mult):\n",
" array = np.arange(0.0, 15.0, 0.1)\n",
" sin_array = np.sin(array)\n",
" other_sin_array = np.sin(array*freq_mult) * amp_mult\n",
" \n",
" plt.plot(array, array*0, \"k-\")\n",
" plt.plot(array, sin_array, \"c:\")\n",
" plt.plot(array, other_sin_array, \"y:\")\n",
" plt.plot(array, sin_array + other_sin_array, \"r-\")\n",
" plt.title('To sinus-kurver plusset sammen')\n",
" plt.show()\n",
" \n",
"double_sine_plot(12,0.3)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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