Interview Tips: Python

  • Given a set or list of items find a sub set that matches some criteria: Combinations
  • Given a set or list of items find different arrangements of the items: Permutations
https://calcworkshop.com/functions-statistics/permutations-combinations/

Sample problems

Permutations

Combinations

Solutions

Permutations:

from itertools import permutationss = ['A','B','C']
print(f"Permutations of {s} is")
for p in permutations(s):
print(p, end=" ")
#ouput
Permuation of ['A', 'B', 'C'] is
('A', 'B', 'C') ('A', 'C', 'B') ('B', 'A', 'C') ('B', 'C', 'A') ('C', 'A', 'B') ('C', 'B', 'A')

Combinations

from itertools import combinations# unlike permutation API, combination API needs one extra param for number of items to consider while combining elementss = "ABC"
print(f"Combination of {s} is")
for i in range(len(s)+1): # +1 if we wanted to add entire sequence
for c in combinations(s,i):
print(c, end=" ")
print(" ")
# output
# Combination of ABC is
# () ('A',) ('B',) ('C',) ('A', 'B') ('A', 'C') ('B', 'C') ('A', 'B', 'C')

def find_numbers(l, total):
for i in range(len(l)+1): # +1 if we wanted to add entire sequence
for c in combinations(l, i):
if sum(c) == total:
print(c)

find_numbers([12, 1, 61, 5, 9, 2], 24)
#output
#(12, 1, 9, 2)
  1. Using loops
  2. Recursion
  3. Backtracking : Recursion + Loop

--

--

--

A simple guy in pursuit of of AI and Deep Learning with Big Data tools :) @ https://www.linkedin.com/in/mageswaran1989/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Story of the Migration of Netflix’s IT Resources to Cloud

Box Model and a few CSS properties

This week in #Scala (Jun 1, 2020)

Exploring Wikidata using GraphQL and DGraph

Understanding HTML Canvas Scaling and Sizing

Task 02- Run GUI Application inside Docker Container In this blog , I am going to launch two GUI…

In a year full of unexpected disruptions, you could be excused if you missed Microsoft’s

A Simple Example of “Lambda-backed Custom Resource” in AWS CloudFormation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mageswaran D

Mageswaran D

A simple guy in pursuit of of AI and Deep Learning with Big Data tools :) @ https://www.linkedin.com/in/mageswaran1989/

More from Medium

Python’s In-Built Data Structures

DATA STRUCTURES IN PYTHON

Python Interview Questions for 2022

Linear Search Using Python 🐍