python使用正则筛选信用卡
作者:水似冰 发布时间:2021-03-10 14:06:47
本文实例为大家分享了python使用正则筛选信用卡的具体代码,供大家参考,具体内容如下
本文来源于两个简单的题目:
1.判断一对单词是否是" Anagrams "
2.判断信用卡是否合理
判断 Anagramsstrong>
anagrams 的百度翻译:由颠倒字母顺序而构成的字(短语)
而题目给出例子:
[ DOG , ODG ]
[ DOG , DOG ]
[ DOG , GOD ]
[ DOG , GDO ]
均为 anagrams 。
那思路就简单了,直接拆分字母,排序,比较就 ok 。
判断信用卡
题目给出的要求如下:
The criteria are:
• It must start with a 4,5 or 6
• It must be exactly 16 digits
• It must be numbers only
• It can have a digits in groups of 4, separated by one hyphen “-“
• It should not contain any other characters.
• It must NOT have any 4 repeated digits.
样例输出如下:
378282246310005 Invalid
30569309025904 Invalid
6011111111111117 Invalid
5123-2332-3232-3213 valid
py文件
两个题目合并在一个 py 文件中。
而入参数分别是两个文件的名字,一个是 anagram.txt ,另一个是 credit_cards.txt ,他们分别长这样:
最后的程序长这样:
#! /usr/bin/python3
import re
###############################
##You need to implet the following methods:
##
##Question 1
##anagram_validator()
##
##Question 2
##credit_card_validator()
################################
###################################################
# Question 1: Check for anagrams:#
###################################################
def get_list(src_str):
sub_list=[]
word_tmp=''
for word in src_str:
if word != ',':
word_tmp = word_tmp+word
else:
sub_list.append(word_tmp)
word_tmp = ''
return sub_list
def read_anagram(file_name):
'''
Input: a file name
Return: a nested list of two words list
Example : [[word1,word2],[word3,word4]...etc]
'''
with open(file_name,encoding = 'utf-8') as f:
return_list =[]
file_conment = f.read()
file_conment_new = re.sub(r'\n',',',file_conment)
file_conment_new = file_conment_new+','
subs_list = get_list(file_conment_new)
i=0
while i < (len(subs_list)-1):
return_list.append([subs_list[i],subs_list[i+1]])
i=i+2
return return_list
def anagram_validator(anagram):
'''
Input is the output from "read_anagram()".
Return: list of "anagrams" or "Not anagrams" values for each two words
example input (dog,gdo),(try,elm) then output would be ["anagrams","Not anagrams"] with sequence of the input
'''
result_list=[]
for i in range(len(anagram)):
word_font = ''.join((lambda x:(x.sort(),x)[1])(list(anagram[i][0])))
word_back = ''.join((lambda x:(x.sort(),x)[1])(list(anagram[i][1])))
if word_font == word_back:
result_list.append('Anagrams')
else:
result_list.append('Not anagrams')
return result_list
############################################
# Question 2: Validate credit cards #
############################################
def read_credit_cards(file_name):
'''
Input: a file name
Return tuple of numbers
'''
with open(file_name,encoding='utf-8') as f:
card_conment = f.read()
card_conment = re.sub(r'\[','',card_conment)
card_conment = re.sub(r'\]','',card_conment)
card_conment = card_conment+','
card_list = get_list(card_conment)
return tuple(card_list)
def credit_card_validator(numbers):
'''
Input: tuple of numbers
Return:dictionary of credit card numbers where key is the number and value if valid or invalid
'''
validator_list=[]
credit_dict={}
for i in range(len(numbers)):
validator_list.append(numbers[i])
for i in range(len(numbers)):
ch_num = 0
repeat_flag = 0
for ch in validator_list[i]:
if ord("A")<=ord(ch)<=ord("Z"):
ch_num+=1
break
tmp_list = []
tmp_list = (re.sub(r"[^\d]", "", validator_list[i]))
for index in range(len(tmp_list)-4):
if(tmp_list[index] == tmp_list[index+1] and
tmp_list[index] == tmp_list[index+2] and
tmp_list[index] == tmp_list[index+3] ):
repeat_flag = 1
break;
if(validator_list[i][0] != '4' and
validator_list[i][0] != '5' and
validator_list[i][0] != '6' ):
credit_dict[validator_list[i]]='Invalid'
elif( len(re.sub(r"\D", "", validator_list[i])) != 16 ):
credit_dict[validator_list[i]]='Invalid'
elif(ch_num > 0):
credit_dict[validator_list[i]]='Invalid'
elif(
(re.search(r"-",validator_list[i])) and
( validator_list[i][4] != '-' or
validator_list[i][9] != '-'or
validator_list[i][14] != '-'
)
):
credit_dict[validator_list[i]]='Invalid'
elif(re.search(r"[^-\d]",validator_list[i])):
credit_dict[validator_list[i]]='Invalid'
elif(repeat_flag == 1 ):
credit_dict[validator_list[i]]='Invalid'
else :
credit_dict[validator_list[i]]='Valid'
space_str = ''
return credit_dict
def print_credit_card_summary(dict_o):
'''
Input: dict
Return:
printing summary of validation result - space between credit card and status is 40 width
example:
378282246310005 Invalid
30569309025904 Invalid
'''
space_str = ''
for key in dict_o:
new_str = key + dict_o[key]
if(dict_o[key] == "Valid"):
new_str_tmp = new_str[:-5]
space_lenth = 46-len(new_str)
for x in range(space_lenth):
space_str +=' '
print(new_str_tmp + space_str + 'Valid')
space_str=''
else :
new_str_tmp = new_str[:-7]
space_lenth = 48-len(new_str)
for x in range(space_lenth):
space_str +=' '
print(new_str_tmp + space_str + 'Invalid')
space_str=''
####### THE CODE BELOW IS FOR TESTING###################
############### DO NOT CHANGE #########################
import sys
if __name__ == '__main__':
# Take care of the console inputs
if len(sys.argv) <= 1:
sys.argv = ['', "anagram.txt", "credit_cards.txt"]
stars = '*' * 40
print(stars)
print("Testing Question 1 --- Anagrams?")
print(stars)
# testing reading_anagrams
try:
anagram = read_anagram(sys.argv[1])
if not anagram:
print("read_anagram() returns None.")
else:
print("anagram: ", anagram)
print()
except Exception as e:
print("Error (readnumbers()): ", e)
# testing anagram_validator
Anagrams = 0
NAnagrams = 0
try:
if not anagram: # Question 1 has not been implemented
print("anagram_validator() skipped....")
else:
result = anagram_validator(anagram)
if result == None:
print("anagram_validator() returns None.")
else:
for i in result:
if i == "Anagrams":
Anagrams += 1
elif i == "Not anagrams":
NAnagrams += 1
print("Number of valid Anagrams is {} and Not anagrams is {}.".format(Anagrams, NAnagrams))
except Exception as e:
print("Error (anagram_validator()):", e)
# testing Question 2
print("\n\n" + stars)
print("Testing Question 2 --- Credit Card Validator")
print(stars)
# Testing reading_credit_cards
try:
tup = read_credit_cards(sys.argv[2])
if not tup:
print("read_credit_cards() returns None.")
else:
print("The tuple of credit_cards: {}".format(tup))
except Exception as e:
print("Error (read_credit_cards()):", e)
# Testing credit_card_validator
vcc = 0
ivcc = 0
try:
if not tup: # Readin_Question 2 has not been implemented
print("credit_card_validator() skipped...")
else:
cc_dict = credit_card_validator(tup)
tmp_cc_dict = cc_dict
if not cc_dict:
print("credit_card_validator() returns None.")
else:
for items in cc_dict.keys():
if cc_dict[items] == "Valid":
vcc += 1
elif cc_dict[items] == "Invalid":
ivcc += 1
print("Number of valid credit cards is {} and invalid {}.".format(vcc, ivcc))
except Exception as e:
print("Error (credit_card_validator()):", e)
# testing Question 2
print("\n\n" + stars)
print("Testing Question 2b --- Print Credit Card Summary")
print(stars)
# Testing print_credit_card_summary
try:
if not tmp_cc_dict: # Dict credit card output has not been implemented
print("print_credit_card_summary() skipped...")
else:
import io # do not delete this line
from contextlib import redirect_stdout # do not delete this line
f = io.StringIO()
with redirect_stdout(f):
print_credit_card_summary(tmp_cc_dict)
out = f.getvalue()
if not out:
print("print_credit_card_summary() returns None.")
else:
count44 = 0
count46 = 0
for line in out.splitlines():
if len(line) - len(line.split()) == 44:
count44 += 1
elif len(line) - len(line.split()) == 46:
count46 += 1
if count44 == vcc and count46 == ivcc:
print("Your format looks good")
else:
print("You might have some issues in your summary format")
except Exception as e:
print("Error (print_credit_card_summary()):", e)
来源:https://blog.csdn.net/qq_30650153/article/details/84110948
猜你喜欢
- 如何将123456789转化成123,456,789这样的形式呢?很多流量大的站比如优酷都有这样的格式。也是设计程序最常用的算
- 前言其实就是个小问题,但是爆出来的时候也很莫名其妙。因为之前都跑得好好的,只是换了不同的文件去跑才出的问题,关键是不同的文件要处理的内容和格
- 体系结构 Microsoft按照客户/服务器体系结构的分布进行操作。这种方法产生不必要的代价和复杂性。在Internet中,Oracle已经
- 您在访问网站时是否会在有些页面上见到这种功能---您在可以访问此网站的同时,还可以查看您免费邮箱中是否有新邮件。这个功能是不是让您觉得很心动
- MYSQL数据库以它短小、方便、速度快、免费等优点成为很多网站目前首选数据库,但一般都是用PHP+MYSQL相结合来开发各种动态页面,其实A
- 决定数据类型的第一步是定义所存数数据的分类: 数值型, 字符串型还是临时型等;除了一些特别的并不是那么直观的外, 这通常是很直观的。接下来是
- 记一次在写cli脚本的时候,碰到的一个问题。问题自己是写服务端的,有时候会写一些cli脚本去跑测试。习惯main.go写主流程,其他子文件写
- 上一篇:微软建议的ASP性能优化28条守则(8)技巧 28:阅读资源链接下面是一些与性能有关的出色的资源链接。如果您想了解有关信息,请阅读
- 新搞了台linux云主机,瞎折腾折腾,先装个Python3。Linux环境下有其他软件需要Python2,如YUM,所以安装的Python3
- 前言在 Go 单元测试这个系列的第二部分 数据库的Mock测试 中我们介绍了用 go-sqlmock 给数据库的 CRUD 操作做Mock
- 最近,随着数据库的日益庞大,本来两个差不多的数据库,我开始发现我的数据库查询起来越来越比我朋友网站的慢了,经过初步对照,问题好像出在访问记录
- 我们可用下面的代码将服务器端变量转换为客户端的JavaScrit变量:<%@ Language=VBScript
- 如果你正在负责一个基于SQL Server的项目,或者你刚刚接触SQL Server,你都有可能要面临一些数据库性能的问题,这篇文章会为你提
- 作为一名前端,我们通常要做的就是让页面在各系统A-Grade浏览器,甚至网站浏览份额0.1%以上的浏览器上良好显示。当然,还有性能问题。不过
- 很有趣的招聘方式和题目:以下是该次招聘前端开发工程师的聘题解答:小贤是一条可爱的小狗(Dog),它的叫声很好听(wow),每次看到主人的时候
- 定义和用法fopen() 函数打开文件或者 URL。如果打开失败,本函数返回 FALSE。语法fopen(filename,mode,inc
- 当在php中使用mb_detect_encoding函数进行编码识别时,很多人都碰到过识别编码有误的问题,例如对与GB2312和UTF- 8
- 在MySQL数据库中导出整个数据库:1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldu
- 一、写在前面作为一名测试,有时候经常会遇到需要录屏记录自己操作,方便后续开发同学定位。以前都是用ScreenToGif来录屏制作成动态图,偶
- 这几天一直困惑我的问题,我觉得pycharm应该也是有的啊,偶然间找到了这个。把那个框里的选中,就可以了。ps:我的是 PyCharm 20