<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Peaks and Valleys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import Data.List (sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "peaksAndValleys :: Ord a => [a] -> [a]\n",
    "peaksAndValleys xs =\n",
    "    let \n",
    "        swapPairs :: [b] -> [b]\n",
    "        swapPairs (y1:y2:ys) = y2 : y1 : swapPairs ys\n",
    "        swapPairs ys = ys\n",
    "    in\n",
    "        swapPairs $ sort xs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import Test.QuickCheck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "isAlternatingSequence :: Ord a => [a] -> Bool\n",
    "isAlternatingSequence xs =\n",
    "    let\n",
    "        startsWithPeak :: Ord b => [b] -> Bool\n",
    "        startsWithPeak (y1:y2:ys) = y1 >= y2 && startsWithValley (y2:ys)\n",
    "        startsWithPeak _ = True\n",
    "        \n",
    "        startsWithValley :: Ord b => [b] -> Bool\n",
    "        startsWithValley (y1:y2:ys) = y1 <= y2 && startsWithPeak (y2:ys)\n",
    "        startsWithValley _ = True\n",
    "    in\n",
    "        startsWithPeak xs || startsWithValley xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "isAlternatingSequence [5, 3, 1, 2, 3]\n",
    "isAlternatingSequence [5, 1, 3, 2, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "testPeaksAndValleys :: [Int] -> Bool\n",
    "testPeaksAndValleys xs =\n",
    "    let\n",
    "        xs' = peaksAndValleys xs\n",
    "    in\n",
    "        isAlternatingSequence xs' && sort xs == sort xs'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "+++ OK, passed 100 tests."
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "quickCheck testPeaksAndValleys"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Haskell",
   "language": "haskell",
   "name": "haskell"
  },
  "language_info": {
   "codemirror_mode": "ihaskell",
   "file_extension": ".hs",
   "name": "haskell",
   "version": "7.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}<commit_msg>Task v6 10.11 (Haskell) - refactor solution & add alternative implementation<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Peaks and Valleys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import Data.List (sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "peaksAndValleys :: Ord a => [a] -> [a]\n",
    "peaksAndValleys =\n",
    "    let \n",
    "        swapPairs :: [b] -> [b]\n",
    "        swapPairs (y1:y2:ys) = y2 : y1 : swapPairs ys\n",
    "        swapPairs ys = ys\n",
    "    in\n",
    "        swapPairs . sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "peaksAndValleys' :: Ord a => [a] -> [a]\n",
    "peaksAndValleys' =\n",
    "    let\n",
    "        sortFirst :: Ord b => [b] -> [b]\n",
    "        sortFirst [y1, y2, y3]\n",
    "            | y1 < y2 = [y1, y2, y3]\n",
    "            | otherwise = [y2, y1, y3]\n",
    "\n",
    "        sortLast :: Ord b => [b] -> [b]\n",
    "        sortLast [y1, y2, y3]\n",
    "            | y2 < y3 = [y1, y2, y3]\n",
    "            | otherwise = [y1, y3, y2]\n",
    "\n",
    "        sortTriple = sortFirst . sortLast . sortFirst\n",
    "\n",
    "        reorderElems :: Ord b => [b] -> [b]\n",
    "        reorderElems (y1:y2:y3:ys) =\n",
    "            let\n",
    "                [smallest, middle, largest] = sortTriple [y1, y2, y3]\n",
    "            in\n",
    "                largest : smallest : reorderElems (middle:ys)\n",
    "                \n",
    "        reorderElems [y1, y2] = [max y1 y2, min y1 y2]\n",
    "\n",
    "        reorderElems [y] = [y]\n",
    "        reorderElems [] = []\n",
    "        \n",
    "    in\n",
    "        reorderElems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import Test.QuickCheck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "isAlternatingSequence :: Ord a => [a] -> Bool\n",
    "isAlternatingSequence xs =\n",
    "    let\n",
    "        startsWithPeak :: Ord b => [b] -> Bool\n",
    "        startsWithPeak (y1:y2:ys) = y1 >= y2 && startsWithValley (y2:ys)\n",
    "        startsWithPeak _ = True\n",
    "        \n",
    "        startsWithValley :: Ord b => [b] -> Bool\n",
    "        startsWithValley (y1:y2:ys) = y1 <= y2 && startsWithPeak (y2:ys)\n",
    "        startsWithValley _ = True\n",
    "    in\n",
    "        startsWithPeak xs || startsWithValley xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "isAlternatingSequence [5, 3, 1, 2, 3]\n",
    "isAlternatingSequence [5, 1, 3, 2, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "testPeaksAndValleys :: ([Int] -> [Int]) -> [Int] -> Bool\n",
    "testPeaksAndValleys f xs =\n",
    "    let\n",
    "        xs' = f xs\n",
    "    in\n",
    "        isAlternatingSequence xs' && sort xs == sort xs'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "+++ OK, passed 100 tests."
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "quickCheck $ testPeaksAndValleys peaksAndValleys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "+++ OK, passed 100 tests."
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "quickCheck $ testPeaksAndValleys peaksAndValleys'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Haskell",
   "language": "haskell",
   "name": "haskell"
  },
  "language_info": {
   "codemirror_mode": "ihaskell",
   "file_extension": ".hs",
   "name": "haskell",
   "version": "7.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
{'fraction_non_alphanumeric': 0.20757519494987003, 'fraction_numerical': 0.01534843421215497, 'mean_word_length': 1.8510938602681721, 'pattern_counts': {'":': 162, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4723477', 'n_tokens_mistral': 3293, 'n_tokens_neox': 3153, 'n_words': 575}