DS coding interview

发布时间 2024-01-08 01:10:19作者: ForHHeart

? Table of Content




? Get Started!

I. Python 39


01 - Python kick off

02 - List I

(1) List Comprehension

Description
Build a function LstComp which takes two integers (i and j) as inputs and returns a list of numbers from i to j.

Examples
LstComp(1, 5) returns [1, 2, 3, 4, 5]

def LstComp(i, j):
    # Using list comprehension to create a list of numbers from i to j
    return [num for num in range(i, j + 1)]

# Testing the function with the provided example
LstComp(1, 5)

(2) OddNums

Description
Build a function OddNums which takes two integers (i and j) as inputs and returns a list of ODD numbers between i and j inclusively.

Examples
OddNums(1, 5) returns [1, 3, 5]
OddNums(2, 6) returns [3, 5]
OddNums(1, 2) returns [1]

def OddNums(i, j):
    # Using list comprehension to create a list of odd numbers between i and j inclusively
    return [num for num in range(i, j + 1) if num % 2 != 0]

# Testing the function with the provided examples
test_results = {
    "Example 1": OddNums(1, 5),
    "Example 2": OddNums(2, 6),
    "Example 3": OddNums(1, 2)
}

test_results

03 - List II

04 - String

05 - Dictionary