python使用正则筛选信用卡


Posted in Python onJanuary 27, 2019

本文实例为大家分享了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 ,他们分别长这样:

python使用正则筛选信用卡

python使用正则筛选信用卡

最后的程序长这样:

#! /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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python代码的打包与发布详解
Jul 30 Python
python基础教程之基本数据类型和变量声明介绍
Aug 29 Python
Python执行时间的计算方法小结
Mar 17 Python
python json.loads兼容单引号数据的方法
Dec 19 Python
Python : turtle色彩控制实例详解
Jan 19 Python
python各层级目录下import方法代码实例
Jan 20 Python
TensorFlow实现打印每一层的输出
Jan 21 Python
浅谈pytorch torch.backends.cudnn设置作用
Feb 20 Python
解决pyqt5异常退出无提示信息的问题
Apr 08 Python
python数据库批量插入数据的实现(executemany的使用)
Apr 30 Python
PyCharm 安装与使用配置教程(windows,mac通用)
May 12 Python
Pytorch 如何实现常用正则化
May 27 Python
pthon贪吃蛇游戏详细代码
Jan 27 #Python
只需7行Python代码玩转微信自动聊天
Jan 27 #Python
python实现贪吃蛇游戏
Mar 21 #Python
Python制作动态字符图的实例
Jan 27 #Python
python贪吃蛇游戏代码
Apr 18 #Python
DRF跨域后端解决之django-cors-headers的使用
Jan 27 #Python
在numpy矩阵中令小于0的元素改为0的实例
Jan 26 #Python
You might like
PHP的异常处理类Exception的使用及说明
2012/06/13 PHP
PHP输入输出流学习笔记
2015/05/12 PHP
php实现的pdo公共类定义与用法示例
2017/07/19 PHP
thinkPHP3.2使用RBAC实现权限管理的实现
2019/08/27 PHP
js中几种去掉字串左右空格的方法
2006/12/25 Javascript
关于删除时的提示处理(确定删除吗)
2013/11/03 Javascript
JavaScript闭包函数访问外部变量的方法
2014/08/27 Javascript
json格式数据的添加,删除及排序方法
2016/01/21 Javascript
12个非常实用的JavaScript小技巧【推荐】
2016/05/18 Javascript
浅析javaScript中的浅拷贝和深拷贝
2017/02/15 Javascript
Nodejs回调加超时限制两种实现方法
2017/06/09 NodeJs
bootstrap table服务端实现分页效果
2017/08/10 Javascript
node.js-v6新版安装具体步骤(分享)
2017/09/06 Javascript
jQuery实现的简单无刷新评论功能示例
2017/11/08 jQuery
vue+webpack中配置ESLint
2018/11/07 Javascript
JavaScript简单实现动态改变HTML内容的方法示例
2018/12/25 Javascript
微信小程序wx.navigateTo方法里的events参数使用详情及场景
2020/01/07 Javascript
JS实现网页烟花动画效果
2020/03/10 Javascript
vue移动端下拉刷新和上滑加载
2020/10/27 Javascript
JavaScript实现HTML导航栏下拉菜单
2020/11/25 Javascript
Python 专题三 字符串的基础知识
2017/03/19 Python
开源软件包和环境管理系统Anaconda的安装使用
2017/09/04 Python
python 从文件夹抽取图片另存的方法
2018/12/04 Python
Python3 sys.argv[ ]用法详解
2019/10/24 Python
浅析Python面向对象编程
2020/07/10 Python
Python 如何操作 SQLite 数据库
2020/08/17 Python
Html5 canvas实现粒子时钟的示例代码
2018/09/06 HTML / CSS
国际礼品店:GiftsnIdeas
2018/05/03 全球购物
就业推荐自我鉴定
2013/10/06 职场文书
硕士研究生求职自荐信范文
2014/03/11 职场文书
校园环保建议书
2014/05/14 职场文书
2014年基层党建工作总结
2014/11/11 职场文书
八年级地理课件资料及考点知识分享
2019/08/30 职场文书
Django模型层实现多表关系创建和多表操作
2021/07/21 Python
2021好看的国漫排行榜前十名 《完美世界》上榜,《元龙》排名第一
2022/03/18 国漫
JS精髓原型链继承及构造函数继承问题纠正
2022/06/16 Javascript