第一次写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由于困意加上面瘫学长"听不懂就是你不行"的呵呵传授,全程梦游.最后面对连入门都算不上的几道动态规划,我的内心一片宁静,甚 ...
随机推荐
- Android布局中的空格以及占一个汉字宽度的空格的实现
在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: 窄空格: 一个汉字宽度的空格: [用两个空格( )占一个汉字的宽度时,两个空格比 ...
- 返回页面,主页面不刷新window.history.go(-1),主页面刷新window.location.go(-1)
返回上一页,不刷新 window.history.go(-1) 返回上一页,刷新 window.location.go(-1)
- 动态磁盘恢复为基本磁盘--DiskGenius
近日在老电脑中安装了Win8.1,想不到使用起来比Win7还流畅. 周末,手贱,由于C盘只有10GB,为主分区,D盘有40GB,为扩展分区,想要将C.D两个分区合二为一,在Win8.1的磁盘管理器中, ...
- python遍历目录文件脚本的示例
例子 自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理.没啥技术含量,但是也记录一下吧. 代码如下 复制代码 #!/usr/bin/python# -*- coding: utf-8 ...
- TTTAttributedLabel 如何将多个字符串高亮显示
TTTAttributedLabel进行多个字符串的高亮显示. 需要对每个字符串进行匹配,从而得到所有需要高亮的NSRange,然后利用NSMutableAttributedString对每个NSRa ...
- JS判断移动设备最佳方法
最实用的还是下面这个: 方法一:纯JS判断 使用这方法既简单,又实用,不需要引入jQuery库,把以下代码加入到<head>里即可. <script type=”text/javas ...
- CodeSmith Template Model Output
背景:上学那会儿就接触CodeSmith,是一款非常优秀的代码自动生成工具.以前写过好些基本模版,可惜早不知道扔到哪儿去了,如今只能重新开始,把它捡回来,加油. 效果:将数据库 DataBase 应用 ...
- 使用Ctex总结1
使用Ctex应该是每一个做学术研究的人要学会掌握的. 它的基本结构: \documentclass[11pt,two side,a4paper]{cctart}%使用cctart可以让摘要变成中文,比 ...
- oracle 中proc和oci操作对缓存不同处理
oracle 中proc和oci操作对缓存不同处理
- how to Enable Client Integration
i got a problem,the problem is list cant use export to excel button in sharepoint 2010. I found my a ...