Solutions.ipynb 8.79 KB
Newer Older
christian.foerster's avatar
christian.foerster committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solutions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Read in some of your data with pandas, plot it and get the main statistics on the data.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "%matplotlib inline\n",
    "import cufflinks as cf\n",
    "cf.go_offline()\n",
    "\n",
    "fp=r\"M:\\Projekte\\2013_UWRM\\3_messen und daten\\Messstellen\\Pegel Kreischa\\Q_Kreischa.txt\"\n",
    "my_df=pd.read_csv(fp,parse_dates=True,index_col=0,sep=\",\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iplot(secondary_y=\"q\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(my_df.describe())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a class, that takes a filepath as an argument when it is initialized. Create a method, that takes a string as an argument and returns the number of occurances of that string in that file."
   ]
  },
  {
   "cell_type": "code",
christian.foerster's avatar
christian.foerster committed
60
   "execution_count": 5,
christian.foerster's avatar
christian.foerster committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "class StrCount():\n",
    "    def __init__(self,filepath):\n",
    "        self.filepath=filepath\n",
    "        return\n",
    "    \n",
    "    def count(self,string):\n",
    "        count=0\n",
    "        with open(self.filepath) as file:\n",
    "            for line in file:\n",
    "                count+=len(line.split(string))-1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
christian.foerster's avatar
christian.foerster committed
81
   "execution_count": 6,
christian.foerster's avatar
christian.foerster committed
82
   "metadata": {},
christian.foerster's avatar
christian.foerster committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'C:\\\\Users\\\\Gabi\\\\Documents\\\\Python\\\\NN\\\\Inputdata\\\\PCM_MEZ_good_shape.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-cdb379e1f3ca>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mfilepath\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34mr\"C:\\Users\\Gabi\\Documents\\Python\\NN\\Inputdata\\PCM_MEZ_good_shape.txt\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mStrCount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilepath\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\";\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;31m# if this takes too long it might be the server connection... (depending on the file size)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-5-e09a27b1721e>\u001b[0m in \u001b[0;36mcount\u001b[0;34m(self, string)\u001b[0m\n\u001b[1;32m      8\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mcount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mstring\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m         \u001b[0mcount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m         \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilepath\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     11\u001b[0m             \u001b[0;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     12\u001b[0m                 \u001b[0mcount\u001b[0m\u001b[0;34m+=\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstring\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'C:\\\\Users\\\\Gabi\\\\Documents\\\\Python\\\\NN\\\\Inputdata\\\\PCM_MEZ_good_shape.txt'"
     ]
    }
   ],
christian.foerster's avatar
christian.foerster committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
   "source": [
    "filepath=r\"C:\\Users\\Gabi\\Documents\\Python\\NN\\Inputdata\\PCM_MEZ_good_shape.txt\"\n",
    "count=StrCount(filepath).count(\";\")\n",
    "print(count)\n",
    "\n",
    "# if this takes too long it might be the server connection... (depending on the file size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a function that takes an arbitrary number of inputs and prints them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#this function only takes list like entries\n",
    "def arbitrary_list(*arguments):\n",
    "    print(arguments)\n",
    "    \n",
    "arbitrary_list(2,3,4,5,4,3,2,4,[432],[{2:3}])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#this function only takes list like entries\n",
    "def arbitrary_dict(**dictarguments):\n",
    "    print(dictarguments)\n",
    "    \n",
    "arbitrary_dict(a=3,b=4,wer=88)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#this function can do all list arguments must come before dict arguments\n",
    "def arbitrary(*list_args,**dict_args):\n",
    "    print(list_args)\n",
    "    print(dict_args)\n",
    "    \n",
    "arbitrary(2,3,4,5,6,a=3,b=33,d=\"dd\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Write a function, that creates the fibonacci sequence up to the n'th term."
   ]
  },
  {
   "cell_type": "code",
christian.foerster's avatar
christian.foerster committed
161
   "execution_count": 22,
christian.foerster's avatar
christian.foerster committed
162
   "metadata": {},
christian.foerster's avatar
christian.foerster committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 3,\n",
       " 5,\n",
       " 8,\n",
       " 13,\n",
       " 21,\n",
       " 34,\n",
       " 55,\n",
       " 89,\n",
       " 144,\n",
       " 233,\n",
       " 377,\n",
       " 610,\n",
       " 987,\n",
       " 1597,\n",
       " 2584,\n",
       " 4181,\n",
       " 6765,\n",
       " 10946,\n",
       " 17711,\n",
       " 28657,\n",
       " 46368]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
christian.foerster's avatar
christian.foerster committed
199
   "source": [
christian.foerster's avatar
christian.foerster committed
200 201 202 203 204
    "# non-recursive\n",
    "def Fibonacci2(n):\n",
    "    if n<=0:\n",
    "        raise ValueError(\"The input must be larger 0.\")\n",
    "    fibo=[0,1]\n",
christian.foerster's avatar
christian.foerster committed
205 206 207 208 209 210 211 212
    "    if n==1:\n",
    "        return fibo[0]\n",
    "    elif n==2:\n",
    "        return fibo\n",
    "    else:\n",
    "        for run in range(n-2):\n",
    "            fibo.append(fibo[-1]+fibo[-2])\n",
    "        return fibo\n",
christian.foerster's avatar
christian.foerster committed
213
    "\n"
christian.foerster's avatar
christian.foerster committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make python list of all files in the current directory and sort them by their file type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# list all contest\n",
    "foldercontent=os.listdir()\n",
    "\n",
    "#filter files\n",
    "files=[file for file in foldercontent if os.path.isfile(file)]\n",
    "\n",
    "#sort key makes sure i sort by the file ending\n",
    "def sortkey(x):\n",
    "    return x.split(\".\")[1]\n",
    "# sorting\n",
    "files.sort(key=sortkey)\n",
    "\n",
    "print(files)"
   ]
  }
 ],
 "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",
christian.foerster's avatar
christian.foerster committed
263
   "version": "3.6.8"
christian.foerster's avatar
christian.foerster committed
264 265 266 267 268
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}