自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(114)
  • 收藏
  • 关注

原创 万能计算器——中缀表达式转换成后缀表达式(C++实现)【可以计算小数和负数】

核心代码与思路:int GetExprValue(vector<string> srcVec) //根据后缀表达式求值{ stack<int> temp; char op = '\0'; int a = 0; int b = 0; for (int i = 0; i < srcVec.size(); i++) { op = srcVec[i][srcVec[i].length() - 1]; if (isdigit(op)) { temp.pus

2022-05-10 21:08:47 1432

原创 多线程实现生产者-消费者问题——C++版本

问题描述:1)生产者进程每次生产1个产品(产品数量大于4时停止生产);2)消费者进程每次消耗2个产品;#include <iostream>#include <thread>#include <mutex>#include <condition_variable>using namespace std;std::mutex mu;std::condition_variable cond;void ProduceThread(int *pr

2022-04-03 13:36:02 1587

原创 Python———删除字符串中的特殊字符

# Demo1: delete the space in src(' www.david@163.com ')# Demo2: delete the '\r' in string from windows file then get linux format without '\r'# Demo3: delete the hanyupinyin tones in 'wén zhāng běn tiān chéng ,miào shǒu ǒu dé zhī 'import unicodedat

2022-01-01 11:53:41 644

原创 Python——字符串的对齐问题

# Now we have a src dict as following:# src_dict = {# 'David':2000# 'Jony':12.3# 'Michel Jackson': 12# 'MM': 13.2# }# we need to get alignment-seted dict as following# dest_dict = {# 'David ':2000# 'Jony ':12.3# 'Michel Jackson'

2021-12-31 23:38:46 730

原创 Python——字符串的连接

# Demo: Now we have a big list with different base data bype(str,int,float)# we need to contact them into a fianl stringsrc = ['abc', 123, 'defg', 49.8, '2021-01-21', '21:23:25']# solution 1:[not recommended as it cost a lot of memory]dest = ''for

2021-12-31 23:18:13 414

原创 Python——将日期格式yyyy-mm-dd转换为mm/dd/yyyy

