{ "metadata": { "name": "", "signature": "sha256:3e3ca90bdc0f09b43aa11f98861ff402ea48e82c35ad4f6c42ced0b5158a79bf" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Week 1: Python/Postgres/Ipython/Pandas\n", "Week one was all about getting your programming up to par.\n", "\n", "Class would start at 930 with a mini quiz. Basically a warm up to get yourself in the data mood. Then we would move on to the morning lecture. Introducing a new idea/frame work of how to do things. After that we moved onto the indiviudal assignment for the day. Here we explore the idea in the first lecture and get our feet wet in that area. Time absolutely flew by this first week. After the individual assignment and lunch (free mini food at galvanize btw) it was on to the afternoon lecture. This time we take the topic that we learned in the morning and go more in depth. The lectures are not necessarily meant to teach you all the little details, but rather show you what is possible, get the ideas in your head, and prepare you enough to go out and try/fail/succeed repeat on your own. Once the afternoon lecture is done we move on to the pair programming assignment. This is usually a much larger task and one that requires a ton of pre planning. I've been fortunate to have good partners and learn a ton from other people.\n", "\n", "I spent the weekend preping for week 2 which is all about statistics. Although week 1 was tiring, it wasn't conceptually difficult to grasp the topics. I know stats however, is going to be a little more difficult. I'm exicted to see what the best practices are in the work place when it comes to the hardcore math\n", "\n", "Material:\n", "1. Python, intros\n", "2. SQL Zoo, Postgres\n", "3. Pandas\n", "4. All around best practices for exploratory data analysis\n", "\n", "Lets get into a little code from the week. Here is a large portion of a blackjack game, [GScott](https://github.com/gscottstukey) and I did. The goal of the game was to get your OOP up to speed. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "#from deck2 import Deck\n", "#from blackjack_player import Player\n", "import sys\n", "\n", "class Blackjack(object):\n", " def __init__(self):\n", " self.player1 = self.create_player(init_name = \"Player 1\")\n", " self.dealer = self.create_dealer()\n", " self.deal()\n", "\n", "\n", " def create_player(self, init_name):\n", " name = raw_input(\"Enter %s's name: \" % init_name)\n", " wallet = int(raw_input(\"Enter %s's wallet: \" % name))\n", " return Player(name, wallet)\n", "\n", " def create_dealer(self):\n", " return Player(\"Dealer\", 0)\n", "\n", " def deal(self):\n", " #start new game by asking for bets\n", " self.reset_game()\n", " bet_size = self.ask_bet()\n", " \n", " #Initialize players hands\n", " self.player1.hand = self.draw_cards(2)\n", " print \"%s, you have %s, %s\" % (self.player1.name, self.player1.hand, self.player1.hand_value())\n", " self.dealer.hand = self.draw_cards(2)\n", " print \"%s, you have %s, %s\" % (self.dealer.name, self.dealer.hand, self.dealer.hand_value())\n", " \n", " #run through player 1's turn\n", " player_didnt_bust = self.player_round(self.player1)\n", " if player_didnt_bust:\n", " print \"%s has %d.\" % (self.player1.name, self.player1.hand_value())\n", " else: \n", " print \"%s has busted with %d...\" % (self.player1.name, self.player1.hand_value())\n", " self.ask_new_game()\n", "\n", " #run through the dealer's turn if the player didn't bust\n", " dealer_bust = not(self.dealer_round())\n", " if dealer_bust:\n", " print \"Dealer busted with %d\" % (self.dealer.hand_value())\n", " print \"Awesome %s, you won\" % (self.player1.name)\n", " self.add_to_wallet(bet_size*3.0/2)\n", " self.ask_new_game()\n", "\n", " #compare results if neither busted\n", " self.compare_hands(self.player1, self.dealer, bet_size)\n", "\n", " #prompt for a new game\n", " self.ask_new_game()\n", "\n", " def add_to_wallet(self, amount):\n", " self.player1.wallet += amount\n", " print \"%s now has %d in their wallet\\n\" % (self.player1.name, self.player1.wallet)\n", "\n", "\n", " def ask_new_game(self):\n", " #check to make sure player1 isn't broke! \n", " if self.player1.wallet <= 0:\n", " print \"SORRY, BRAH! You are broke!... Goodbye!\\n\"\n", " sys.exit()\n", "\n", " #check if the player wants to play again\n", " play_again = raw_input(\"\\n\\n\\n%s, do you want to play again (y/n)? \" % (self.player1.name))\n", " if play_again == \"Y\" or play_again == \"y\":\n", " print \"\\n\\n\\n\"\n", " self.deal()\n", " elif play_again == \"N\" or play_again == \"n\":\n", " print \"Thank you for playing, %s!\\n\\n\\n\" % (self.player1.name)\n", " sys.exit()\n", " else:\n", " print \"command not accepted, please make sure to enter a legal character...\\n\"\n", " self.ask_new_game()\n", "\n", "\n", " def player_round(self, player):\n", " hand_continue = True\n", " hand_value = self.player1.hand_value()\n", " while hand_continue and hand_value <=21:\n", " hand_continue = self.hit_or_not()\n", " hand_value = self.player1.hand_value()\n", " print self.player1.hand, hand_value\n", " return hand_value <=21\n", "\n", "\n", " def dealer_round(self):\n", " hand_value = self.dealer.hand_value()\n", " while hand_value <= 16:\n", " self.dealer.hand.append(self.deck.draw_card())\n", " hand_value = self.dealer.hand_value()\n", " print \"Dealer now has %s, %s\" % (self.dealer.hand, self.dealer.hand_value())\n", " return hand_value <= 21\n", "\n", " def reset_game(self):\n", " self.dealer.reset()\n", " self.player1.reset()\n", " self.deck = Deck()\n", " self.deck.shuffle()\n", " \n", " def ask_bet(self):\n", " print \"%s has %d in their wallet\" % (self.player1.name, self.player1.wallet)\n", " bet_size = int(raw_input(\"%s, what is your bet?: \" % (self.player1.name)))\n", " if self.player1.has_funds(bet_size):\n", " self.player1.wallet -= bet_size\n", " print \"%s bet size: %d, wallet balance: %d\" % (self.player1.name, bet_size, self.player1.wallet)\n", " else:\n", " self.ask_bet()\n", " return bet_size\n", "\n", " def draw_cards(self, n):\n", " cards = []\n", " for i in xrange(n):\n", " cards.append(self.deck.draw_card())\n", " return cards\n", "\n", " def hit_or_not(self):\n", " prompt = raw_input(\"%s, do you want to hit (y/n)? \" % (self.player1.name))\n", " if prompt == \"Y\" or prompt == \"y\":\n", " self.player1.hand.append(self.deck.draw_card())\n", " return True\n", " elif prompt == \"N\" or prompt == \"n\":\n", " return False\n", " else:\n", " print \"command not accepted, please make sure to enter a legal character...\\n\"\n", " self.hit_or_not()\n", "\n", " def compare_hands(self, player, dealer, bet_size):\n", " if dealer.hand_value() > player.hand_value():\n", " print \"Sorry %s, you lost\" % (player.name)\n", " elif dealer.hand_value() < player.hand_value():\n", " print \"Awesome %s, you won\" % (player.name)\n", " self.add_to_wallet(bet_size*3.0/2)\n", " else:\n", " print \"You guys tied, you get your money back\"\n", " self.add_to_wallet(bet_size) \n", "\n", "if __name__ == '__main__':\n", " pass\n", " #game = Blackjack()\n", " #game.play_game()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }