This is a simple python validation routine for (American) phone numbers. Inputs from a user-specified file are validated according to whitelisting rules that I came up with (see comments), and valid inputs are written to a file. This was a good exercise with regex.
#validation method: whitelisting.
#formula for valid phone number:
# a. "1" (optional)
# b. " " or "-" or "." (optional)
# c. open parenthesis (optional)
# d. 3-digit area code
# e. close parenthesis (optional)
# f. " " or "-" or "." (optional)
# g. 3-digit exchange
# h. " " or "-" or "." (optional)
# i. 4-digit extension
import re
import sys
import os
valid = re.compile('^1?[-. ]?[(]?[0-9]{3}[)]?[-. ]?[0-9]{3}[- .]?[0-9]{4}$')
class Validate:
def __init__(self):
self.inputfile_name = None
self.inputfile = ""
self.result = None
self.output_file = open(os.path.join(os.getcwd(), "phoneoutput.txt"), 'w')
def load_input_file(self, inputfile_name):
self.inputfile_name = inputfile_name
if os.path.exists(self.inputfile_name):
self.inputfile = open(self.inputfile_name).readlines()
else:
print "File does not exist."
return
def validate(self):
for line in self.inputfile:
self.result = valid.match(line)
if self.result:
self.output_file.write("%s\n" % line)
else:
pass
self.output_file.close()
def execute():
engine = Validate()
if len(sys.argv) != 2:
print "Usage: 'phonevalidate_file.py [filename]'"
exit
if len(sys.argv) == 2:
engine.load_input_file(sys.argv[1])
engine.validate()
execute()
Explorations in Simple Programming
Tuesday, January 29, 2013
Tuesday, January 22, 2013
Fizzbizz
I hear this is a classic interview question. It demonstrates basic competency with 'for' loops. Shown here in Python.
#fizzbizz
#print all #s 1-100
#if a number is a multiple of 3, sub fizz
#if num is multiple of 5, sub bizz
#if multiple of both, print fizzbizz
for i in range(1,101):
if i%3 == 0:
print "fizz",
if i%5 == 0:
print "bizz",
if i%3 and i%5:
print i,
print ""
#fizzbizz
#print all #s 1-100
#if a number is a multiple of 3, sub fizz
#if num is multiple of 5, sub bizz
#if multiple of both, print fizzbizz
for i in range(1,101):
if i%3 == 0:
print "fizz",
if i%5 == 0:
print "bizz",
if i%3 and i%5:
print i,
print ""
Wednesday, January 16, 2013
Numpalindrome
This is a nice little bash script that takes a user-inputted number and checks to see if it is a palindrome. I like the logic, which is very simple but elegant. This isn't a practical script, but it was a useful exercise.
#!/bin/bash
#determines if an input number is a palindrome
read -p "please enter a number: " num
orignum=$num
while [ $num -gt 0 ]
do
digit=$(( num%10 ))
num=$(( num/10 ))
rev=$(( digit + rev*10 ))
done
echo "your number: $orignum"
echo "the reversed number: $rev"
[[ $rev == $orignum ]] && echo "your number is a palindrome." || echo "your number is not a palindrome."
#!/bin/bash
#determines if an input number is a palindrome
read -p "please enter a number: " num
orignum=$num
while [ $num -gt 0 ]
do
digit=$(( num%10 ))
num=$(( num/10 ))
rev=$(( digit + rev*10 ))
done
echo "your number: $orignum"
echo "the reversed number: $rev"
[[ $rev == $orignum ]] && echo "your number is a palindrome." || echo "your number is not a palindrome."
Time to Explore
As I've been learning some simple programming techniques, I've decided that it could be useful to share some of the lessons I've learned and programs that I've created. It gives me a way to keep track of things, reflect on my progress, and hopefully even share some things that can be helpful to those who find this blog. Enjoy!
Subscribe to:
Comments (Atom)