Coder Social home page Coder Social logo

dsc-combinations-lab-online-ds-pt-081219's Introduction

Combinations - Lab

Introduction

Now, let's dive into combinations. In the previous lab, you saw how the order was important when using permutations. Cracking a code is one example, but what if the order doesn't matter, for example, when an engaged couple wants to pick 3 wedding cakes from a list of 15? You'll need to use another technique here, and this is where combinations come in handy!

Objectives

You will be able to:

  • Decide whether or not permutations and combinations are required for a given problem
  • Use Python to calculate combinations and permutations

Let's get started

From the previous lab, you remember that we created a factorial function.

Now, let's use this factorial function to create a function combination as well as permutation, both holding 2 arguments n and k.

def factorial(n):
    prod = 1
    while n >= 1:
        prod = prod * n
        n = n - 1
    return prod
def permutation(n,k):
    None
def combination(n,k):
    None

Great! We can use these functions in the following exercises.

Permutations or Combinations?

Flatiron School is holding a mini mathematics contest and there are 9 people in the last round.

a. Imagine flatiron school is giving out bronze, silver, and gold medal respectively. How many possible ways are there to create this top three?

medal_top_3 = None
medal_top_3 # 504.0

b. Imagine Flatiron school granting the first three contestants a massive fruit basket. How many ways of selecting three people are there in this case?

scholarship_top_3 = None
scholarship_top_3 # 84.0

Some More Practice using Combinations

Imagine you have 6 different consonants and 4 different vowels written on pieces of paper in a bag. You'll draw 5 letters out of the bag.

a. What is the probability that you draw exactly 2 consonants and 3 vowels when drawing 5 letters?

Write the code for getting total number of ways of drawing 2 out of 6 and 3 out of 4 below

draw_cons = None
draw_vow = None

The total number of ways to draw 5 letters out of 10 letters.

sample = None

The probability of drawing 2 consonants and 3 vowels when drawing 5 letters:

None # 0.23809523809523808

b. Out of 6 consonants and 4 vowels, how many words with 2 consonants and 3 vowels can be formed?

You can reuse a part of the previous exercise. Which part? print the result below.

draw_cons = None
draw_vow = None

Now we need to take into account that order is important.

order_5_letters = None

The total number of words with 2 consonants and 3 vowels then equals:

total_words = None
print("In total,",  total_words, "words with 2 consonants and 3 vowels can be formed from our existing letter pool.")
# In total, 7200.0 words with 2 consonants and 3 vowels can be formed from our existing letter pool.

Combinations: Creating Soccer Teams

We're holding a mini soccer tournament and 16 people are participating. We'd like to form 4 teams of 4. How many ways are there to do this?

# your code here  # the answer is 63063000.0

Summary

In this lab, you got some practice with combinations, and deciding whether or not combinations and permutations are required for a problem. Congrats! Combinations and permutations are the cornerstones of combinatorics, and you now know how to use Python to compute them in various settings.

dsc-combinations-lab-online-ds-pt-081219's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.