import relog = open('time.log', 'r').read()print(==========before translating============)print(log)new_log = re.sub('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', r'\g<month>/\g<day>/\g<year>', log, 10)print(==

2021-12-31 23:04:24 2767

原创 Python——找到特定目录下的特定格式文件并修改权限

#Demo: find files ends with '.py' or '.sh' and change its mode executiveimport os, statfile_list = os.listdir('./files') # find all files in dest dirprint(file_list)dest_files = [x for x in file_list if x.endswith(('.sh', '.py'))]print(dest_files)

2021-12-31 22:41:32 1647

原创 Python——拆分包含多种分隔符的字符串

程序功能:现有一个如下的字符串:s = ‘ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz’根据‘,;|\t’四种不同分隔符将字符串拆分import res = 'ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz'res = re.split(r'[,;|\t]+', s)print(res)运行结果:['ab', 'cd', 'efg', 'hi', 'jkl', 'mn', 'opq', 'rst', 'uvw', 'xyz'][

2021-12-30 23:06:20 249

原创 如何学习一门新的编程语言

既然是学习一门新的编程语言,那么前提就是你已经学习过了一门编程语言了——比如C语言(计算机专业出身的应该都学过);在学习新的编程语言的之前要做的不是网上/书店到处找“快速入门”、“21天掌握”之类的“速成秘籍”,而是将已经学习的语言中的框架结构梳理清楚;这一点非常重要,尤其是在互联网高度发达的今天,搜寻一门语言的入门书籍成了一件非常稀松平常的事情,但是正是因为这样,导致我们在选择书籍的时候眼花缭乱,这样就很难学精,学透;我本人是计算机科班出身,接触的第一门语言就是C语言,当时只是掌握了C语言的大部分语法,

2021-12-29 22:19:31 603

原创 Python——for语句中访问可迭代对象

#coding utf-8from random import randintfrom itertools import chain# Demo1: get the total scores of each student; score stored in 3 lists(math,chinese,english)stu_count = 50math = [randint(60, 100) for x in range(stu_count)]chinese = [randint(60, 1

2021-12-29 21:55:03 297

原创 Python——自定义逆序迭代对象

#coding utf-8precision = 0.00000000001 # ensure no exception occurs of float compare operationclass FloatRange: def __init__(self, start, end, step = 0.1) : self.start = start self.end = end self.step = step def __iter__(self) : temp = self.s

2021-12-29 21:53:56 263

原创 Python——yield关键字的应用

程序功能:获取[start,end)区间的质数;from math import sqrtclass PrimeList: def __init__(self, start, end) : self.start = start self.end = end def is_prime(self, num) : if num < 2 : return False for v in (2, sqrt(num) + 1) : if num % v == 0 : r

2021-12-28 23:21:11 196

原创 Pthon——自定义迭代器和可迭代对象

#coding:utf-8from collections.abc import Iterator, Iterablefrom random import randintdef get_weather(city) : return city + str(randint(20, 35))class WeatherIterator(Iterator): def __init__(self, cities) : self.cities = cities self.index = 0

2021-12-28 22:35:42 328

原创 Python——实现历史记录的功能

游戏描述:猜大小。系统随机生成一个整数(1,100)用户通过输入数据来猜测该整数的值;系统根据输入返回三种结果:too big, too small, you win.输入过程中可以查看最近输入了哪些数字,游戏结束后将历史记录值保存在文件’history’中。from random import randintfrom collections import dequeimport picklehistory = deque([], 6)dest = randint(1, 100)def gu

2021-12-26 22:26:51 3229

原创 Python——保持字典的有序性

from random import randintfrom collections import OrderedDictfrom time import timeordered_dict = OrderedDict()players = list('ABCDEFGHI')start_second = time()for i in range(9) : input() p = players.pop(randint(0, 8-i)) end_second = time() print

2021-12-26 21:43:33 676

原创 Python——求多个字典中的公共键有哪些

# Demo1: get the common key in server sets(transeform from dicts)from random import randint, samplefrom functools import reducel = sample('abcdefgh', randint(3, 6))s1 = {x:randint(1, 5) for x in sample('abcdefgh', randint(3, 6))}s2 = {x:randint(1, 5)

2021-12-26 21:13:27 116

原创 Python——字典元素的排序

# Here is a dict with several members, we need to sort them increasingly# Solution 1print('------------------------solution1----------')from random import randintsrc_dict = {x:randint(60, 100) for x in 'BaCdEfGcMzYW'}print(src_dict)dest_temp = zip(sr

2021-12-26 20:51:15 293

原创 Python——根据序列中的元素出现频率筛选频度较高的元素

# Demo1: generate a list with 100 integers(radom value 1_20)# then get the top 3 frequency numbersfrom random import randintfrom collections import Counterimport resrc = [randint(1, 20) for _ in range(100)]dict_src = dict.fromkeys(src, 0)for x in sr

2021-12-26 19:28:44 315

原创 Python——为元组元素命名

# here is a tuple ('David', 29, 'male', '23raff@163.com'), when we want to get its member# we have to use indexe = ('David', 29, 'male', '23raff@163.com')print(e[0])print(e[1])print(e[2])print(e[3])# it is not abvious when we want to the identity o

2021-12-26 17:22:38 469

原创 Python——列表、字典和集合的解析

# Demo1 screen right data you want to get from list by functiondef screen(data): new_data = [] for i in data : if i>0 : new_data.append(i) return new_datafrom random import randintdata = [randint(-10, 10) for _ in range(10)]print('src list:

2021-12-26 17:04:35 138

原创 Python实现文件读写操作

def my_copy(src_file, dest_file) : try: src_file_obj = open(src_file, 'rb') except FileNotFoundError: print('src file is not found, please check the paragram') return dest_file_obj = open(dest_file, 'wb') for block in src_file_obj : dest_file_o

2021-12-22 22:44:56 224

原创 Python——一个例子感受一下“面向对象”

import scheduleimport timedef job() : print('Hello Python')schedule.every(3).seconds.do(job)while True: schedule.run_pending() time.sleep(1)

2021-12-22 21:15:24 321

原创 Python中的多态实例

class Animal(object): def eat(self) : print('Animal eat something')class Dog(Animal): def eat(self) : print('Dog eats meat')class Cat(Animal): print('Cat eats fish')animal = Animal()dog = Dog()cat = Cat()animal.eat()dog.eat()cat.eat()输出

2021-12-22 21:14:17 610

原创 Python——模块和主函数调用

python_module.py#Demo 1: revoke the default function called pow# print(pow(3, 3))#Demo 2: revoke the function pow in math# from math import pow# print(pow(3, 3))#Demo 3: revoke the self defined function of powimport syssys.path.append('./common')

2021-12-20 22:31:14 1130

原创 Python———__init__()和__new__()调用与重写

class MyClass(object): def __new__(cls, *args, **kargs) : print('__new__ method is called and id is {0}'.format(id(cls))) obj = super().__new__(cls) print('id of object is {0}'.format(id(obj))) return obj; def __init__(self, name, age) : super(

2021-12-19 20:35:10 189

原创 Python——多态的实现

class Animal(object): def eat(self) : print('Animal eat something')class Dog(Animal): def eat(self) : print('Dog eats meat')class Cat(Animal): def eat(self) : print('Cat eats fish')animal = Animal()dog = Dog()cat = Cat()animal.eat()dog.eat

2021-12-19 19:22:00 272

原创 Python——继承的实现

# class Person:# def __init__(self, name, age) :# self.name = name# self.age = age# def say_hello(self) :# print('name:', self.name, 'age:', self.age)# mr_smith = Person('Smith', 30)# mr_smith.say_hello()# print(mr_smith)class Person: de

2021-12-19 19:08:25 220

原创 Python——属性和方法的动态绑定

# as the following example, we can create different object with different name or age# but when we want to add a new attribute gender for Smith but not for Jack# Now how we can solve itclass MyClass: def __init__(self, name, age) : self.name = name

2021-12-19 17:17:05 102

原创 Python面向对象——类的封装

# Demo1: put the attribute of a person(Smith, 30years old) into a class name MyClassclass MyClass: name = 'Smith' age = 30 def say_hello(self) : print('My name is %s, and I am %d years old.'%(self.name, self.age))mr_smith = MyClass()mr_smith.say_h

2021-12-19 16:57:53 152

原创 Python——字符串的编码与解码

#encoding and decoding of strsrc = '天生我材必有用's_gbk = src.encode(encoding = 'GBK')s_utf8 = src.encode(encoding = 'UTF-8')print(s_gbk)print(s_utf8)dest_str_from_gbk = s_gbk.decode(encoding = 'GBK')dest_str_from_utf8 = s_utf8.decode(encoding = 'UTF-8')

2021-12-19 16:56:47 288

原创 Python——字符串的格式化输出

# string format output# using %name = '孙悟空'age = 550print('myname is %s and my age is %d'%(name, age))# using .formatprint('myname is {0} and my age is {1}'.format(name, age))#format usage of int and floatnum1 = 200print('%d'%num1)print('%10d'%n

2021-12-19 16:55:56 143

原创 Python——字符串的切片

src = 'hello, python'# how to get 'hello's1 = src[:5]# how to get 'python's2 = src[6:]# how to contact 'hello' and 'python' with '!'s3 = s1 + '!' + s2# show the id of string above allprint(id(src))print(id(s1))print(id(s2))print(id(s3))# how

2021-12-19 16:54:45 218

原创 python学习第二天——装饰器

# decrator of python# definition of func# def func() :# print('Hello world')# def new_func() :# print('decrating new_func')# func()# print('end decrating')# new_func()# when other func needs decrating, we should define another new_func# h

2021-12-14 22:50:11 567

原创 计数排序的Python实现

学习Python的第一天,实现计数排序算法;import randomdef list_init(array, size): for i in range(size): array[i] = random.randint(1, size) return arraydef count_sort(array, size): max = array[0] min = array[0] for index in range(size): if max < array[index]:

2021-12-12 17:29:34 632

原创 如何理解IT工程师文化

如何理解IT工程师文化?必须首先说清楚IT产业的承载——计算机;从本质上说,计算机是工具——是帮助我们实现算力范围内的和谐地解决实际问题的工具。从这一点出发进行讨论的时候,我们就要考虑我们从事的IT产业最终是服务于“内容”;这里的“内容”可以是多种多样的,从这个意义上来说,当今的所有和IT相关的产业恐怕要占据我们日常生活的90%以上。然而,有一些产业表面上看是IT产业,实际上它们的核心则是工业产品;比如:工业软件、管理软件等。尽管IT主导的信息产业革命被很多人称作是“人类第四次革命”;然而今天IT产业本身

2021-12-11 20:43:34 528

原创 如何获取代码片段执行的时间

问题:在实际工作中,需要计算某一个程序中代码片段的执行时间,从而判断这部分代码放在驱动层处理更高效还是放在用户层处理更合适。这里用到的结构体是timeval,通过函数gettimeofday()来获取代码片段起始时间值,从而计算片段执行时间;代码如下:#include <stdio.h>#include <unistd.h>#include <sys/time.h>#define START_TIME \ struct timeval time1, ti

2021-12-08 21:49:26 1370

原创 库函数调用和系统调用的区别

在日常工作中一般会根据不同的情况选择使用系统调用或者库函数,对于它们有什么区别列在下面:

2021-12-07 22:34:33 126

原创 推荐吴军的著作《文明之光》

在读完《浪潮之巅》之后深深地被吴军先生的眼界和见识所震撼,带着更强烈的好奇心打开了吴军的又一著作《文明之光》又被惊到了,序言是王石先生和张首晟教授所做,算是推荐吧。强烈推荐习惯历史的理工科的程序员们阅读,全书层层递进,逻辑清晰,思维开阔…… 困了,睡觉????...

2021-12-04 00:21:41 684

原创 函数中的变量为什么要放在“栈”中?

当我们要解释这个问题时,我们首先回顾一下函数的调用是如何实现的;有如下的代码:int Add(int &a, int &b){ int p = 2 * a; int q = 4 * b; return p + q;}int main(){ int m = 10; int n = 20; int x = Add(m, n); int y = 3*x; return 0;}上图对应上面的代码:当程序执行到point a时,系统先保存当前的上下文——也就是把

2021-11-29 21:40:08 484

原创 栈的应用——实现浏览器中的前进和后退功能

假定有4个网页A、B、C、D;为了实现前进和后退功能我们需要借助两个栈:stackX和stackY;初始时浏览器页面为空,stackX和stackY也为空;当浏览第一个网页A时,stackX.push(A);当跳转第二个网页B时,stackX.push(B);当浏览第一个网页C时,stackX.push©;当跳转第二个网页D时,stackX.push(D);当点击后退按钮时:if (!stackX.isEmpt()){ stackX.pop(); browser.reload();}

2021-11-29 21:15:45 544

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除