第一次写python
这是一个在BJDP上学习Coding Kata的时候用到的一个练习,原来打算用Java写的,但是一想正好是学习的好机会。
就用Python了。第一次,写的有些复杂。
这个题目是关于购买图书的打折信息的。
题目来源:
http://codingdojo.org/cgi-bin/wiki.pl?KataPotter
class Strategy:
def __init__(self, items):
self.items = items;
self.rate = self.get_discount_rate(); def get_discount_rate(self):
if len(self.items) == 5:
return 0.75;
if len(self.items) == 4:
return 0.8;
if len(self.items) == 3:
return 0.9;
if len(self.items) == 2:
return 0.95;
return 1.0; def get_price(self):
return self.total_price() * self.rate; def total_price(self):
price = 0.0;
for item in self.items:
price += item.book.price;
return price; def count(self):
return len(self.items); class StrategyOptimizer:
def optimize(self, strategies):
found = False;
while True:
found = self.replace_53_with_44(strategies);
if not found:
break;
return strategies; def replace_53_with_44(self, strategies):
strategyMap = {};
strategyMap.clear();
for i in range(0, len(strategies)):
strategy = strategies[i];
strategyMap[strategy.count()] = i; if (strategyMap != None and len(strategyMap) != 0):
if (strategyMap.get(5, None) != None and strategyMap.get(3, None) != None):
self.move_book(strategies[strategyMap[5]], strategies[strategyMap[3]]);
return True;
return False; def move_book(self, source, dest):
item = self.findAnyDiff(source, dest);
if item == None:
return;
source.items.remove(item);
source.rate = source.get_discount_rate();
dest.items.extend([item]);
dest.rate = source.get_discount_rate();
return; def findAnyDiff(self, source, dest):
for item in source.items:
if item not in dest.items:
return item;
return None; class Book:
def __init__(self, index, name, price):
self.index = index;
self.name = name
self.price = price class Item:
def __init__(self, book, count):
self.book = book
self.count = count class Cart:
items = [];
def add_item(self, item):
self.items.append(item); def pick_most_books(cart):
items = [];
for i in range(0, len(cart.items)):
item = cart.items[i];
if item.count == 0:
continue;
items.append(Item(item.book, 1));
cart.items[i].count -= 1;
return items; def is_empty(cart):
for item in cart.items:
if item.count > 0:
return False;
return True; def count_price(strategies):
price = 0;
for s in strategies:
price += s.get_price();
return price; def find_best_solution(cart):
strategies = [];
price = 0.0;
while not is_empty(cart):
items = pick_most_books(cart);
strategy = Strategy(items);
strategies.append(strategy);
return strategies; def count_best_price(cart):
strategies = find_best_solution(cart);
so = StrategyOptimizer();
strategies = so.optimize(strategies)
price = count_price(strategies);
print(price); if __name__ == '__main__':
item_1 = Item(Book("#1.", "Philosophy Stone", 8), 2);
item_2 = Item(Book("#2.", "Secret Chamber", 8), 2);
item_3 = Item(Book("#3.", "Prisoner of Azkaban", 8), 2);
item_4 = Item(Book("#4.", "Goblet of Fire", 8), 1);
item_5 = Item(Book("#5.", "The Order of Phoenix", 8), 1); cart = Cart();
cart.add_item(item_1);
cart.add_item(item_2);
cart.add_item(item_3);
cart.add_item(item_4);
cart.add_item(item_5); count_best_price(cart);
第一次写python的更多相关文章
- 第一次写python爬虫
花了4天终于把写完了把国内的几个漏洞平台爬完了,第一次写py,之前一直都在说学习,然后这周任务是把国内的漏洞信息爬取一下.花了1天学PY,剩下的1天一个.期间学习到了很多.总结如下: ======== ...
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
- 第一次写博客Poj1044
Date bugs Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3005 Accepted: 889 Descript ...
- 写python时加入缩进设置
发现如果用vim写python的时候,还是设成8好像会报错,在现有的基础上,加入下面设置就好了set shiftwidth=4
- 用Emacs 写python了
之前都是用python 自带的IDLE 写 python 的,现在换了Emacs,感觉真是不错,爽. 截图留念: 用了sr-speedbar ,顿时有了IDE 的感觉,是不是很爽. 版权声明:本文为博 ...
- Java第一次写的流布局图形界面,留个纪念
package jisuanqi; import java.awt.*; public class MyFrame extends Frame{ //继承Frame类 public MyFrame() ...
- 第一次写博客,关于前端开发deMVC在js中的应用
对前端MVC MVC分别是model.view.controller的缩写,模型.视图.控制器.这些更加偏向于后台,在以前MVC是只属于后台的.当然随着技术的进步,前端的大牛们将后台的一些东西应用于前 ...
- 在html中写python代码的语法和特点-----基于webpy的httpserver
在html文件里写python语法的内容,的注意事项: 1:python程序中的变量通过以下方法传入到html: 1:通过全局变量 :全局变量是不须要用$def with语法实现传递的,仅仅要定义了 ...
- HDU 2064 菜鸡第一次写博客
果然集训就是学长学姐天天传授水铜的动态规划和搜索,今天讲DP由于困意加上面瘫学长"听不懂就是你不行"的呵呵传授,全程梦游.最后面对连入门都算不上的几道动态规划,我的内心一片宁静,甚 ...
随机推荐
- Linux 内核模块设计
一. 内核模块 1. 头文件 Linux/init.h 和 Linux/module.h 2. 装载内核 insmod 对应的转载函数 module_init(); 3. 卸载内核 rmm ...
- linux下svn命令常用操作
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain 简写:s ...
- Quartz Scheduler(2.2.1) - Working with SchedulerListeners
SchedulerListeners SchedulerListeners are much like TriggerListeners and JobListeners, except they r ...
- Android Wi-Fi基本操作
从用户角度看,Android Wi-Fi模块自下向上可以看为5层:硬件驱动程序,wpa_suppplicant,JNI,WiFi API,WifiSettings应用程序. 1.wpa_supplic ...
- 转 Android 4.0后,自定义Title报错 You cannot combine custom titles with other title feature
自定义Titlebar时为了避免冲突 需要修改:AndroidManifest.xml android:theme="@style/mystyle" styles.xml文件中 ...
- hive 未初始化元数据库报错
启动hive-metastore和hive-server2 用beeline连接hive报错 [root@node04 hive]# beeline Beeline version 0.13.1-cd ...
- JIRA的常用选项
常用的一些选项有: 问题类型 Bug 测试过程维护过程发现影响系统运行的缺陷 New Feature 对系统提出的新功能 Task 需要完成的任务 Improvement 对现有系统功能的改 ...
- 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:
在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...
- UVALive 3645 Objective: Berlin(最大流 :时序模型)
题意:已知n(n <= 150)个城市和m(m <= 5000)个航班,每个航班有出发地.到达地.乘坐人数.起飞时间和降落时间(时间用时和分表示),求从一个指定城市出发,去往另一个指定城市 ...
- [OC] UITabBarController
1. New CocoaTouch class -> Select Objective C -> named RootViewController 2. Disable APC error ...