GithubHelp home page GithubHelp logo

py-code-examples's Introduction

Code Examples in Python

Example 1 - Weighted Die:

####Task definition:

Let’s implement model of six sided die with weights on the sides, so we don't have an even probability distribution, die is weighted by a list of weights passed in at construction time.

####Testing conditions:

After 100k iterations of throwing this die, the results should closely match the desired distribution, and this should be reproducible in the unit test example.

since these are weights on a probability distribution, these should sum to one, and the incoming array should be of length 6. You should throw if either of these preconditions is false

source files: six_sided_dice.py six_sided_dice_numpy.py unittst source: test_weighted_dice.py


####Runtime example:

>>> from six_sided_dice import SixSidedWeightedDie
>>> weights = [.05, .10, .15, .2, .2, .3]
>>> SixSidedWeightedDie(weights).throw_die()
4
>>> SixSidedWeightedDie(weights).throw_die()
4
>>> SixSidedWeightedDie(weights).throw_die()
5
>>> SixSidedWeightedDie(weights).throw_die()
3
>>> SixSidedWeightedDie(weights).throw_die()
6

####run unittest:

python -m unittest test_weighted_dice

Example 2 - palindrome generator

####Task definition:

  1. Let's implement palindrome generator, which reads a string of chars as an input, and produces unique palindromes (strings which read the same reversed) unique means that any following value was never produced before.

  2. first produces all 1 letter palindromes, then 2 letter ones, then ...

For example: if given string is 'abc', then 'a', 'aa', 'aba', etc are all legal results (i.e., you can repeat the input chars just not the result).

source files: palindrome.py


####runtime example:

>>>from palindrome import palindromes
>>>palindromes('abc');
['a', 'c', 'b', 'aa', 'cc', 'bb', 'aba', 'aca', 'aaa', 'cbc', 'bbb', 'bcb', 'bab', 'cac', 'ccc']

####run unittest:

python -m unittest test_weighted_dice

py-code-examples's People

Watchers

 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.