prepbook
  • Introduction
  • Some common stuff
    • python __repr__
    • HackerRank input tips
  • Data Structures and Algorithms
    • Breadth first search
    • Depth First Search
    • Dijkstra
    • A* Search Algorithm
    • Binary Search
    • python counter
    • Sorting
      • Merge Sort
      • Quick Sort
    • Priority Queue
  • Multiprocessing vs Threading
  • Common Coding
    • Find loop in lin list
    • Maximum sum subarray
  • Coding
    • Valid palindrome
    • Palindrome number
    • Remove duplicates from sorted array
    • Island perimeter
    • Serialize and Deserialize Binary Tree
    • Valid Soduku
    • Word Pattern
    • Word Pattern II
    • Group Anagrams
    • Implement Trie
    • Deep copy list with random node
    • Palindrome Permutation
    • Combination Sum
    • Clone Graph
    • Generate parenthesis
    • Fibonacci Number
    • LRU Cache
    • Merge two sorted arrays in place
    • Hamming Distance
    • Merge K sorted arrays
    • Kth smalles element in BST
    • Kth largest element in an array
    • Remove duplicates from sorted list
    • Power of 2
    • Nested list weight sum
    • SIngle number in a list
    • Factor combinations
    • Delete node from BST
  • hacker Rank
    • Coding
      • print staircase
      • Drawing book
      • Challenge 0
      • Min-Max sum
  • WorkRelatedCoding
    • Rectangle Overlap
  • Python tips
Powered by GitBook
On this page

Was this helpful?

  1. Coding

Factor combinations

Numbers can be regarded as product of its factors. For example,

8 = 2 x 2 x 2;
  = 2 x 4.

Write a function that takes an integernand return all possible combinations of its factors.

Note:

  1. You may assume that n is always positive.

  2. Factors should be greater than 1 and less than n.

input:

32

output:

[
  [2, 16],
  [2, 2, 8],
  [2, 2, 2, 4],
  [2, 2, 2, 2, 2],
  [2, 4, 4],
  [4, 8]
]
import math
class Solution(object):
    def getFactors(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        retList = []
        if not n:
            return []

        self.solve(n, retList, [], 2)
        return retList
        #return self.solve([], n, [], 2)
    '''
    def solve(self, ret, n, sofar, start):
        if sofar:
            ret.append(sofar + [n])
            #print 'ret: ', ret

        for i in range(start, int(math.sqrt(n))+1):
            if n % i == 0:
                #print 'sofar: ', sofar + [i]
                self.solve(ret, n/i, sofar+[i], i)

        return ret
    '''

    def solve(self, n, retList, curList, start):
        if curList:
            # both of these works
            #retList += [curList[:] +[n]]
            retList += [curList + [n]]
            #print 'retList: ', retList

        for i in xrange(start, int(math.sqrt(n)) + 1):
            if n % i == 0:
                #curList += [i]
                self.solve(n/i, retList, curList + [i], i)
                #curList.pop()
PreviousSIngle number in a listNextDelete node from BST

Last updated 5 years ago

Was this helpful?