编程珠玑第三章总结
来源:互联网 发布:qq五笔for mac版 编辑:程序博客网 时间:2024/06/05 13:31
第二题:直接用两个数组就解决了。
def const_recursion(k,a,c,m): def get(n): if n <= len(a):return a[n-1] next = 0 kase = 0 for i in range(n-k-1,n-1): next += a[i]*c[kase] kase += 1 next += c[kase]#c_k+1 a.append(next) return next for i in range(m): print(get(i+1))
第三题:此题可以采用编码方式进行绘制,由于十分繁琐,只画了几个字母的情况。
const_code = {'A':'1 2 b 1 * 2 b/1 1 b 3 * 1 b/1 1 * 3 b 1 *',\'I':'1 9 */3 3 b 3 * 3 b/1 9 *',\'D':'1 1 * 5 b/1 1 * 3 b 1 * 1 b/1 1 * 4 b 1 */1 1 * 3 b 1 * 1 b/1 1 * 5 b'}const_code['B'] = const_code['D']+'/'+const_code['D']def get(char): if char == 'b':return ' ' return chardef decode_line(line_code): line_code = line_code.split(' ') length = len(line_code) index = 0 while index <= length-1: cols = int(line_code[index]) char = line_code[index+1] index += 2 print(get(char)*cols,end ='') print('')def de_code(code): lines = code.split('/') for line in lines: rows = int(line[0])#获取要连续执行的行数 for row in range(rows): decode_line(line[2:])def output(): for each in const_code: de_code(const_code[each]) print('='*10)output()
画的不是很像…
第四题:这道题目有一点小技巧,我自己的思路不是很好,不利于拓展性.
思路1:直接模拟就好,相对繁琐,需要从第一个日期开始,分几个阶段模拟,代码就不给出了。
思路2:对每一个日期在各自的年份进行编号,然后相减,并且加上差的年份*365 or 366即可。这个思路是很巧妙的,当然你想到了也就不难,没想到就和我一样采用模拟的办法确实要繁琐一些,但是算法复杂度是一样的。编号的好处在于对其他一些需要利用日期的需求都可以借助编号来解决!另外,这里有一个小细节很容易错,在计算编号的时候,需要要注意每过一个月都要加上整个月的天数而不是月末减月初!∗∗Excited!∗∗
from collections import namedtupledate = namedtuple('date',['year','month','day'])const_month = [None,31,28,31,30,31,30,31,31,30,31,30,31]base_date = date(2016,6,6)#周1weekdays = ['一','二','三','四','五','六','七']def is_special(year): if not year%100: return not year%400 else:return not year%4def get_endof_month(year,month): if month != 2:return const_month[int(month)]#不是2月 elif is_special(year):return 29#是润年的2月 return 28#普通2月def get_own_index(date): first_index = 0 for first_month in range(1,date.month): month_end = get_endof_month(date.year,first_month) first_index += month_end first_index += (date.day -1) return first_indexdef fast_offset(date1,date2): gap = 0 for year in range(date1.year,date2.year): gap += 366 if is_special(year) else 365 #计算两个日期在各自年份的编号 first_index = get_own_index(date1) second_index = get_own_index(date2) gap += (second_index - first_index) return gapdef get_weekday(date): offset = fast_offset(base_date,date)%7 return '周'+weekdays[offset]def generate_calender(year,month): first_date = date(year,month,1) end_date = date(year,month,get_endof_month(year,month)) prefix = str(year)+'.'+str(month)+'.' for i in range(7): print(' 周'+weekdays[i],end = ' ') print('') offset = fast_offset(base_date,first_date)%7 print((offset*10)*' '+' ',end='') count = 0 for each in range(end_date.day-first_date.day+1): curr_date = date(year,month,1+each) print(str(1+each),end=' '* (8 if (1+each)>9 else 9)) if not (offset+each+1)%7:print('\n ',end='')generate_calender(2016,6)
注意一点,对齐格式还是有点麻烦
习题8:注意补充完不足的字节就好。
const_cache = {0:'',1:'35',2:'01245',3:'01246',4:'1346',5:'01236',6:'012356',7:'246',8:'0123456',9:'012346'}def process_one_digit(num): data = ['0' for i in range(8)] rule = const_cache[num] for each in rule: each = int(each) data[each] = '1' return ''.join(data)def complete(x): x = str(x) x += '0'*(5-len(x))+x return xdef process_input(bit_16_num): x = int(bit_16_num,base = 2) x = complete(x) output = '' for each in x: output += process_one_digit(int(each)) return outputprint(process_input('11111'))
0 0
- 编程珠玑第三章总结
- 编程珠玑第三章
- 编程珠玑第三章
- 编程珠玑第三章
- 编程珠玑第三章
- 编程珠玑第三章习题
- 编程珠玑第二章总结:
- 编程珠玑第四章总结
- 【编程珠玑】第三章 数据决定程序结构
- 《编程珠玑》--第三章 数据决定程序结构
- 编程珠玑第三章课后习题
- 【编程珠玑】第三章:数据决定程序结构
- 【编程珠玑】第三章:数据结构程序
- 日期问题函数(编程珠玑第三章)
- 编程珠玑第三章习题答案
- 编程珠玑之第三章习题1
- 编程珠玑之第三章习题2
- 编程珠玑之第三章习题3
- Activity_ContentProvider
- 求所有的水仙花数
- 最小生成树
- windows下iOviyam的安装
- JavaScript 淡出淡入效果并增加LEFT值实现移动到屏幕中间
- 编程珠玑第三章总结
- 程序员的出路-追求卓越,有计划、有目标高效的做事情
- pull解析和Tomcat
- WallpaperManager
- 数据驱动精准化营销在大众点评的实践
- 第12周项目2-Time类中的运算符重载
- Android用户登陆状态信息
- JavaScript 运行缓冲,从快到慢
- UIViewAutoresizing控件旋转时大小自动伸缩