From a7ad9acdac67bd6bbe7b651d4dd807e4c12c695c Mon Sep 17 00:00:00 2001 From: william Date: Tue, 7 Jul 2015 01:25:30 -0400 Subject: [PATCH] mvc --- README.md | 9 ++ exercises/1-bank-software/README.md | 22 ++++ exercises/1-bank-software/bank_controller.py | 2 + exercises/1-bank-software/bank_models.py | 1 + exercises/1-bank-software/bank_views.py | 0 exercises/1-bank-software/create_db.py | 1 + exercises/2-optimizing-queries/README.md | 58 ++++++++ exercises/2-optimizing-queries/classes.py | 124 ++++++++++++++++++ exercises/2-optimizing-queries/create_db.py | 11 ++ exercises/2-optimizing-queries/intersect.py | 0 exercises/2-optimizing-queries/majors.py | 41 ++++++ exercises/3-vigenere-cipher/README.md | 38 ++++++ .../3-vigenere-cipher/vigenere_controller.py | 6 + .../3-vigenere-cipher/vigenere_models.py | 3 + exercises/3-vigenere-cipher/vigenere_views.py | 0 15 files changed, 316 insertions(+) create mode 100644 README.md create mode 100644 exercises/1-bank-software/README.md create mode 100644 exercises/1-bank-software/bank_controller.py create mode 100644 exercises/1-bank-software/bank_models.py create mode 100644 exercises/1-bank-software/bank_views.py create mode 100644 exercises/1-bank-software/create_db.py create mode 100644 exercises/2-optimizing-queries/README.md create mode 100644 exercises/2-optimizing-queries/classes.py create mode 100644 exercises/2-optimizing-queries/create_db.py create mode 100644 exercises/2-optimizing-queries/intersect.py create mode 100644 exercises/2-optimizing-queries/majors.py create mode 100644 exercises/3-vigenere-cipher/README.md create mode 100644 exercises/3-vigenere-cipher/vigenere_controller.py create mode 100644 exercises/3-vigenere-cipher/vigenere_models.py create mode 100644 exercises/3-vigenere-cipher/vigenere_views.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..c6adb03 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Week 3 Day 2 +============ + +Ready for some serious OOD, SQL, and algorithm challenges today? Grab a coffee and do some stretches, we're gonna tap that sweet brain. + +#### Resources + +[Object Oriented Design](http://en.wikipedia.org/wiki/Object-oriented_design) +[SQL Quick Reference](http://www.w3schools.com/sql/sql_quickref.asp) diff --git a/exercises/1-bank-software/README.md b/exercises/1-bank-software/README.md new file mode 100644 index 0000000..237c164 --- /dev/null +++ b/exercises/1-bank-software/README.md @@ -0,0 +1,22 @@ +## Your Own Private Bank + + +Welcome back! In this challenge you'll be creating a rich Python terminal app emulating bank software. + +#### Step 1: Database + +Our DB schema for this challenge is simple. For now, we only need two tables - Users and accounts. A user can have many accounts. Users should have at minimum a username to log in to the app, a time when they were created, and a permissions level. An account should have a number and a balance. Design the schema in SQL designer if it helps you. + +There's a file called create_db.py - write your sql that creates the db in there to keep everything organized. + +#### Step 2: Our program models + +We need two different classes for users - a client, and a banker. A client should be able to view all their accounts, deposit and withdraw funds to and from their OWN accounts, and transfer money from their OWN accounts to another user account. A banker should be able to create accounts, deposit and withdraw funds from ANY user account, and transfer money between ANY two user accounts. A banker should not have any accounts (no co-mingling of funds) and a person should not see the superuser options that bankers have. How you want to design this is up to you. See [inheritance](http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/) + +I also strongly recommend that you have another class (see [static methods](https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods)) or a module that handles only reading and writing to the database. + +#### Step 3: Controller and Views + +Stick to the MVC pattern - No spaghetti code! Keep your code dry. There's a lot of user choice options which could be a lot of if/else statements - can you think of a better way? + +Present a nice clean interface to the user for the options they are allowed to perform. Some extra account details would be nice to display - some ideas are how long the user has been a member of the bank, which banker created their account, etc. diff --git a/exercises/1-bank-software/bank_controller.py b/exercises/1-bank-software/bank_controller.py new file mode 100644 index 0000000..655662e --- /dev/null +++ b/exercises/1-bank-software/bank_controller.py @@ -0,0 +1,2 @@ +import bank_models +import bank_views \ No newline at end of file diff --git a/exercises/1-bank-software/bank_models.py b/exercises/1-bank-software/bank_models.py new file mode 100644 index 0000000..730f789 --- /dev/null +++ b/exercises/1-bank-software/bank_models.py @@ -0,0 +1 @@ +import sqlite3 \ No newline at end of file diff --git a/exercises/1-bank-software/bank_views.py b/exercises/1-bank-software/bank_views.py new file mode 100644 index 0000000..e69de29 diff --git a/exercises/1-bank-software/create_db.py b/exercises/1-bank-software/create_db.py new file mode 100644 index 0000000..730f789 --- /dev/null +++ b/exercises/1-bank-software/create_db.py @@ -0,0 +1 @@ +import sqlite3 \ No newline at end of file diff --git a/exercises/2-optimizing-queries/README.md b/exercises/2-optimizing-queries/README.md new file mode 100644 index 0000000..44b1ac6 --- /dev/null +++ b/exercises/2-optimizing-queries/README.md @@ -0,0 +1,58 @@ +## Optimizing Queries and Data Comparisons + +This challenge will take what we already know about SQL and Python and add the necessary tools to optimize how we make queries and compare data. + +#### Setup + +In create_db.py, initialize and create a new database "schedules.db" and give it the necessary tables to hold the following: + +Students have a name, and a major. +Classes have a title, and a field of study. + +Major and field of study are the same, ie. Economics is a major and a field of study. + +Students have many classes. +Classes have many students. + +Write a seed_db() function that takes the existing data in create db and randomly fills the database with it. + + +#### Query + +Write a function shared_classes() in Python that takes two student names. It should return the classes they take together, if any. How you want to do this is open ended. + +What is the Big O time complexity of your algorithm for loading the students and comparing their classes? Write it down, and benchmark your code. + +#### First Optimization - the Index + +Read [how indexes work](http://www.programmerinterview.com/index.php/database-sql/what-is-an-index/) here. What type of data structure does sqlite3 use to hold indexes? + +Now, add an index to one or more of your tables on columns you think would be appropriate. + +Benchmark again - what is your speed now? + +#### Second Optimization - use a Set + +Are you comparing the data returned from the DB in Python? If so, you probably aren't using the right data type. We haven't covered Sets yet - read about them [here](https://docs.python.org/3.4/library/stdtypes.html#set) + +A set is very similar to a dictionary, only it does not store values. Only keys. The syntax is like so: +``` +{'Programming', 'Calculus', 'Literature'} +``` +Use sets to store the returned class data about both students and use set's built in methods to find the intersection if it exists. + +The Big O complexity of this operation is `O(len(x) * len(y))`. If you did the most simple comparison with arrays, it was probably `O(len(x) * len(y)**2)`. + +Read about sets. Why is this? Here's a [list](https://wiki.python.org/moin/TimeComplexity) of all Python datatype method's time complexity. + +#### Third Optimization - a better (longer!) Query + +Instead of doing all your comparisons on the Python side, can we pull the information straight out of the database with a more advanced query? + +Before you write the query to replace your original answers- let's write a new function that takes a Major and number of students, and returns classes where that number of students or more in that class have that major. + +Now, write your enhanced query to find the intersect without any Python data parsing. + +Benchmark this function against your first two. + +Sandbox!! You will want to get familiar with the following SQL commands: GROUP BY, HAVING, IN. diff --git a/exercises/2-optimizing-queries/classes.py b/exercises/2-optimizing-queries/classes.py new file mode 100644 index 0000000..b2b5784 --- /dev/null +++ b/exercises/2-optimizing-queries/classes.py @@ -0,0 +1,124 @@ +def classes(): + return ['Pharmacology for AT', + 'Human Nutrition', + 'Comp Networks & Security', + 'Principles of Counseling', + 'Intro to Human Com', + 'Mass Comm', + 'Public Relations', + 'Com Relations in Orgs', + 'Survey of Com Research', + 'Technology and Society', + 'Intro Comps & Office', + 'Intro to Microsoft Excel', + 'Intro to Microsoft Acces', + 'Computer Science II', + 'Computer Science II Lab', + 'Logic for Comp Scientist', + 'Comp NW Culture: Music', + 'Comp NW Social Systems', + 'Economic Life', + 'Principle Microeconomics', + 'The Global Economy', + 'Exceptionalities', + 'Continuation Prep Math', + 'Internship I', + 'Engineering Fundamentals', + 'Acad. Writing & Reading', + 'Research Writing', + 'Business Writing', + 'British Texts', + 'Post-Colonial Texts', + 'Technical Writing', + 'Intro to Poetry Writing', + 'Intro to Food Science', + 'Farm Business Management', + 'Food Plant San and HACCP', + 'Food Microbiology', + 'Food Chem and Analysis', + 'Food Laws and Regulation', + 'West. Civ. to 1500', + 'West & World since 1500', + 'Tech Based Ventures', + 'I&E Seminar Series', + 'Illustration', + 'E-Commerce Advertising', + 'Psychology of Sport', + 'Human Resources Mgt', + 'Internship in IS', + 'Principles of Marketing', + 'Music Listening', + 'Music in Western Culture', + 'Patho Across Lifespan', + 'Found of Research & EBP', + 'Transition Role Prof Nur', + 'Nur Role for Unlicensed', + 'Nursing', + 'Ldrship & Mngmt', + 'Colab Impv Pat Hlth Outc', + 'Honors Project Seminar', + 'Professional Devel II', + 'Professional Devel IV', + 'Integ Office Software', + 'Admin Office Management', + 'Self as Leader', + 'Leading Change', + 'Political Life', + 'International Politics', + 'Cyber Crime', + 'Topics Criminal Justice', + 'Intro to Psychology Lab', + 'Psych of Disabilities', + 'Childhood & Adolescence', + 'Psychology Men and Women', + 'Forensic Psychology', + 'Honors Pharmacology Res', + 'Drug & Alcohol Abuse', + 'Rehab Internship', + 'Educational Interpreting', + 'SLI Senior Capstone', + 'Intro to Sociology', + 'Grow/Change Urb Society', + 'Womens Studies', + 'Ethics in Engineering', + 'Adv Ergon', + 'Lean Proc Impr Engr', + 'Comp Networks & Security Lab', + 'Host Computer Security', + 'Information Security', + 'Adv. Comp. Networks', + 'CNL Theory and Practice', + 'Techniques of Counseling', + 'Stats Res for Counseling', + 'Group Background &Theory', + 'Asses & Eval in Counsel', + 'Marriage', + 'Coun Life-Span Develop', + 'Pro Orient Eth & Leg Iss', + 'Multicultural Counseling', + 'Prin & Prac of Schl Coun', + 'Human Sexuality Counsel', + 'Clin Assess in Cnl Prac', + 'Diagnosis Clin Cnl Prac', + 'Comp Sys & Structures', + 'Functional & Logic Prog.', + 'Information Retrieval', + 'Eco Applica Internet II', + 'Read & Lit II: Int Spec', + 'Teach in the Amer Ed Sys', + 'Action Research: Science', + 'Diagnosis and Assessment', + 'Pract I: Intervention', + 'Literacy Inquiry Project', + 'Ldrshp Schl Improvement', + 'Analysis of Teaching', + 'Tchr Ldr Masters Exit', + 'Data Driven Decisions', + 'Advanced Ed Measurement', + 'Bldg-Level Leadership', + 'Bldg Budget', + 'Principal Practicum', + 'Adv Tchr Ldr Seminar', + 'Pol & Soc Contexts', + 'Organizational Behavior', + 'Superintendent Practicum'] \ No newline at end of file diff --git a/exercises/2-optimizing-queries/create_db.py b/exercises/2-optimizing-queries/create_db.py new file mode 100644 index 0000000..870cc65 --- /dev/null +++ b/exercises/2-optimizing-queries/create_db.py @@ -0,0 +1,11 @@ +import sqlite3 +import classes +import majors +import random +from faker import Faker + +def create(): + pass + +def seed(): + pass \ No newline at end of file diff --git a/exercises/2-optimizing-queries/intersect.py b/exercises/2-optimizing-queries/intersect.py new file mode 100644 index 0000000..e69de29 diff --git a/exercises/2-optimizing-queries/majors.py b/exercises/2-optimizing-queries/majors.py new file mode 100644 index 0000000..a0ae0e2 --- /dev/null +++ b/exercises/2-optimizing-queries/majors.py @@ -0,0 +1,41 @@ +def majors(): + return ['Biology', + 'Chemistry', + 'Mathematics', + 'Political Science', + 'Psychology', + 'Social Sciences', + 'Social Work', + 'Sociology', + 'Speech Pathology', + 'Accounting', + 'Finance' , + 'Management', + 'Management Information Systems', + 'Marketing', + 'Family & Consumer Sciences', + 'Child & Family Studies', + 'Dietetics', + 'Retail Merchandising', + 'Art', + 'Ceramics', + 'Drawing', + 'Graphic Design', + 'Painting', + 'Photography', + 'Printmaking', + 'Sculpture', + 'New Media', + 'Mass Communication', + 'Music', + 'Composition', + 'Instrumental Performance', + 'Organ Performance', + 'Piano Pedagogy', + 'Piano Performance', + 'Vocal Performance', + 'Theatre', + 'Acting', + 'Directing', + 'Design and Technology', + 'Musical Theater'] \ No newline at end of file diff --git a/exercises/3-vigenere-cipher/README.md b/exercises/3-vigenere-cipher/README.md new file mode 100644 index 0000000..1ccd7c2 --- /dev/null +++ b/exercises/3-vigenere-cipher/README.md @@ -0,0 +1,38 @@ +## Vigenere Cipher + +#### Yeah crypto! + +Remember the Caesar cipher we did? Well we're moving up in the world. + +The [Vigenere cipher](http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) was formulated in the 1400s and accomplished then using metal discs. + +While much more powerful cryptography exists and is used, the Vigenere cipher is still theoretically unbreakable. + +Here's a [video that explains it in depth](https://www.youtube.com/watch?v=9zASwVoshiM) + +The way it works is simple. You have a string to encrypt, and a key string. Moving one letter at a time simultaneously through both strings, raise the string to encrypt's character by the value of the key string's character, with A equal to 0, B equal to 1, and so on. When you reach the end of the key string, start again at the beginning. + +For example, say we want to encrypt "programmingrules" with the key string "ratchet", this is our result + + to_encrypt: programmingrules + key string: ratchetratchetra + resulting:: grhiyefdigiyyevs + +If you don't see it right away, look at the "a" characters. When a in ratchet occurs, the letter in the result is unchanged. When a is in string to encrypt, the resulting letter is equal to the character in the key string. + +The key is merely the word "ratchet" - as the program runs, the word is repeated to match the length of the string you wish to encrypt. This is what is demonstrated above. + + +#### Requirements + +Note that, like the caesar cipher, when we go past 'Z' we start again at 'A'. Leave spaces and symbols untouched and add them to your resulting string. We must also account for both uppercase and lowercase letters and leave them as the user input them. + +In Python, we use ord() to get the ASCII numeric value of a character and chr() to convert ASCII value back to a character. + +Using an MVC framework, make an app that any user can use. Give them a menu view that lets them choose to encrypt or decrypt, and takes the string and the key. Write the necessary functionality in your model in a vigenere class. There can be a lot here and your code can become a mess fast - stick to one method doing one thing. + +No need for any persistence here. + +Tie them together with the controller. + +# Let's go crypto! diff --git a/exercises/3-vigenere-cipher/vigenere_controller.py b/exercises/3-vigenere-cipher/vigenere_controller.py new file mode 100644 index 0000000..f2efad0 --- /dev/null +++ b/exercises/3-vigenere-cipher/vigenere_controller.py @@ -0,0 +1,6 @@ +import vigenere_views +import vigenere_models + +class Controller: + def __init__(self): + pass \ No newline at end of file diff --git a/exercises/3-vigenere-cipher/vigenere_models.py b/exercises/3-vigenere-cipher/vigenere_models.py new file mode 100644 index 0000000..9e1d155 --- /dev/null +++ b/exercises/3-vigenere-cipher/vigenere_models.py @@ -0,0 +1,3 @@ +class Vigenere: + def __init__(self): + pass \ No newline at end of file diff --git a/exercises/3-vigenere-cipher/vigenere_views.py b/exercises/3-vigenere-cipher/vigenere_views.py new file mode 100644 index 0000000..e69de29