šŸ•¶ļø
VICEINTELPRO
GitHub: HorrorClause
  • In Tenebris Videmus
  • 🚩CTFs
    • šŸ’¾Hack the Box
      • šŸ«Academy
        • Command Injection Assessment
        • XSS Assessment
        • Web Attacks Assessment
    • Try Hack Me
      • In Progress
  • šŸ“–Documents
  • šŸ‘Øā€šŸ«HOW-TOs
    • Obisidian How-To
    • Setup Mandiant FLARE VM
  • šŸ“‘Security Fundamentals
    • Security Controls
      • Physical Security
      • Endpoint Security
      • Email Security
      • Network Security
      • AAA Controls
    • Networking 101
      • OSI Model
      • Network Fundamentals
      • Network Devices
      • Network Tools
      • Protocols and Ports
    • šŸ‘Øā€šŸ’¼Management Principles
      • Risk
      • Policies and Procedures
      • Compliance and Frameworks
      • Change and Patch Management
  • šŸ›”ļøSecurity Concepts
    • āš ļøRisk Assessment Models
      • DREAD Risk Assessment Model
      • STRIDE Threat Model
      • Common Vulnerability Scoring System (CVSS)
    • Pentesting
      • Common Terms
      • AV Identification-Evasion
      • Introduction to Payloads
      • Automating Payloads & Delivery with Metasploit
      • Shells Jack Us In, Payloads Deliver Us Shells
      • Web Shells
      • Pentesting Overview
      • Penetration Testing Process
    • šŸ›Vulnerability Assessment
      • Common Vulnerabilities and Exposures (CVE)
      • Common Vulnerability Scoring System (CVSS)
      • Assessment Standards
      • Vulnerability Assessment
      • Vulnerability Scanning
      • Reporting
      • šŸŽÆNessus
        • Getting Started with Nessus
        • Nessus Scan
        • Working with Nessus Scan Output
        • Advanced Settings
        • Scanning Issues
      • 🦓OpenVAS (Greenbone)
        • Getting Started with OpenVAS
        • OpenVAS
        • Exporting Results
    • Passwords
      • Password Managers
      • Password Policies
      • Password Security Fundamentals
    • Frameworks
    • GRC
    • Logon Types
    • What is Dev-Null ?
  • āš”ļøOffensive Security
    • OSINT
      • OSINT - Websites
      • Google Dorks
    • šŸ”«Attacking Common Services
      • The Concept of Attacks
      • Interacting with Common Services
      • Finding Sensitive Information
      • Attacking DNS
      • Attacking Email Services
      • Attacking FTP
      • Attacking RDP
      • Attacking SMB
      • Attacking SQL Databases
      • Cheat Sheet - Attacking Common Services
      • Service Misconfigurations
    • šŸ”ŖAttacking Web Apps with Ffuf
      • Web Fuzzing
      • Directory Fuzzing
      • Page Fuzzing
      • Recursive Fuzzing
      • DNS Records
      • Sub-domain Fuzzing
      • Vhost Fuzzing
      • Filtering Results
      • Parameter Fuzzing - GET
      • Parameter Fuzzing - POST
      • Value Fuzzing
    • ā˜ļøCloud
      • AWS
        • AWS S3 Buckets
    • šŸ’‰Command Injection
      • Command Injection Cheat Sheet
      • Intro to Command Injections
      • Detection
      • Injecting Commands
      • Other Injection Operators
      • Identifying Filters
      • Bypassing Space Filters
      • Bypassing Other Blacklisted Characters
      • Bypassing Blacklisted Commands
      • Advanced Command Obfuscation
      • Evasion Tools
      • Command Injection Prevention
    • Containers
      • Docker
    • āŒCross-Site Scripting (XSS)
      • Introduction to XSS
      • Stored XSS
      • Reflected XSS
      • DOM XSS
      • XSS Discovery
      • Defacing
      • Phishing
      • Session Hijacking
      • XSS Prevention
    • Directory Busting
      • DirB
      • DirBuster
      • Ffuf
      • Gobuster
    • šŸ…°ļøDNS
      • DNSRecon
      • Fierce
    • File Inclusion
      • Local File Inclusion Cheatsheet
      • Intro to File Inclusion
      • Local File Inclusion (LFI)
      • Basic Bypass
      • PHP Filters
      • PHP Wrappers
      • Remote File Inclusion (RFI)
      • LFI and File Uploads
      • Log Poisoning
      • Automated Scanning
      • File Inclusion Prevention
    • File Transfers
      • Transferring Files
      • File Transfer - Quick Commands
      • Living off the Land
      • Windows File Transfer Methods
      • Linux File Transfer Methods
      • Catching Files over HTTP(S)
      • Transferring Files with Code
      • Miscellaneous File Transfer Methods
      • Protected File Transfers
      • Mounting Encrypted VHD Drives
      • Mounting VHD in Kali
      • File Transfer Detection
    • File Upload Attacks
      • File Upload Cheatsheet
      • Absent Validation
      • Upload Exploitation
      • Client-Side Validation
      • Blacklist Filters
      • Whitelist Filters
      • Type Filters
      • Limited File Uploads
      • Other Upload Attacks
      • Preventing File Upload Vulnerabilities
    • šŸ‘£Footprinting
      • Linux Remote Management Protocols
      • Windows Remote Management Protocols
      • Enumeration
        • Enumeration Methodology
        • šŸ–„ļøHost Based
          • Quick Commands
          • DNS
          • FTP
          • IMAP-POP3
          • IPMI
          • MSSQL
          • MySQL
          • NFS
          • Oracle TNS
          • SMB
  • Powershell
    • Powershell CheatSheet
  • Python
    • Map
    • Anonymous Functions
    • Recursion
      • ZipMap
      • Nested Sum
      • Recursion on a Tree
      • Count Nested Levels
      • Longest Word
    • Function Transformations
      • More Transformations
      • Why Transform?
    • Closures
    • Currying
    • Decorators
    • Sum Types
    • Enums
    • Match
    • Regex
  • Kusto (KQL)
    • SQL and KQL Comparison
    • Using the Where and Sort Operators
    • KQL Queries
  • HTML
  • Insecure File Uploads
