From dbb4d719da64d38188e1bd088779c53a7d9d635f Mon Sep 17 00:00:00 2001 From: Manuella Date: Mon, 29 Jan 2024 15:26:15 +0000 Subject: [PATCH 1/3] add lab02 --- Lab02/Lab02.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Lab02/Lab02.py diff --git a/Lab02/Lab02.py b/Lab02/Lab02.py new file mode 100644 index 0000000..27cf001 --- /dev/null +++ b/Lab02/Lab02.py @@ -0,0 +1,5 @@ +# prints out all odd numbers between 0 and 100 +start, end = 0, 100 +for num in range(start, end + 1): + if num % 2 != 0: + print(num, end = " ") From 25485d62663a3d7f8ea3afc4c82a2f62ca8b0ecc Mon Sep 17 00:00:00 2001 From: Manuella Date: Mon, 29 Jan 2024 15:38:31 +0000 Subject: [PATCH 2/3] add lab03-1 --- Lab03/2659993_Lab03-1.ipynb | 683 ++++++++++++++++++++++++++++++++++++ 1 file changed, 683 insertions(+) create mode 100644 Lab03/2659993_Lab03-1.ipynb diff --git a/Lab03/2659993_Lab03-1.ipynb b/Lab03/2659993_Lab03-1.ipynb new file mode 100644 index 0000000..efee7ef --- /dev/null +++ b/Lab03/2659993_Lab03-1.ipynb @@ -0,0 +1,683 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0a43ec65", + "metadata": {}, + "source": [ + "# Lab03-1: Operators and Strings" + ] + }, + { + "cell_type": "markdown", + "id": "d6e1ee13", + "metadata": {}, + "source": [ + "### Student ID: 2659993" + ] + }, + { + "cell_type": "markdown", + "id": "c9de655c", + "metadata": {}, + "source": [ + "#### Operators: Exercises 1-2 from Chapter 2" + ] + }, + { + "cell_type": "markdown", + "id": "c805caca", + "metadata": {}, + "source": [ + "##### Exercise 1" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fc08905b", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "cannot assign to literal here. Maybe you meant '==' instead of '='? (546313846.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[3], line 3\u001b[1;36m\u001b[0m\n\u001b[1;33m 42 = n\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot assign to literal here. Maybe you meant '==' instead of '='?\n" + ] + } + ], + "source": [ + "# We’ve seen that n = 42 is legal. What about 42 = n?\n", + "\n", + "42 = n" + ] + }, + { + "cell_type": "markdown", + "id": "d44032e1", + "metadata": {}, + "source": [ + "- Answer: 42 = n is not the same as n = 42. It produces a syntax error." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "5b5ac2da", + "metadata": {}, + "outputs": [], + "source": [ + "# How about x = y = 1?\n", + "\n", + "x = y = 1" + ] + }, + { + "cell_type": "markdown", + "id": "01ae1ea1", + "metadata": {}, + "source": [ + "- Answer: it is valid. X and Y are assigned the same value 1." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "2b68ca3f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "# In some languages every statement ends with a semi-colon, ;.\n", + "# What happens if you put a semi-colon at the end of a Python statement?\n", + "\n", + "w = 3;\n", + "print(w);" + ] + }, + { + "cell_type": "markdown", + "id": "c5938fe3", + "metadata": {}, + "source": [ + "- Answer: putting a semi-colon at the end of a statement does not seem to affect the code from running normally" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "178de052", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (2514894257.py, line 4)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[8], line 4\u001b[1;36m\u001b[0m\n\u001b[1;33m print(z).\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "# What if you put a period at the end of a statement?\n", + "\n", + "z = 4.\n", + "print(z)." + ] + }, + { + "cell_type": "markdown", + "id": "b0a0e801", + "metadata": {}, + "source": [ + "- Answer: apparently putting a period causes a syntax error when running the code." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "9bd90e27", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (737226731.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[11], line 3\u001b[1;36m\u001b[0m\n\u001b[1;33m x y\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "# In math notation you can multiply x and y like this: x y. What happens if you try that in Python?\n", + "\n", + "x y" + ] + }, + { + "cell_type": "markdown", + "id": "52cb6170", + "metadata": {}, + "source": [ + "- Answer: Python returns an error for this syntax." + ] + }, + { + "cell_type": "markdown", + "id": "596f407a", + "metadata": {}, + "source": [ + "##### Exercise 2" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "205f43c8", + "metadata": {}, + "outputs": [], + "source": [ + "# The volume of a sphere with radius r is 4/3 π r3. What is the volume of a sphere with radius 5?" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "dac3f4d4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "523.5987755982989\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "r = 5\n", + "volume = (4 / 3) * np.pi * r**3\n", + "print(volume)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "0e42db6b", + "metadata": {}, + "outputs": [], + "source": [ + "# Suppose the cover price of a book is $24.95, but bookstores get a 40% discount.\n", + "# Shipping costs $3 for the first copy and 75 cents for each additional copy.\n", + "# What is the total wholesale cost for 60 copies?" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "f52a5500", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "945.4499999999999\n" + ] + } + ], + "source": [ + "b = 60\n", + "bookstore_price = 24.95 * 0.6\n", + "shipping = 3 + 0.75 * (b - 1)\n", + "cost = (bookstore_price * b) + shipping\n", + "print(cost)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "3b7ae518", + "metadata": {}, + "outputs": [], + "source": [ + "# If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile),\n", + "# then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "bcaeb8d4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "76.2\n" + ] + } + ], + "source": [ + "# 8:15 = 8.25 minutes; 7:12 = 7.2 minutes\n", + "total_time = 2 * (8.25*2 + 3*7.2)\n", + "print(total_time)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "2d435c20", + "metadata": {}, + "outputs": [], + "source": [ + "# 76.2 minutes = 1 hour, 16 minutes and 12 seconds" + ] + }, + { + "cell_type": "markdown", + "id": "1f0f85a5", + "metadata": {}, + "source": [ + "- Answer: You will get home for breakfast at 8:08 am" + ] + }, + { + "cell_type": "markdown", + "id": "8e20786b", + "metadata": {}, + "source": [ + "#### Strings: exercises 1-5 from Chapter 8" + ] + }, + { + "cell_type": "markdown", + "id": "1ceade82", + "metadata": {}, + "source": [ + "##### Exercise 2" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "fb4e7715", + "metadata": {}, + "outputs": [], + "source": [ + "# There is a string method called count that is similar to the function in Section 8.7.\n", + "# Read the documentation of this method and write an invocation that counts the number of a’s in 'banana'." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "a05329ea", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "x = \"banana\"\n", + "print(x.count(\"a\"))" + ] + }, + { + "cell_type": "markdown", + "id": "a1f63b4a", + "metadata": {}, + "source": [ + "##### Execise 3" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "6cacb826", + "metadata": {}, + "outputs": [], + "source": [ + "# A string slice can take a third index that specifies the “step size”;\n", + "# that is, the number of spaces between successive characters.\n", + "# A step size of 2 means every other character; 3 means every third, etc.\n", + "\n", + "# >>> fruit = 'banana'\n", + "# >>> fruit[0:5:2]\n", + "# 'bnn'\n", + "# A step size of -1 goes through the word backwards, so the slice [::-1] generates a reversed string.\n", + "\n", + "# Use this idiom to write a one-line version of is_palindrome from Exercise 3." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "d6cc1ab4", + "metadata": {}, + "outputs": [], + "source": [ + "def is_palindrome(word):\n", + " if word[::-1] == word:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "bc40cf3d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "word = \"noon\"\n", + "is_palindrome(word)" + ] + }, + { + "cell_type": "markdown", + "id": "62a01604", + "metadata": {}, + "source": [ + "##### Exercise 4" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "c04af84f", + "metadata": {}, + "outputs": [], + "source": [ + "# The following functions are all intended to check whether a string contains any lowercase letters,\n", + "# but at least some of them are wrong. For each function, describe what the function actually does\n", + "# (assuming that the parameter is a string)." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "b278c755", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def any_lowercase1(s):\n", + " for c in s:\n", + " if c.islower():\n", + " return True\n", + " else:\n", + " return False\n", + "\n", + "s = \"suN\"\n", + "any_lowercase1(s)" + ] + }, + { + "cell_type": "markdown", + "id": "2486b7f5", + "metadata": {}, + "source": [ + "- Answer: The function returns True if the first letter of the string is lower case. Otherwise, it returns False." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "65c29de4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'True'" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def any_lowercase2(s):\n", + " for c in s:\n", + " if 'c'.islower():\n", + " return 'True'\n", + " else:\n", + " return 'False'\n", + "\n", + "any_lowercase2(s)" + ] + }, + { + "cell_type": "markdown", + "id": "c5566996", + "metadata": {}, + "source": [ + "- Answer: this function is wrong because it puts \"c\" between quotation marks, thus it always returns True, no matter what is the value assigned to s." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "38e004b1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def any_lowercase3(s):\n", + " for c in s:\n", + " flag = c.islower()\n", + " return flag\n", + "\n", + "any_lowercase3(s)" + ] + }, + { + "cell_type": "markdown", + "id": "8bdd445b", + "metadata": {}, + "source": [ + "- Answer: it returns True if all the letters are lower case or only the first letter is capitalised. Otherwise, it returns false." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "0bd37a51", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def any_lowercase4(s):\n", + " flag = False\n", + " for c in s:\n", + " flag = flag or c.islower()\n", + " return flag\n", + "\n", + "any_lowercase4(s)" + ] + }, + { + "cell_type": "markdown", + "id": "38589140", + "metadata": {}, + "source": [ + "- Answer: the function returns True if there is at least one lower case letter in the string, no matter the position. Otherwise, it returns False." + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "0e17bb8e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def any_lowercase5(s):\n", + " for c in s:\n", + " if not c.islower():\n", + " return False\n", + " return True\n", + "\n", + "any_lowercase5(s)" + ] + }, + { + "cell_type": "markdown", + "id": "984ae467", + "metadata": {}, + "source": [ + "- Answer: the function only returns True if all letters are lower case." + ] + }, + { + "cell_type": "markdown", + "id": "9bf18987", + "metadata": {}, + "source": [ + "##### Exercise 5" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "95063789", + "metadata": {}, + "outputs": [], + "source": [ + "# A Caesar cypher is a weak form of encryption that involves “rotating” each letter by a fixed number of places. To rotate a letter means to shift it through the alphabet, wrapping around to the beginning if necessary, so ’A’ rotated by 3 is ’D’ and ’Z’ rotated by 1 is ’A’.\n", + "\n", + "# To rotate a word, rotate each letter by the same amount. For example, “cheer” rotated by 7 is “jolly” and “melon” rotated by -10 is “cubed”. In the movie 2001: A Space Odyssey, the ship computer is called HAL, which is IBM rotated by -1.\n", + "\n", + "# Write a function called rotate_word that takes a string and an integer as parameters, and returns a new string that contains the letters from the original string rotated by the given amount.\n", + "\n", + "# You might want to use the built-in function ord, which converts a character to a numeric code, and chr, which converts numeric codes to characters. Letters of the alphabet are encoded in alphabetical order, so for example:\n", + "\n", + "# >>> ord('c') - ord('a')\n", + "# 2\n", + "# Because 'c' is the two-eth letter of the alphabet. But beware: the numeric codes for upper case letters are different.\n", + "\n", + "# Potentially offensive jokes on the Internet are sometimes encoded in ROT13, which is a Caesar cypher with rotation 13. If you are not easily offended, find and decode some of them. Solution: https://thinkpython.com/code/rotate.py." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "00abbc79", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'jolly'" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def rotate_word(string: str, num: int) -> str:\n", + " result = ''\n", + " for i in string:\n", + " i = chr(ord(i) + num)\n", + " result += i\n", + " return result\n", + "\n", + "string = \"cheer\"\n", + "num = 7\n", + "rotate_word(string, num)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 88814ef3350321fced8c17d72bc130aabc90a099 Mon Sep 17 00:00:00 2001 From: Manuella Date: Sun, 4 Feb 2024 12:52:45 +0000 Subject: [PATCH 3/3] Lab04-1 --- Lab04/Lab/gal.py | 770 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 770 insertions(+) create mode 100644 Lab04/Lab/gal.py diff --git a/Lab04/Lab/gal.py b/Lab04/Lab/gal.py new file mode 100644 index 0000000..aeadf7e --- /dev/null +++ b/Lab04/Lab/gal.py @@ -0,0 +1,770 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "2c4f8183", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 49 mystery mystery\n", + "1 2\n", + "2 3\n", + "2 3\n", + "4 3 1\n", + "3 4\n", + "5 4 2 1\n", + "4 4\n", + "8 3 5 2\n", + "5 7\n", + "15 11 8 9 6 3 4\n", + "6 2\n", + "9 5\n", + "7 3\n", + "14 13 8\n", + "8 5\n", + "12 11 5 4 7\n", + "9 6\n", + "26 22 15 10 6 5\n", + "10 3\n", + "20 17 9\n", + "11 4\n", + "16 12 5 8\n", + "12 5\n", + "16 14 13 11 8\n", + "13 3\n", + "14 12 7\n", + "14 6\n", + "19 12 13 16 18 7\n", + "15 4\n", + "25 16 5 9\n", + "16 7\n", + "25 24 18 15 11 12 14\n", + "17 3\n", + "23 20 10\n", + "18 4\n", + "24 19 16 14\n", + "19 3\n", + "18 24 14\n", + "20 9\n", + "35 33 27 22 23 32 40 17 10\n", + "21 4\n", + "34 24 30 9\n", + "22 5\n", + "28 27 26 20 9\n", + "23 3\n", + "32 17 20\n", + "24 6\n", + "30 25 16 18 21 19\n", + "25 5\n", + "29 15 26 16 24\n", + "26 4\n", + "28 22 9 25\n", + "27 4\n", + "33 28 20 22\n", + "28 7\n", + "38 29 27 33 35 22 26\n", + "29 4\n", + "37 30 28 25\n", + "30 4\n", + "37 29 24 21\n", + "31 2\n", + "36 34\n", + "32 4\n", + "41 40 23 20\n", + "33 4\n", + "35 20 27 28\n", + "34 4\n", + "42 36 21 31\n", + "35 5\n", + "44 38 20 33 28\n", + "36 5\n", + "46 39 34 42 31\n", + "37 5\n", + "45 38 43 29 30\n", + "38 4\n", + "43 35 28 37\n", + "39 2\n", + "46 36\n", + "40 4\n", + "47 41 32 20\n", + "41 3\n", + "47 32 40\n", + "42 2\n", + "34 36\n", + "43 5\n", + "48 45 44 38 37\n", + "44 4\n", + "49 48 35 43\n", + "45 4\n", + "48 49 37 43\n", + "46 2\n", + "36 39\n", + "47 2\n", + "40 41\n", + "48 4\n", + "49 44 43 45\n", + "49 3\n", + "44 48 45\n", + "\n" + ] + } + ], + "source": [ + "# Read in Lab04-1.gal\n", + "with open(r\"C:\\Users\\manue\\OneDrive - University of Glasgow\\URBAN5123 - Programming Tools for UA\\Labs\\PTUA2024\\Lab04\\Lab\\Lab04-1.gal\", \"r\") as f:\n", + " file_content = f.read()\n", + "print(file_content)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "73940d18", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['0 49 mystery mystery', '1 2', '2 3', '2 3', '4 3 1', '3 4', '5 4 2 1', '4 4', '8 3 5 2', '5 7', '15 11 8 9 6 3 4', '6 2', '9 5', '7 3', '14 13 8', '8 5', '12 11 5 4 7', '9 6', '26 22 15 10 6 5', '10 3', '20 17 9', '11 4', '16 12 5 8', '12 5', '16 14 13 11 8', '13 3', '14 12 7', '14 6', '19 12 13 16 18 7', '15 4', '25 16 5 9', '16 7', '25 24 18 15 11 12 14', '17 3', '23 20 10', '18 4', '24 19 16 14', '19 3', '18 24 14', '20 9', '35 33 27 22 23 32 40 17 10', '21 4', '34 24 30 9', '22 5', '28 27 26 20 9', '23 3', '32 17 20', '24 6', '30 25 16 18 21 19', '25 5', '29 15 26 16 24', '26 4', '28 22 9 25', '27 4', '33 28 20 22', '28 7', '38 29 27 33 35 22 26', '29 4', '37 30 28 25', '30 4', '37 29 24 21', '31 2', '36 34', '32 4', '41 40 23 20', '33 4', '35 20 27 28', '34 4', '42 36 21 31', '35 5', '44 38 20 33 28', '36 5', '46 39 34 42 31', '37 5', '45 38 43 29 30', '38 4', '43 35 28 37', '39 2', '46 36', '40 4', '47 41 32 20', '41 3', '47 32 40', '42 2', '34 36', '43 5', '48 45 44 38 37', '44 4', '49 48 35 43', '45 4', '48 49 37 43', '46 2', '36 39', '47 2', '40 41', '48 4', '49 44 43 45', '49 3', '44 48 45']\n" + ] + } + ], + "source": [ + "# Save the contents to a list (assuming each line is a separate item)\n", + "data = file_content.splitlines()\n", + "\n", + "# Print the object to confirm\n", + "print(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "e76ce1c2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['1 2', '2 3', '2 3', '4 3 1', '3 4', '5 4 2 1', '4 4', '8 3 5 2', '5 7', '15 11 8 9 6 3 4', '6 2', '9 5', '7 3', '14 13 8', '8 5', '12 11 5 4 7', '9 6', '26 22 15 10 6 5', '10 3', '20 17 9', '11 4', '16 12 5 8', '12 5', '16 14 13 11 8', '13 3', '14 12 7', '14 6', '19 12 13 16 18 7', '15 4', '25 16 5 9', '16 7', '25 24 18 15 11 12 14', '17 3', '23 20 10', '18 4', '24 19 16 14', '19 3', '18 24 14', '20 9', '35 33 27 22 23 32 40 17 10', '21 4', '34 24 30 9', '22 5', '28 27 26 20 9', '23 3', '32 17 20', '24 6', '30 25 16 18 21 19', '25 5', '29 15 26 16 24', '26 4', '28 22 9 25', '27 4', '33 28 20 22', '28 7', '38 29 27 33 35 22 26', '29 4', '37 30 28 25', '30 4', '37 29 24 21', '31 2', '36 34', '32 4', '41 40 23 20', '33 4', '35 20 27 28', '34 4', '42 36 21 31', '35 5', '44 38 20 33 28', '36 5', '46 39 34 42 31', '37 5', '45 38 43 29 30', '38 4', '43 35 28 37', '39 2', '46 36', '40 4', '47 41 32 20', '41 3', '47 32 40', '42 2', '34 36', '43 5', '48 45 44 38 37', '44 4', '49 48 35 43', '45 4', '48 49 37 43', '46 2', '36 39', '47 2', '40 41', '48 4', '49 44 43 45', '49 3', '44 48 45']\n" + ] + } + ], + "source": [ + "# Delete first element in data\n", + "del(data[0])\n", + "print(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "d3d27106", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 2), (2, 3), (2, 3), (4, 3, 1), (3, 4), (5, 4, 2, 1), (4, 4), (8, 3, 5, 2), (5, 7), (15, 11, 8, 9, 6, 3, 4), (6, 2), (9, 5), (7, 3), (14, 13, 8), (8, 5), (12, 11, 5, 4, 7), (9, 6), (26, 22, 15, 10, 6, 5), (10, 3), (20, 17, 9), (11, 4), (16, 12, 5, 8), (12, 5), (16, 14, 13, 11, 8), (13, 3), (14, 12, 7), (14, 6), (19, 12, 13, 16, 18, 7), (15, 4), (25, 16, 5, 9), (16, 7), (25, 24, 18, 15, 11, 12, 14), (17, 3), (23, 20, 10), (18, 4), (24, 19, 16, 14), (19, 3), (18, 24, 14), (20, 9), (35, 33, 27, 22, 23, 32, 40, 17, 10), (21, 4), (34, 24, 30, 9), (22, 5), (28, 27, 26, 20, 9), (23, 3), (32, 17, 20), (24, 6), (30, 25, 16, 18, 21, 19), (25, 5), (29, 15, 26, 16, 24), (26, 4), (28, 22, 9, 25), (27, 4), (33, 28, 20, 22), (28, 7), (38, 29, 27, 33, 35, 22, 26), (29, 4), (37, 30, 28, 25), (30, 4), (37, 29, 24, 21), (31, 2), (36, 34), (32, 4), (41, 40, 23, 20), (33, 4), (35, 20, 27, 28), (34, 4), (42, 36, 21, 31), (35, 5), (44, 38, 20, 33, 28), (36, 5), (46, 39, 34, 42, 31), (37, 5), (45, 38, 43, 29, 30), (38, 4), (43, 35, 28, 37), (39, 2), (46, 36), (40, 4), (47, 41, 32, 20), (41, 3), (47, 32, 40), (42, 2), (34, 36), (43, 5), (48, 45, 44, 38, 37), (44, 4), (49, 48, 35, 43), (45, 4), (48, 49, 37, 43), (46, 2), (36, 39), (47, 2), (40, 41), (48, 4), (49, 44, 43, 45), (49, 3), (44, 48, 45)]\n" + ] + } + ], + "source": [ + "# Split each string into a list of integers\n", + "list_of_lists = [[int(x) for x in item.split()] for item in data]\n", + "\n", + "# Convert the list of lists to a list of tuples\n", + "tuple_list = [(item[0], *item[1:]) for item in list_of_lists]\n", + "print(tuple_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "24162e29", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{1: [2, 3], 2: [4, 3, 1], 3: [5, 4, 2, 1], 4: [8, 3, 5, 2], 5: [15, 11, 8, 9, 6, 3, 4], 6: [9, 5], 7: [14, 13, 8], 8: [12, 11, 5, 4, 7], 9: [26, 22, 15, 10, 6, 5], 10: [20, 17, 9], 11: [16, 12, 5, 8], 12: [16, 14, 13, 11, 8], 13: [14, 12, 7], 14: [19, 12, 13, 16, 18, 7], 15: [25, 16, 5, 9], 16: [25, 24, 18, 15, 11, 12, 14], 17: [23, 20, 10], 18: [24, 19, 16, 14], 19: [18, 24, 14], 20: [35, 33, 27, 22, 23, 32, 40, 17, 10], 21: [34, 24, 30, 9], 22: [28, 27, 26, 20, 9], 23: [32, 17, 20], 24: [30, 25, 16, 18, 21, 19], 25: [29, 15, 26, 16, 24], 26: [28, 22, 9, 25], 27: [33, 28, 20, 22], 28: [38, 29, 27, 33, 35, 22, 26], 29: [37, 30, 28, 25], 30: [37, 29, 24, 21], 31: [36, 34], 32: [41, 40, 23, 20], 33: [35, 20, 27, 28], 34: [42, 36, 21, 31], 35: [44, 38, 20, 33, 28], 36: [46, 39, 34, 42, 31], 37: [45, 38, 43, 29, 30], 38: [43, 35, 28, 37], 39: [46, 36], 40: [47, 41, 32, 20], 41: [47, 32, 40], 42: [34, 36], 43: [48, 45, 44, 38, 37], 44: [49, 48, 35, 43], 45: [48, 49, 37, 43], 46: [36, 39], 47: [40, 41], 48: [49, 44, 43, 45], 49: [44, 48, 45]}\n" + ] + } + ], + "source": [ + "# Return a Python dictionary where the key is the id of a spatial unit and the value is a list of the ids of its neighbors.\n", + "gal_dict = {}\n", + "key = None\n", + "values = []\n", + "\n", + "for i, element in enumerate(tuple_list):\n", + " if i % 2 == 0:\n", + " if key is not None:\n", + " gal_dict[key] = values\n", + " key = element[0]\n", + " values = []\n", + " else:\n", + " values.extend(element) # Add all elements from odd-indexed tuples to values\n", + "\n", + "# Add the last key-value pair\n", + "gal_dict[key] = values\n", + "\n", + "print(gal_dict)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "a952c9f4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{2: [1, 6, 31, 39, 42, 46, 47], 3: [2, 7, 10, 13, 17, 19, 23, 41, 49], 4: [3, 4, 11, 15, 18, 21, 26, 27, 29, 30, 32, 33, 34, 38, 40, 44, 45, 48], 7: [5, 16, 28], 5: [8, 12, 22, 25, 35, 36, 37, 43], 6: [9, 14, 24], 9: [20]}\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8pXeV/AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAdgklEQVR4nO3df3RX9X348VcIklAGcUIJRAKG1h8cc7Q7wa4BmbNqeoBxttOewo5rsfw4a04oCJmuIDt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NOxMAYEjK6WmaiIhFixbFiRMnYsOGDdHW1haVlZXR1NQU06ZNi4iItra2Pp858pGPfCS6urrigQceiL/5m7+Jiy++ON773vfGJz/5yTfvLACAISvnGImIqKuri7q6ugF/tmPHjn5rK1eujJUrVw7mrgCAC5zvpgEAkhIjAEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkJQYAQCSEiMAQFJiBABISowAAEmJEQAgKTECACQlRgCApMQIAJCUGAEAkhIjAEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkJQYAQCSEiMAQFJiBABISowAAEmJEQAgKTECACQlRgCApMQIAJCUGAEAkhIjAEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhqUDGyefPmqKioiOLi4qiqqoo9e/a85v7d3d2xfv36mDZtWhQVFcU73vGO2L59+6AGBgAuLCNzPaCxsTFWr14dmzdvjtmzZ8fnP//5mDt3bhw6dCimTp064DELFy6MX/7yl/HQQw/FO9/5zmhvb4/Tp0+/4eEBgKEv5xjZuHFjLFu2LJYvXx4REZs2bYpdu3bFli1boqGhod/+TzzxROzevTuOHDkSl1xySUREXHbZZW9sagDggpHT0zSnTp2KAwcORE1NTZ/1mpqa2Lt374DHfPOb34yZM2fGpz71qbj00kvjiiuuiDvuuCN+85vfnPN+uru7o7Ozs88GAFyYcroy0tHREWfOnInS0tI+66WlpXH8+PEBjzly5Eg888wzUVxcHI8//nh0dHREXV1d/OpXvzrn60YaGhri3nvvzWU0AGCIGtQLWAsKCvrczrKs39pZPT09UVBQEI899li8+93vjnnz5sXGjRtjx44d57w6sm7dujh58mTvdvTo0cGMCQAMATldGZkwYUIUFhb2uwrS3t7e72rJWZMnT45LL700SkpKetdmzJgRWZbFL37xi7j88sv7HVNUVBRFRUW5jAYADFE5XRkZNWpUVFVVRXNz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jHPhYRET09PZFlWYwcOTKefPLJeO9739vvuKKioigqKsplNABgiMrpysioUaOiqqoqmpub+6w3NzfHrFmz+u0/bty4+MEPfhAHDx7s3Wpra+PKK6+MgwcPxh//8R+/sekBgCEvpysjERH19fXx4Q9/OGbOnBnV1dWxbdu2aG1tjdra2oh49SmWY8eOxaOPPhojRoyIysrKPsdPnDgxiouL+60DAMNTzjGyaNGiOHHiRGzYsCHa2tqisrIympqaYtq0aRER0dbW9ns/cwQA4KycYyQioq6uLurq6gb82Y4dO17z2HvuuSfuueeewdwtAHAB8t00AEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkJQYAQCSEiMAQFJiBABISowAAEmJEQAgKTECACQlRgCApMQIAJCUGAEAkhIjAEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkJQYAQCSEiMAQFJiBABISowAAEmJEQAgKTECACQlRgCApMQIAJCUGAEAkhIjAEBSYgQASEqMAABJiREAICkxAgAkJUYAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACCpQcXI5s2bo6KiIoqLi6Oqqir27Nlzzn2//vWvxy233BJvf/vbY9y4cVFdXR27du0a9MAAwIUl5xhpbGyM1atXx/r166OlpSXmzJkTc+fOjdbW1gH3f/rpp+OWW26JpqamOHDgQNx4442xYMGCaGlpecPDAwBDX84xsnHjxli2bFksX748ZsyYEZs2bYry8vLYsmXLgPtv2rQp/vZv/zauu+66uPzyy+P++++Pyy+/PL71rW+94eEBgKEvpxg5depUHDhwIGpqavqs19TUxN69e1/X7+jp6Ymurq645JJLzrlPd3d3dHZ29tkAgAtTTjHS0dERZ86cidLS0j7rpaWlcfz48df1Oz7zmc/Eyy+/HAsXLjznPg0NDVFSUtK7lZeX5zImADCEDOoFrAUFBX1uZ1nWb20gO3fujHvuuScaGxtj4sSJ59xv3bp1cfLk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27LjxxhvjO9/5TkycODF++tOfntdPe34r2bdvX5w5c6b39g9/+MO45ZZb4oMf/GDCqfLnk5/8ZGzdujUeeeSRuPrqq2P//v2xZMmSKCkpidtvvz31eHmzfPny+OEPfxhf+MIXoqysLL74xS/GzTffHIcOHYpLL700f4NkZO3t7VlEZLt37049SjJ/+Id/mD344IOpx8irrq6u7PLLL8+am5uzG264Ibv99ttTj5Q3d999d3bttdemHiOpj3/849n111+feoy3jNtvvz17xzvekfX09KQeJS/mz5+fLV26tM/a+9///uxDH/pQoony75VXXskKCwuzf/u3f+uzfu2112br16/P6yzD8mma33Xy5MmIiLjkkksST5J/Z86ciS9/+cvx8ssvR3V1depx8mrFihUxf/78uPnmm1OPksTzzz8fZWVlUVFREX/5l38ZR44cST1SXn3zm9+MmTNnxgc/+MGYOHFi/NEf/VH8y7/8S+qxkjh16lR88YtfjKVLl76pX0b6Vnb99dfHf/zHf8Rzzz0XERH//d//Hc8880zMmzcv8WT5c/r06Thz5kwUFxf3WR89enQ888wz+R0mr+nzFtTT05MtWLBg2P0f0rPPPpuNGTMmKywszEpKSrJvf/vbqUfKq507d2aVlZXZb37zmyzLsmF3ZaSpqSn76le/mj377LO9V4ZKS0uzjo6O1KPlTVFRUVZUVJStW7cu+/73v59t3bo1Ky4uzh555JHUo+VdY2NjVlhYmB07diz1KHnT09OTrV27NisoKMhGjhyZFRQUZPfff3/qsfKuuro6u+GGG7Jjx45lp0+fzr7whS9kBQUF2RVXXJHXOYZ9jNTV1WXTpk3Ljh49mnqUvOru7s6ef/75bN++fdnatWuzCRMmZD/60Y9Sj5UXra2t2cSJE7ODBw/2rg23GPldL730UlZaWpp95jOfST1K3lx00UVZdXV1n7WVK1dm73nPexJNlE5NTU32Z3/2Z6nHyKudO3dmU6ZMyXbu3Jk9++yz2aOPPppdcskl2Y4dO1KPllc/+clPsj/5kz/JIiIrLCzMrrvuuuyv/uqvshkzZuR1jmEdIx/72MeyKVOmZEeOHEk9SnI33XRT9td//depx8iLxx9/vPc/vLNbRGQFBQVZYWFhdvr06dQjJnHzzTdntbW1qcfIm6lTp2bLli3rs7Z58+asrKws0URp/OxnP8tGjBiRfeMb30g9Sl5NmTIle+CBB/qs/cM//EN25ZVXJpoorZdeeil78cUXsyzLsoULF2bz5s3L6/0Py3fTZFkWK1eujMcffzyeeuqpqKioSD1SclmWRXd3d+ox8uKmm26KH/zgB33WlixZEldddVV8/OMfH3bvKomI6O7ujsOHD8ecOXNSj5I3s2fP7veW/ueee673Sz+Hi4cffjgmTpwY8+fPTz1KXr3yyisxYkTfl00WFhYOu7f2njVmzJgYM2ZM/PrXv45du3bFpz71qbze/7CMkRUrVsSXvvSl+Nd//dcYO3ZsHD9+PCIiSkpKYvTo0YmnO//uuuuumDt3bpSXl0dXV1d8+ctfjqeeeiqeeOKJ1KPlxdixY6OysrLP2pgxY2L8+PH91i9Ud9xxRyxYsCCmTp0a7e3tcd9990VnZ2fcdtttqUfLmzVr1sSsWbPi/vvvj4ULF8Z//dd/xbZt22Lbtm2pR8ubnp6eePjhh+O2226LkSOH1z8HCxYsiH/8x3+MqVOnxtVXXx0tLS2xcePGWLp0aerR8mrXrl2RZVlceeWV8ZOf/CTuvPPOuPLKK2PJkiX5HSSv12HeIiJiwO3hhx9OPVpeLF26NJs2bVo2atSo7O1vf3t20003ZU8++WTqsZIabq8ZWbRoUTZ58uTsoosuysrKyrL3v//9w+Y1Q//Xt771rayysjIrKirKrrrqqmzbtm2pR8qrXbt2ZRGR/fjHP049St51dnZmt99+ezZ16tSsuLg4mz59erZ+/fqsu7s79Wh51djYmE2fPj0bNWpUNmnSpGzFihXZ//zP/+R9joIsy7L85g8AwP/nc0YAgKTECACQlBgBAJISIwBAUmIEAEhKjAAASYkRACApMQIAJCVGAICkxAgAkJQYAQCSEiMAQFL/CzZSslmT4SLDAAAAAElFTkSuQmCC", + "text/plain": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Take a gal dictionary from 1 and return a second dictionary that has the histogram for the neighbor cardinalities.\n", + "# In the second dictionary, the key is the number of neighbors and the value is the list of ids that have key neighbors.\n", + "# It is up to you if you want to draw the histogram.\n", + "\n", + "dict_2 = {}\n", + "for key, val in gal_dict.items():\n", + " value_count = len(val)\n", + " if value_count in dict_2:\n", + " dict_2[value_count].append(key)\n", + " else:\n", + " dict_2[value_count] = [key]\n", + "\n", + "print(dict_2)\n", + "\n", + "# Histogram\n", + "import matplotlib.pyplot as plt\n", + "plt.hist(dict_2)\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "0d499a85", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 [2, 3]\n", + "1 says 2 is a neighbor\n", + "ok\n", + "1 says 3 is a neighbor\n", + "ok\n", + "2 [4, 3, 1]\n", + "2 says 4 is a neighbor\n", + "ok\n", + "2 says 3 is a neighbor\n", + "ok\n", + "2 says 1 is a neighbor\n", + "ok\n", + "3 [5, 4, 2, 1]\n", + "3 says 5 is a neighbor\n", + "ok\n", + "3 says 4 is a neighbor\n", + "ok\n", + "3 says 2 is a neighbor\n", + "ok\n", + "3 says 1 is a neighbor\n", + "ok\n", + "4 [8, 3, 5, 2]\n", + "4 says 8 is a neighbor\n", + "ok\n", + "4 says 3 is a neighbor\n", + "ok\n", + "4 says 5 is a neighbor\n", + "ok\n", + "4 says 2 is a neighbor\n", + "ok\n", + "5 [15, 11, 8, 9, 6, 3, 4]\n", + "5 says 15 is a neighbor\n", + "ok\n", + "5 says 11 is a neighbor\n", + "ok\n", + "5 says 8 is a neighbor\n", + "ok\n", + "5 says 9 is a neighbor\n", + "ok\n", + "5 says 6 is a neighbor\n", + "ok\n", + "5 says 3 is a neighbor\n", + "ok\n", + "5 says 4 is a neighbor\n", + "ok\n", + "6 [9, 5]\n", + "6 says 9 is a neighbor\n", + "ok\n", + "6 says 5 is a neighbor\n", + "ok\n", + "7 [14, 13, 8]\n", + "7 says 14 is a neighbor\n", + "ok\n", + "7 says 13 is a neighbor\n", + "ok\n", + "7 says 8 is a neighbor\n", + "ok\n", + "8 [12, 11, 5, 4, 7]\n", + "8 says 12 is a neighbor\n", + "ok\n", + "8 says 11 is a neighbor\n", + "ok\n", + "8 says 5 is a neighbor\n", + "ok\n", + "8 says 4 is a neighbor\n", + "ok\n", + "8 says 7 is a neighbor\n", + "ok\n", + "9 [26, 22, 15, 10, 6, 5]\n", + "9 says 26 is a neighbor\n", + "ok\n", + "9 says 22 is a neighbor\n", + "ok\n", + "9 says 15 is a neighbor\n", + "ok\n", + "9 says 10 is a neighbor\n", + "ok\n", + "9 says 6 is a neighbor\n", + "ok\n", + "9 says 5 is a neighbor\n", + "ok\n", + "10 [20, 17, 9]\n", + "10 says 20 is a neighbor\n", + "ok\n", + "10 says 17 is a neighbor\n", + "ok\n", + "10 says 9 is a neighbor\n", + "ok\n", + "11 [16, 12, 5, 8]\n", + "11 says 16 is a neighbor\n", + "ok\n", + "11 says 12 is a neighbor\n", + "ok\n", + "11 says 5 is a neighbor\n", + "ok\n", + "11 says 8 is a neighbor\n", + "ok\n", + "12 [16, 14, 13, 11, 8]\n", + "12 says 16 is a neighbor\n", + "ok\n", + "12 says 14 is a neighbor\n", + "ok\n", + "12 says 13 is a neighbor\n", + "ok\n", + "12 says 11 is a neighbor\n", + "ok\n", + "12 says 8 is a neighbor\n", + "ok\n", + "13 [14, 12, 7]\n", + "13 says 14 is a neighbor\n", + "ok\n", + "13 says 12 is a neighbor\n", + "ok\n", + "13 says 7 is a neighbor\n", + "ok\n", + "14 [19, 12, 13, 16, 18, 7]\n", + "14 says 19 is a neighbor\n", + "ok\n", + "14 says 12 is a neighbor\n", + "ok\n", + "14 says 13 is a neighbor\n", + "ok\n", + "14 says 16 is a neighbor\n", + "ok\n", + "14 says 18 is a neighbor\n", + "ok\n", + "14 says 7 is a neighbor\n", + "ok\n", + "15 [25, 16, 5, 9]\n", + "15 says 25 is a neighbor\n", + "ok\n", + "15 says 16 is a neighbor\n", + "ok\n", + "15 says 5 is a neighbor\n", + "ok\n", + "15 says 9 is a neighbor\n", + "ok\n", + "16 [25, 24, 18, 15, 11, 12, 14]\n", + "16 says 25 is a neighbor\n", + "ok\n", + "16 says 24 is a neighbor\n", + "ok\n", + "16 says 18 is a neighbor\n", + "ok\n", + "16 says 15 is a neighbor\n", + "ok\n", + "16 says 11 is a neighbor\n", + "ok\n", + "16 says 12 is a neighbor\n", + "ok\n", + "16 says 14 is a neighbor\n", + "ok\n", + "17 [23, 20, 10]\n", + "17 says 23 is a neighbor\n", + "ok\n", + "17 says 20 is a neighbor\n", + "ok\n", + "17 says 10 is a neighbor\n", + "ok\n", + "18 [24, 19, 16, 14]\n", + "18 says 24 is a neighbor\n", + "ok\n", + "18 says 19 is a neighbor\n", + "ok\n", + "18 says 16 is a neighbor\n", + "ok\n", + "18 says 14 is a neighbor\n", + "ok\n", + "19 [18, 24, 14]\n", + "19 says 18 is a neighbor\n", + "ok\n", + "19 says 24 is a neighbor\n", + "ok\n", + "19 says 14 is a neighbor\n", + "ok\n", + "20 [35, 33, 27, 22, 23, 32, 40, 17, 10]\n", + "20 says 35 is a neighbor\n", + "ok\n", + "20 says 33 is a neighbor\n", + "ok\n", + "20 says 27 is a neighbor\n", + "ok\n", + "20 says 22 is a neighbor\n", + "ok\n", + "20 says 23 is a neighbor\n", + "ok\n", + "20 says 32 is a neighbor\n", + "ok\n", + "20 says 40 is a neighbor\n", + "ok\n", + "20 says 17 is a neighbor\n", + "ok\n", + "20 says 10 is a neighbor\n", + "ok\n", + "21 [34, 24, 30, 9]\n", + "21 says 34 is a neighbor\n", + "ok\n", + "21 says 24 is a neighbor\n", + "ok\n", + "21 says 30 is a neighbor\n", + "ok\n", + "21 says 9 is a neighbor\n", + "not ok\n", + "because 9 says that 21 is not a neighbor\n", + "22 [28, 27, 26, 20, 9]\n", + "22 says 28 is a neighbor\n", + "ok\n", + "22 says 27 is a neighbor\n", + "ok\n", + "22 says 26 is a neighbor\n", + "ok\n", + "22 says 20 is a neighbor\n", + "ok\n", + "22 says 9 is a neighbor\n", + "ok\n", + "23 [32, 17, 20]\n", + "23 says 32 is a neighbor\n", + "ok\n", + "23 says 17 is a neighbor\n", + "ok\n", + "23 says 20 is a neighbor\n", + "ok\n", + "24 [30, 25, 16, 18, 21, 19]\n", + "24 says 30 is a neighbor\n", + "ok\n", + "24 says 25 is a neighbor\n", + "ok\n", + "24 says 16 is a neighbor\n", + "ok\n", + "24 says 18 is a neighbor\n", + "ok\n", + "24 says 21 is a neighbor\n", + "ok\n", + "24 says 19 is a neighbor\n", + "ok\n", + "25 [29, 15, 26, 16, 24]\n", + "25 says 29 is a neighbor\n", + "ok\n", + "25 says 15 is a neighbor\n", + "ok\n", + "25 says 26 is a neighbor\n", + "ok\n", + "25 says 16 is a neighbor\n", + "ok\n", + "25 says 24 is a neighbor\n", + "ok\n", + "26 [28, 22, 9, 25]\n", + "26 says 28 is a neighbor\n", + "ok\n", + "26 says 22 is a neighbor\n", + "ok\n", + "26 says 9 is a neighbor\n", + "ok\n", + "26 says 25 is a neighbor\n", + "ok\n", + "27 [33, 28, 20, 22]\n", + "27 says 33 is a neighbor\n", + "ok\n", + "27 says 28 is a neighbor\n", + "ok\n", + "27 says 20 is a neighbor\n", + "ok\n", + "27 says 22 is a neighbor\n", + "ok\n", + "28 [38, 29, 27, 33, 35, 22, 26]\n", + "28 says 38 is a neighbor\n", + "ok\n", + "28 says 29 is a neighbor\n", + "ok\n", + "28 says 27 is a neighbor\n", + "ok\n", + "28 says 33 is a neighbor\n", + "ok\n", + "28 says 35 is a neighbor\n", + "ok\n", + "28 says 22 is a neighbor\n", + "ok\n", + "28 says 26 is a neighbor\n", + "ok\n", + "29 [37, 30, 28, 25]\n", + "29 says 37 is a neighbor\n", + "ok\n", + "29 says 30 is a neighbor\n", + "ok\n", + "29 says 28 is a neighbor\n", + "ok\n", + "29 says 25 is a neighbor\n", + "ok\n", + "30 [37, 29, 24, 21]\n", + "30 says 37 is a neighbor\n", + "ok\n", + "30 says 29 is a neighbor\n", + "ok\n", + "30 says 24 is a neighbor\n", + "ok\n", + "30 says 21 is a neighbor\n", + "ok\n", + "31 [36, 34]\n", + "31 says 36 is a neighbor\n", + "ok\n", + "31 says 34 is a neighbor\n", + "ok\n", + "32 [41, 40, 23, 20]\n", + "32 says 41 is a neighbor\n", + "ok\n", + "32 says 40 is a neighbor\n", + "ok\n", + "32 says 23 is a neighbor\n", + "ok\n", + "32 says 20 is a neighbor\n", + "ok\n", + "33 [35, 20, 27, 28]\n", + "33 says 35 is a neighbor\n", + "ok\n", + "33 says 20 is a neighbor\n", + "ok\n", + "33 says 27 is a neighbor\n", + "ok\n", + "33 says 28 is a neighbor\n", + "ok\n", + "34 [42, 36, 21, 31]\n", + "34 says 42 is a neighbor\n", + "ok\n", + "34 says 36 is a neighbor\n", + "ok\n", + "34 says 21 is a neighbor\n", + "ok\n", + "34 says 31 is a neighbor\n", + "ok\n", + "35 [44, 38, 20, 33, 28]\n", + "35 says 44 is a neighbor\n", + "ok\n", + "35 says 38 is a neighbor\n", + "ok\n", + "35 says 20 is a neighbor\n", + "ok\n", + "35 says 33 is a neighbor\n", + "ok\n", + "35 says 28 is a neighbor\n", + "ok\n", + "36 [46, 39, 34, 42, 31]\n", + "36 says 46 is a neighbor\n", + "ok\n", + "36 says 39 is a neighbor\n", + "ok\n", + "36 says 34 is a neighbor\n", + "ok\n", + "36 says 42 is a neighbor\n", + "ok\n", + "36 says 31 is a neighbor\n", + "ok\n", + "37 [45, 38, 43, 29, 30]\n", + "37 says 45 is a neighbor\n", + "ok\n", + "37 says 38 is a neighbor\n", + "ok\n", + "37 says 43 is a neighbor\n", + "ok\n", + "37 says 29 is a neighbor\n", + "ok\n", + "37 says 30 is a neighbor\n", + "ok\n", + "38 [43, 35, 28, 37]\n", + "38 says 43 is a neighbor\n", + "ok\n", + "38 says 35 is a neighbor\n", + "ok\n", + "38 says 28 is a neighbor\n", + "ok\n", + "38 says 37 is a neighbor\n", + "ok\n", + "39 [46, 36]\n", + "39 says 46 is a neighbor\n", + "ok\n", + "39 says 36 is a neighbor\n", + "ok\n", + "40 [47, 41, 32, 20]\n", + "40 says 47 is a neighbor\n", + "ok\n", + "40 says 41 is a neighbor\n", + "ok\n", + "40 says 32 is a neighbor\n", + "ok\n", + "40 says 20 is a neighbor\n", + "ok\n", + "41 [47, 32, 40]\n", + "41 says 47 is a neighbor\n", + "ok\n", + "41 says 32 is a neighbor\n", + "ok\n", + "41 says 40 is a neighbor\n", + "ok\n", + "42 [34, 36]\n", + "42 says 34 is a neighbor\n", + "ok\n", + "42 says 36 is a neighbor\n", + "ok\n", + "43 [48, 45, 44, 38, 37]\n", + "43 says 48 is a neighbor\n", + "ok\n", + "43 says 45 is a neighbor\n", + "ok\n", + "43 says 44 is a neighbor\n", + "ok\n", + "43 says 38 is a neighbor\n", + "ok\n", + "43 says 37 is a neighbor\n", + "ok\n", + "44 [49, 48, 35, 43]\n", + "44 says 49 is a neighbor\n", + "ok\n", + "44 says 48 is a neighbor\n", + "ok\n", + "44 says 35 is a neighbor\n", + "ok\n", + "44 says 43 is a neighbor\n", + "ok\n", + "45 [48, 49, 37, 43]\n", + "45 says 48 is a neighbor\n", + "ok\n", + "45 says 49 is a neighbor\n", + "ok\n", + "45 says 37 is a neighbor\n", + "ok\n", + "45 says 43 is a neighbor\n", + "ok\n", + "46 [36, 39]\n", + "46 says 36 is a neighbor\n", + "ok\n", + "46 says 39 is a neighbor\n", + "ok\n", + "47 [40, 41]\n", + "47 says 40 is a neighbor\n", + "ok\n", + "47 says 41 is a neighbor\n", + "ok\n", + "48 [49, 44, 43, 45]\n", + "48 says 49 is a neighbor\n", + "ok\n", + "48 says 44 is a neighbor\n", + "ok\n", + "48 says 43 is a neighbor\n", + "ok\n", + "48 says 45 is a neighbor\n", + "ok\n", + "49 [44, 48, 45]\n", + "49 says 44 is a neighbor\n", + "ok\n", + "49 says 48 is a neighbor\n", + "ok\n", + "49 says 45 is a neighbor\n", + "ok\n" + ] + } + ], + "source": [ + "# Take a gal dictionary from 1 and test if there are any asymmetries.\n", + "# An asymmetry occurs when i says j is a neighbor of i, but j does not say i is a neighbor of j.\n", + "\n", + "for key in gal_dict:\n", + " print(key, gal_dict[key])\n", + " for neighbor in gal_dict[key]:\n", + " print(key,' says ', neighbor, 'is a neighbor')\n", + " if key in gal_dict[neighbor]:\n", + " print('ok')\n", + " else:\n", + " print('not ok')\n", + " print('because ', neighbor, ' says that ', key, 'is not a neighbor')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}