{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hallo', 'world', 'This', 'is', 'my', 'first', 'program', 'three', 'spaces', 'here', 'Yes']\n",
      "['Hallo', 'world', 'This', 'is', 'my', 'first', 'program', 'three', 'spaces', 'here', 'Yes']\n",
      "[]\n",
      "[]\n",
      "['ddd']\n"
     ]
    }
   ],
   "source": [
    "def splitString(s):\n",
    "    Ortho=[\" \",\";\",\".\",\"!\",\"?\",\":\",\",\",\"-\"]\n",
    "    L=[]\n",
    "    if len(s)==0: # you never know....\n",
    "        return L\n",
    "    current=0\n",
    "    start=0\n",
    "    # first of all I skip all the starting ortho symbols\n",
    "    while current<len(s) and s[current] in Ortho: \n",
    "        current=current+1\n",
    "    start=current\n",
    "    # now we are sure that start and current are at the beginning of the first real word (or that the string is over)\n",
    "    while current<len(s):\n",
    "        if s[current] in Ortho:\n",
    "            # I found an orthograhic symbol. \n",
    "            # first I append the word from start up tu current-1\n",
    "            L.append(s[start:current])\n",
    "            # But there might be several symbols, so I go on until I find a non-orthographic symbol or until the string is over\n",
    "            while current<len(s) and s[current] in Ortho:\n",
    "                current=current+1\n",
    "            # now current is on the first non-ortho symbol\n",
    "            start=current # and thus start is set equal to current, as we will have to re-start the procedure.\n",
    "        else:\n",
    "            current=current+1\n",
    "    # I am now outside the loop. I might be outside because the last character is an orthographic symbol and in this case fine!\n",
    "    # but I might be outside because the string ended with a letter and thus I am still \"inside\" a word! And I have to add it\n",
    "    if s[-1] not in Ortho:\n",
    "        L.append(s[start:])\n",
    "    return L\n",
    "\n",
    "print splitString(\"Hallo world! This is my first program   three spaces here?Yes!\")\n",
    "print splitString(\"        Hallo world! This is my first program   three spaces here?Yes\")\n",
    "print splitString(\"\")\n",
    "print splitString(\" \")\n",
    "print splitString(\"ddd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s=\"This is my day! Why? Not: this isn't\"\n",
    "def splitString(s):\n",
    "    ort=[\" \",\",\",\".\",\"?\",\"!\",\";\",\":\"]\n",
    "    L=[]\n",
    "    if len(s)==0:\n",
    "        return L\n",
    "    if s[0] in ort:\n",
    "        inAWord=False\n",
    "    else:\n",
    "        inAWord=True\n",
    "        startWord=0\n",
    "    i=1\n",
    "    while i<len(s):\n",
    "        if s[i] in ort:\n",
    "            if inAWord:\n",
    "                L.append(s[startWord:i])\n",
    "                inAWord=False\n",
    "        else:\n",
    "            if not inAWord:\n",
    "                inAWord=True\n",
    "                startWord=i\n",
    "        i+=1\n",
    "    if inAWord:\n",
    "        L.append(s[startWord:])\n",
    "    return L\n",
    "        \n",
    "print(splitString(\"This is my day! Why? Not: this isn't\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
