I want to have recursion come to my mind

When I read the haskell book, I remembered opening my eyes to scala and wanted to write a recursive function, but I wasn't confident, so I put up with qsort. I want to write a cooler recursion by sticking the recursion properly to the registry in the brain. Whenever I wrote a loop, I thought it would be good to imagine what would happen if I wrote it recursively.

qsort.py


import random
a = [i for i in range(100)]
random.shuffle(a)
print "before"
print a 

def qsort(l):
  if len(l) > 1:
    pivot = random.randrange(len(l))
    smaller = [i for i in l if i < l[pivot] ]
    bigger = [i for i in l if i >= l[pivot] ]
    l = qsort(smaller) + qsort(bigger)
  return l

a = qsort(a)
print "after"
print a
```##Output result

before
[5, 40, 28, 72, 7, 68, 64, 91, 37, 67, 23, 90, 85, 43, 39, 93, 58, 62, 75, 41, 53, 14, 3, 52, 47, 94, 44, 15, 80, 99, 89, 56, 33, 84, 86, 70, 10, 61, 59, 76, 35, 1, 60, 96, 55, 19, 26, 24, 32, 97, 30, 57, 66, 25, 69, 98, 12, 42, 78, 27, 87, 51, 21, 82, 92, 71, 9, 95, 11, 6, 83, 2, 81, 73, 17, 63, 16, 46, 34, 22, 77, 4, 48, 79, 49, 88, 36, 65, 29, 50, 31, 8, 18, 54, 45, 38, 20, 0, 74, 13]
after
[0, 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, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Recommended Posts

I want to have recursion come to my mind
I want to make fits from my head
I want to solve Sudoku (Sudoku)
I want to announce my graduation thesis on IPython Notebook
I want to understand systemd roughly
I want to scrape images to learn
I want to do ○○ with Pandas
I want to copy yolo annotations
I want to debug with Python
I want to check the position of my face with OpenCV!
I want to pin Spyder to the taskbar
I want to get angry with my mom when my memory is tight
I want to detect objects with OpenCV
I want to output to the console coolly
I want to print in a comprehension
I want to scrape them all together.
I want to handle the rhyme part1
I want to know how LINUX works!
I want to blog with Jupyter Notebook
I want to handle the rhyme part3
I want to use jar from python
I want to build a Python environment
I want to use Linux on mac
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to use the Django Debug Toolbar in my Ajax application
I want to express my feelings with the lyrics of Mr. Children
I want to use IPython Qt Console
I want to display the progress bar
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
What I always add to my ~ / .bashrc
I want to format and check Python code to my liking on VS Code
I want to handle the rhyme part2
I want to develop Android apps on Android
I want CAPTCHA to say HIWAI words
I want to handle the rhyme part5
I tried my best to return to Lasso
I want to handle the rhyme part4
[Ansible] I want to call my own function from the template module (macro)
I want to create a nice Python development environment for my new Mac
[Linux] You do not have root privileges. But I want to yum install.
I want to make matplotlib a dark theme
I want to connect to PostgreSQL from various languages
I want to do Dunnett's test in Python
I want to easily create a Noise Model
I want to use MATLAB feval with python
I want to pin Datetime.now in Django tests
I want to analyze songs with Spotify API 2
I want to INSERT a DataFrame into MSSQL
I want to memoize including Python keyword arguments
I want to create a window in Python
Anyway, I want to check JSON data easily
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python