/algo

Algorithms and data structure possible solutions.

Primary LanguagePython

Algo repository

Contains all attempted algorithm and data structure questions.
Some files may contain more than one solution whose efficiency may vary.

  • Sumup.py

Attemps to solve the question:

Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.

  • Movezeros.py

Attemps to solve the question:

Given a list, write a function to move all 0's to the end of the list while maintaining the relative order
of the non-zero elements.
For example:
Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]

  • Listproduct.py

Attemps to solve the question:

Given an array of integers, return a new array such that each element at index i of the new array is the
product of all the numbers in the original array except the one at i.
For example:
Input: [1, 2, 3, 4, 5]
Output: [120, 60, 40, 30, 24]
Input: [3, 2, 1]
Output: [2, 3, 6]

  • Lowestpositive.py

Attemps to solve the question:

Given an array of integers, find the first missing positive integer in linear time and constant space.
In other words, find the lowest positive integer that does not exist in the array.
For example:
Input: [3, 4, -1, 1]
Output: 2
Input: [1, 2, 0]
Output: 3

  • Pairconstruct.py

Attemps to solve the question:

cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair.
Given this implementation of cons:

    def cons(a, b):     
        def pair(f):     
           return f(a, b)    
        return pair     

For example:

Input: car(cons(3, 4))
Output: 3
Input: cdr(cons(3, 4))
Output: 4