This commit is contained in:
William Mantly 2016-01-26 17:59:29 -05:00
parent 0ca1d6e179
commit b43c3852d2
6 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,41 @@
Binary Converter
================
In this exercise you will be making functions that convert between base 10 numbers and binary.
Binary notation is based on powers of 2. Each digit is a power of 2. The first digit is 2^0, second 2^1, etc... You then add all of the digits together.
For example, 101 is 5 (4 + 1). 110 is 6 (4 + 2). 1100 is 12 (8 + 4)
Here is a chart of some base 2 vs base 10 notation numbers:
0.....0
1.....1
10.....2
11.....3
100.....4
101.....5
110.....6
111.....7
1000.....8
1001.....9
1010.....10
1011.....11
1100.....12
1101.....13
####Step 1
Write a method that takes binary numbers and outputs a base 10 number.
binary_to_decimal(1011) ## returns 11
####Step 2
Write a method that takes decimal numbers and returns it in binary notation.
decimal_to_binary(12) ## 1100
Resources
----------
[How to Convert Decimal to Binary](http://www.wikihow.com/Convert-from-Decimal-to-Binary)

View File

@ -0,0 +1,16 @@
def binary_to_decimal(num):
pass
def decimal_to_binary(num):
pass
assert binary_to_decimal(0) == 0, "0 to decimal should return 0"
assert binary_to_decimal(1011) == 11, "1011 to decimal should return 11"
assert binary_to_decimal(101011) == 43, "101011 to decimal should return 43"
assert binary_to_decimal(101011101) == 349, "101011101 to decimal should return 349"
assert decimal_to_binary(0) == 0, "0 to binary should return 0"
assert decimal_to_binary(5) == 101, "5 to binary should return 101"
assert decimal_to_binary(10) == 1010, "10 to binary should return 1010"
assert decimal_to_binary(113) == 1110001, "113 to binary should return 1110001"

View File

@ -0,0 +1,70 @@
###Credit Card Validator
Using the [Luhn Algorithm](http://en.wikipedia.org/wiki/Luhn_algorithm), also known as "modulus 10", we will be determining the validity of a given credit card number.
For now, we are just editing the included python file. You will find the skeleton of the `CreditCard` class inside.
###The Class
We want our class to have its three main properties set on [instantiation](http://en.wikipedia.org/wiki/Instance_(computer_science)) - card_number, card_type, and valid. Look at the code, you'll see this already there.
If the card number given passes the Luhn algorithm, valid should be true and cardType should be set to 'VISA', 'AMEX', etc. If it does not pass, valid should be false and cardType should be set to "INVALID"
This way, we can do this:
```python
myCard = CreditCard('347650202246884')
myCard.valid ## true
myCard.card_type ## 'AMEX'
myCard.card_number ## '347650202246884'
```
There are three instance methods. You may perform these methods in the order you see fit.
`determine_card_type` should check whether or not the credit card has valid starting numbers and return the card type.
Visa must start with 4
Mastercard must start with 51, 52, 53, 54 or 55
AMEX must start with 34 or 37
Discover must start with 6011
`check_length` should check whether or not a credit card number is a valid length.
Visa, MC and Discover have 16 digits
AMEX has 15
`validate` should run the Luhn Algorithm and return true or false.
###The Algorithm
From the right most digit, double the value of every second digit. For example:
`4 4 8 5 0 4 0 9 9 3 2 8 7 6 1 6`
becomes
`8 4 16 5 0 4 0 9 18 3 4 8 14 6 2 6`
Now, sum all the individual digits together. That is to say, split any numbers with two digits into their individual digits and add them. Like so:
`8 + 4 + 1 + 6 + 5 + 0 + 4 + 0 + 9 + 1 + 8 + 3 + 4 + 8 + 1 + 4 + 6 + 2 + 6`
Now take the sum of those numbers and modulo 10.
80 % 10
If the result is 0, the credit card number is valid.
###Tests
Make sure your code passes all the assert tests.
Write your own assert tests to test any other possible cases where your code might fail.
###Goals
Keep your code super clean and [DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself).
If you are repeating yourself, stop and think about how to better approach the problem.
Keep your code encapsulated - imagine your CreditCard class is an interface other code will need to read. You want it as separate and unentangled as possible. Your class should not be dependent on any code outside of it - except, of course, code to instantiate it.

View File

@ -0,0 +1,60 @@
class CreditCard:
def __init__(self, card_number):
self.card_number = "the card number"
self.card_type = "a string"
self.valid = "a boolean"
#Create and add your method called `determine_card_type` to the CreditCard class here:
def determine_card_type(self):
pass
#Create and add your method called `check_length` to the CreditCard class here:
def check_length(self):
pass
#Create and add your method called 'validate' to the CreditCard class here:
def validate(self):
pass
#do not modify assert statements
cc = CreditCard('9999999999999999')
assert cc.valid == False, "Credit Card number cannot start with 9"
assert cc.card_type == "INVALID", "99... card type is INVALID"
cc = CreditCard('4440')
assert cc.valid == False, "4440 is too short to be valid"
assert cc.card_type == "INVALID", "4440 card type is INVALID"
cc = CreditCard('5515460934365316')
assert cc.valid == True, "Mastercard is Valid"
assert cc.card_type == "MASTERCARD", "card_type is MASTERCARD"
cc = CreditCard('6011053711075799')
assert cc.valid == True, "Discover Card is Valid"
assert cc.card_type == "DISCOVER", "card_type is DISCOVER"
cc = CreditCard('379179199857686')
assert cc.valid == True, "AMEX is Valid"
assert cc.card_type == "AMEX", "card_type is AMEX"
cc = CreditCard('4929896355493470')
assert cc.valid == True, "Visa Card is Valid"
assert cc.card_type == "VISA", "card_type is VISA"
cc = CreditCard('4329876355493470')
assert cc.valid == False, "This card does not meet mod10"
assert cc.card_type == "INVALID", "card_type is INVALID"
cc = CreditCard('339179199857685')
assert cc.valid == False, "Validates mod10, but invalid starting numbers for AMEX"
assert cc.card_type == "INVALID", "card_type is INVALID"

View File

@ -0,0 +1,20 @@
Roulette Wheel
===============
####Lets make a terminal game of Roulette!
![Roulette Board](http://2.bp.blogspot.com/-kGS1X7KlTDE/U0oxF27SOdI/AAAAAAAABJQ/XkIqmz1h5AA/s400/americanroulette.png)
Write a program that takes the player's bet. The player should be able to bet on any number of spaces, as well as red, black, odd, even, 1-18, and 19-36.
We are modeling the American Roulette board, which includes a "00" space.
Take a look at the betting odds table for roulette [here](http://en.wikipedia.org/wiki/Roulette#Bet_odds_table)
The program should display their odds against winning in real time as they choose places.
When the user decides to roll, they should get the random result and their payout if any. You do not need to implement betting, although you can if want to! You can represent payout as a multiplier of the amount they won over the amount they bet.
Make it fun!

View File

@ -0,0 +1,3 @@
class Roulette:
def __init__(self):
pass