Powered by GitBook
On this page
  • Example of Recursion
  • Assignment
  • Solution
  1. Python

Recursion

PreviousAnonymous FunctionsNextZipMap

Last updated 2 months ago

Recursion is the process of defining something in terms of itself.

Example of Recursion

If you thought loops were the only way to iterate over a list, you were wrong! Recursion is fundamental to functional programming because it's how we iterate over lists while avoiding stateful loops. Take a look at this function that sums the numbers in a list:

def sum_nums(nums):
    if len(nums) == 0:
        return 0
    return nums[0] + sum_nums(nums[1:])

print(sum_nums([1, 2, 3, 4, 5]))
# 15

1. Solve a Small Problem

Our goal is to sum all the numbers in a list, but we're not allowed to loop. So, we start by solving the smallest possible problem: summing the first number in the list with the rest of the list:

return nums[0] + sum_nums(nums[1:])

2. Recurse

So, what actually happens when we call sum_nums(nums[1:])? Well, we're just calling sum_nums with a smaller list! In the first call, the nums input was [1, 2, 3, 4, 5], but in the next call it's just [2, 3, 4, 5]. We just keep calling sum_nums with smaller and smaller lists.

3. The Base Case

So what happens when we get to the "end"? sum_nums(nums[1:]) is called, but nums[1:] is an empty list because we ran out of numbers. We need to write a base case to stop the madness.

if len(nums) == 0:
    return 0

The "base case" of a recursive function is the part of the function that does not call itself.

Assignment

A factorial is the product of all positive integers less than or equal to a number. For example, 5! (read: "five factorial") is 5 * 4 * 3 * 2 * 1, which is 120.

Complete the factorial_r function. It should recursively calculate the factorial of a number.

Tips
  1. What's a small problem you can solve first?

  2. How can you go from the "first" value of x to the "next" value of x, all the way down to the "last" value of x?

  3. What's the base case that should stop the recursion?

def factorial_r(x):
    pass

Solution

def factorial_r(x):
    if x == 0:    # Base Case
        return 1
    return (x * factorial_r(x-1))    # Recursive Case

How Factorial Works

The factorial of a number n (denoted as n!) is:

n!=nƗ(nāˆ’1)Ɨ(nāˆ’2)Ɨ...Ɨ1n!=nƗ(nāˆ’1)Ɨ(nāˆ’2)Ɨ...Ɨ1n!=nƗ(nāˆ’1)Ɨ(nāˆ’2)Ɨ...Ɨ1

For example:

  • 5! = 5 Ɨ 4 Ɨ 3 Ɨ 2 Ɨ 1 = 120

  • 4! = 4 Ɨ 3 Ɨ 2 Ɨ 1 = 24

  • 3! = 3 Ɨ 2 Ɨ 1 = 6

  • 1! = 1

  • 0! is defined as 1 (base case)

Step-by-Step Execution

Let's go through an example: factorial_r(3)

  1. factorial_r(3)

    • x is not 0, so it returns 3 * factorial_r(2)

  2. factorial_r(2)

    • x is not 0, so it returns 2 * factorial_r(1)

  3. factorial_r(1)

    • x is not 0, so it returns 1 * factorial_r(0)

  4. factorial_r(0)

    • x == 0, so it returns 1 (base case)

Now, let's resolve the function calls:

factorial_r(1) = 1 * 1 = 1
factorial_r(2) = 2 * 1 = 2
factorial_r(3) = 3 * 2 = 6

Key Concepts

  • Base Case (x == 0) Prevents infinite recursion and stops the function when x reaches 0.

  • Recursive Case (x * factorial_r(x - 1)) Keeps calling itself with a smaller number until it reaches the base case.

Doc2Doc can automatically generate various layouts for a page. There are a lot of possible layouts, so we need a function to calculate the total number of possible layouts.

Since 0! is an , what should an input of 0 return?

factorial
empty product