I'm having some issues with the psets 2 of cs50p, precisely I'm talking about the "Vanity Plates" problem, where I fulfilled all requests except one, which said:
“Numbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable … vanity plate; AAA22A would not be acceptable. The first number used cannot be a ‘0’.” Can you help me? Thank's
this is the code I wrote so far:
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if s.isalnum() | s[:2].isalpha() | 2 < len(s) < 6 | :
else:
return False
main()
you have to consider all the cases one by one, this is how I solved it:
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if len(s) < 2 or len(s) > 6:
return False
elif not s[0].isalpha() or not s[1].isalpha():
return False
elif checkFirstZero(s):
return False
elif checkMiddleZero(s):
return False
elif last(s):
return False
elif worng(s):
return False
return True
def last(s):
isAlp = False
isNum = False
for w in s:
if not w.isalpha():
isNum = True
else:
if isNum:
return True
return False
def checkCuntuNNumber(s):
isFirstTry = True
isNum = False
for w in s:
if not w.isalpha():
if isFirstTry:
isNum = True
isFirstTry = False
if isNum and s[-1].isalpha():
return True
def checkMiddleZero(s):
isFirstTry = True
isNum = False
for w in s:
if not w.isalpha():
if isFirstTry:
isNum = True
isFirstTry = False
if isNum and s[-1].isalpha():
return True
else:
return False
def checkFirstZero(s):
for w in s:
if not w.isalpha():
if int(w) == 0:
return True
else:
return False
def worng(s):
for w in s:
if w in [" ", ".", ","]:
return True
return False
main()
This is how I did it. I am sure there is an easier way to do it out there but hopefully this helps :)
characters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
numbers = ['1','2','3','4','5','6','7','8','9','0']
def main ():
plate = (input ("Plate: ")).upper()
if is_valid(plate):
print ('Valid')
else:
print ('Invalid')
def is_valid (s):
#Check whether length is between 2 and 6 included
if len(s) < 2 or len(s) > 6:
return False
elif char_check(s):
return False
elif char_start(s):
return False
elif zero_check(s):
return False
elif alpha_follow_check (s):
return False
else:
return True
#Check for valid characters
def char_check(s):
for i in s:
if not (i in characters or i in numbers):
return True
#Check whether first two are letters
def char_start (s):
for i in s[:2]:
if not i in characters:
return True
#Check if zero is first number listed
def zero_check (plate_response):
length_string = len (plate_response)
letter_position = 0
number_present = 0
zero_position = None
if any (i in numbers for i in plate_response):
for i in plate_response [0:length_string]:
if i == '0':
zero_position = letter_position
break
letter_position = letter_position + 1
for i in plate_response [0:zero_position]:
if i in numbers:
number_present = 1
if number_present == 0:
return True
else:
return False
#Check alphabet follows numbers
def alpha_follow_check (plate_response):
length_string = len (plate_response)
letter_position = 0
number_position = None
if any (i in numbers for i in plate_response):
for i in plate_response [0:length_string]:
if i in numbers:
number_position = letter_position
break
letter_position = letter_position + 1
for i in plate_response [number_position:length_string]:
if i in characters:
return True
else:
return False
main ()
idk if will help, but the part that i've had the most difficulty in this problem was: "Numbers cannot be used in the middle of a plate; they must come at the end, AAA22A would not be acceptable", then i learned that you can create a full list from the plate that the user inputed, and how to actually use it, with the:
ls = list(s)
for i in range(len(ls)):
After that, we check when the first number appears. "if == '0'" ,then returns False to the function.
After that, if the first number isn't a 0, the program checks if the next item in that list is letter, and, if it is, also return False.
i < len(ls) -1 => this part guarantee that the program will not run in the last item of the list
ls[i+1].isalpha() => and this part check that, if the item on the list was a number, and then the next item is a letter, it returns False
I hope it helps someone, i've spend a lot of time trying to figure it out what to do, and then reached this solution: "for i in range(len(ls))".
Now my code is complete and working.
My code:
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if not s.isalnum():
return False
elif len(s) < 4 or len(s) > 7:
return False
elif s[0].isdigit()or s[1].isdigit():
return False
elif s[-1].isalpha() or s[-2].isalpha():
return False
else:
ls = list(s)
for i in range(len(ls)):
if ls[i].isdigit():
if ls[i] == '0':
return False
elif i < len(ls) -1 and ls[i+1].isalpha():
return False
else:
return True
main()
I'm trying to check if a string is palindrome in python using deque. But the code below just checks a string with no space, how can I adjust it to a code which handles strings with spaces as well? e.g: It only works if I write the input as "BORROWORROB" but not when it's "BORROW OR ROB"
from pythonds.basic import Deque
def isPalindrome(word):
if word is None:
return False
if len(word) <= 1:
return True
DQ = Deque()
for w in word:
DQ.addRear(w)
while (DQ.size() > 1):
front = DQ.removeFront()
rear = DQ.removeRear()
if front != rear:
return False
return True
def readInput():
inp = input("Enter string: ")
return inp
word = readInput()
print ("Is \"{}\" a palindrome: {}".format(word, isPalindrome(word)))
You have to remove white spaces before starting the logic of the function:
from pythonds.basic import Deque
def isPalindrome(word):
word = word.replace(" ", "")
if word is None:
return False
if len(word) <= 1:
return True
DQ = Deque()
for w in word:
DQ.addRear(w)
while (DQ.size() > 1):
front = DQ.removeFront()
rear = DQ.removeRear()
if front != rear:
return False
return True
def readInput():
inp = input("Enter string: ")
return inp
word = readInput()
print ("Is \"{}\" a palindrome: {}".format(word, isPalindrome(word)))
I am doing a task in class about a password guesser. I stumbled into a lot of problems trying to solve this task, my first approach was to use for loops (code below), but I realized that the amount of 'for loops' is equal to the length of the string.
a_z = 'abcdefghijklmnopqrstuvwxyz'
pasw = 'dog'
tests = 0
guess = ''
azlen = len(a_z)
for i in range(azlen):
for j in range(azlen):
for k in range(azlen):
guess = a_z[i] + a_z[j] + a_z[k]
tests += 1
if guess == pasw:
print('Got "{}" after {} tests'.format(guess, str(tests)))
break
input()
The program above is very concrete. It only works if there are exactly 3 characters entered. I read that you could use a package called intertools, however, I really want to find another way of doing this. I thought about using recursion but don't even know where to start.
import string
import itertools
for possible_password in itertools.permutations(string.ascii_letters, 3):
print(possible_password)
If you don't want to use itertools you can certainly do this with recursion, which will work with passwords of any (reasonable) length—it's not wired to three characters. Basically, each recursive call will attempt to append a new character from your alphabet to your running value of guess. The base case is when the guess attains the same length as value you're seeking, in which case you check for a match. If a match is found, return an indication that you have succeeded (I used return True) so you can short circuit any further searching. Otherwise, return a failure indication (return False). The use of a global counter makes it a bit uglier, but produces the same results you reported.
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def brute_force_guesser(passwd, guess = ''):
global _bfg_counter
if len(guess) == 0:
_bfg_counter = 0
if len(guess) == len(passwd):
_bfg_counter += 1
if guess == passwd:
print('Got "{}" after {} tests'.format(guess, str(_bfg_counter)))
return True
return False
else:
for c in ALPHABET:
if brute_force_guesser(passwd, guess + c):
return True
return False
brute_force_guesser('dog') # => Got "dog" after 2399 tests
brute_force_guesser('doggy') # => Got "doggy" after 1621229 tests
One way to avoid the global counter is by using multiple return values:
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def brute_force_guesser(target, guess = '', counter = 0):
if len(guess) == len(target):
counter += 1
if guess == target:
print('Got "{}" after {} tests'.format(guess, str(counter)))
return True, counter
return False, counter
else:
for c in ALPHABET:
target_found, counter = brute_force_guesser(target, guess + c, counter)
if target_found:
return True, counter
return False, counter
brute_force_guesser('dog') # => Got "dog" after 2399 tests
brute_force_guesser('doggy') # => Got "doggy" after 1621229 tests
Here is my full answer, sorry if it's not neat, I'm still new to coding in general. The credit goes to #JohnColeman for the great idea of using bases.
import math
global guess
pasw = str(input('Input password: '))
chars = 'abcdefghijklmnopqrstuvwxyz' #only limeted myself to lowercase for simplllicity.
base = len(chars)+1
def cracker(pasw):
guess = ''
tests = 1
c = 0
m = 0
while True:
y = tests
while True:
c = y % base
m = math.floor((y - c) / base)
y = m
guess = chars[(c - 1)] + guess
print(guess)
if m == 0:
break
if guess == pasw:
print('Got "{}" after {} tests'.format(guess, str(tests)))
break
else:
tests += 1
guess = ''
cracker(pasw)
input()
import itertools
import string
def guess_password(real):
chars = string.ascii_lowercase + string.digits
attempts = 0
for password_length in range(1, 20):
for guess in itertools.product(chars, repeat=password_length):
attempts += 1
guess = ''.join(guess)
if guess == real:
return 'the password is {}, found in {} guesses.'.format(guess, attempts)
print(guess, attempts)
print(guess_password('abc'))
I have a binary search to search a list of emails from a user input. However when the user inputted email isnt found in the list I want the user to be able to enter another time. However I dont know how to return it to the start of the while loop again.
Here is my Code:
def BubbleSort(logindata):
NoSwaps = 1
N = len(logindata)
logindata = list(logindata)
while NoSwaps == 1:
Count = 1
NoSwaps = 0
for Count in range(N-1):
if logindata[Count] > logindata[Count+1]:
temp = logindata[Count]
logindata[Count] = logindata[Count+1]
logindata[Count+1]=temp
NoSwaps=1
return tuple(logindata)
def BinarySearch(logindata,email):
First=0
Last=len(logindata)-1
ItemFound = False
SearchFailed = False
while ItemFound == False or SearchFailed == False:
Midpoint = (First + Last) // 2
if logindata[Midpoint][0] == email:
ItemFound = True
print("Email Found")
break
elif logindata[Midpoint][0] > email:
Last = Midpoint - 1
print("Not Found")
else:
First = Midpoint + 1
print("Not Found")
return False
if __name__ == "__main__":
logindata=["tom#gmail.com","Password1"],["harry#gmail.com","Password2"],["jake#gmail.com","Password3"]
logindata=BubbleSort(logindata)
print(logindata)
email=input("Enter username")
BinarySearch(logindata,email)
Just add the part you need to repeat in another while loop:
email=input("Enter username")
BinarySearch(logindata,email)
to:
while True:
email=input("Enter username")
res = BinarySearch(logindata,email)
if res:
break
print("Done")
and work with the return value of your BinarySearch function.
You also need to change the implementation of BinarySearch to return the appropriate values. Also, your condition is faulty, binary searching is ends when First <= Last, you can drop the other flags you're using:
def BinarySearch(logindata,email):
First=0
Last=len(logindata)-1
while First <= Last:
Midpoint = (First + Last) // 2
if logindata[Midpoint][0] == email:
print("Email Found")
return True
elif logindata[Midpoint][0] > email:
Last = Midpoint - 1
else:
First = Midpoint + 1
print("Not found")
return False
You shouldn't be printing Not Found whenever one of the else clause is executed, it hasn't been found yet since the search hasn't rerminated. Print that out in the end, after the while loop of BinarySearch has been exited.
So I have to create code that validate whether a password:
Is at least 8 characters long
Contains at least 1 number
Contains at least 1 capital letter
Here is the code:
def validate():
while True:
password = input("Enter a password: ")
if len(password) < 8:
print("Make sure your password is at lest 8 letters")
elif not password.isdigit():
print("Make sure your password has a number in it")
elif not password.isupper():
print("Make sure your password has a capital letter in it")
else:
print("Your password seems fine")
break
validate()
I'm not sure what is wrong, but when I enter a password that has a number - it keeps telling me that I need a password with a number in it. Any solutions?
You can use re module for regular expressions.
With it your code would look like this:
import re
def validate():
while True:
password = raw_input("Enter a password: ")
if len(password) < 8:
print("Make sure your password is at lest 8 letters")
elif re.search('[0-9]',password) is None:
print("Make sure your password has a number in it")
elif re.search('[A-Z]',password) is None:
print("Make sure your password has a capital letter in it")
else:
print("Your password seems fine")
break
validate()
r_p = re.compile('^(?=\S{6,20}$)(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[^A-Za-z\s0-9])')
this code will validate your password with :
min length is 6 and max length is 20
at least include a digit number,
at least a upcase and a lowcase letter
at least a special characters
password.isdigit() does not check if the password contains a digit, it checks all the characters according to:
str.isdigit(): Return true if all characters in the string are digits
and there is at least one character, false otherwise.
password.isupper() does not check if the password has a capital in it, it checks all the characters according to:
str.isupper(): Return true if all cased characters in the string are
uppercase and there is at least one cased character, false otherwise.
For a solution, please check the question and accepted answer at check if a string contains a number.
You can build your own hasNumbers()-function (Copied from linked question):
def hasNumbers(inputString):
return any(char.isdigit() for char in inputString)
and a hasUpper()-function:
def hasUpper(inputString):
return any(char.isupper() for char in inputString)
Example:
class Password:
def __init__(self, password):
self.password = password
def validate(self):
vals = {
'Password must contain an uppercase letter.': lambda s: any(x.isupper() for x in s),
'Password must contain a lowercase letter.': lambda s: any(x.islower() for x in s),
'Password must contain a digit.': lambda s: any(x.isdigit() for x in s),
'Password must be at least 8 characters.': lambda s: len(s) >= 8,
'Password cannot contain white spaces.': lambda s: not any(x.isspace() for x in s)
}
valid = True
for n, val in vals.items():
if not val(self.password):
valid = False
return n
return valid
def compare(self, password2):
if self.password == password2:
return True
if __name__ == '__main__':
input_password = input('Insert Password: ')
input_password2 = input('Repeat Password: ')
p = Password(input_password)
if p.validate() is True:
if p.compare(input_password2) is True:
print('OK')
else:
print(p.validate())
You are checking isdigit and isupper methods on the entire password string object not on each character of the string. The following is a function which checks if the password meets your specific requirements. It does not use any regex stuff. It also prints all the defects of the entered password.
#!/usr/bin/python3
def passwd_check(passwd):
"""Check if the password is valid.
This function checks the following conditions
if its length is greater than 6 and less than 8
if it has at least one uppercase letter
if it has at least one lowercase letter
if it has at least one numeral
if it has any of the required special symbols
"""
SpecialSym=['$','#','#']
return_val=True
if len(passwd) < 6:
print('the length of password should be at least 6 char long')
return_val=False
if len(passwd) > 8:
print('the length of password should be not be greater than 8')
return_val=False
if not any(char.isdigit() for char in passwd):
print('the password should have at least one numeral')
return_val=False
if not any(char.isupper() for char in passwd):
print('the password should have at least one uppercase letter')
return_val=False
if not any(char.islower() for char in passwd):
print('the password should have at least one lowercase letter')
return_val=False
if not any(char in SpecialSym for char in passwd):
print('the password should have at least one of the symbols $##')
return_val=False
if return_val:
print('Ok')
return return_val
print(passwd_check.__doc__)
passwd = input('enter the password : ')
print(passwd_check(passwd))
''' Minimum length is 5;
- Maximum length is 10;
- Should contain at least one number;
- Should contain at least one special character (such as &, +, #, $, #, %, etc.);
- Should not contain spaces.
'''
import string
def checkPassword(inputStr):
if not len(inputStr):
print("Empty string was entered!")
exit(0)
else:
print("Input:","\"",inputStr,"\"")
if len(inputStr) < 5 or len(inputStr) > 10:
return False
countLetters = 0
countDigits = 0
countSpec = 0
countWS = 0
for i in inputStr:
if i in string.ascii_uppercase or i in string.ascii_lowercase:
countLetters += 1
if i in string.digits:
countDigits += 1
if i in string.punctuation:
countSpec += 1
if i in string.whitespace:
countWS += 1
if not countLetters:
return False
elif not countDigits:
return False
elif not countSpec:
return False
elif countWS:
return False
else:
return True
print("Output: ",checkPassword(input()))
With Regex
s = input("INPUT: ")
print("{}\n{}".format(s, 5 <= len(s) <= 10 and any(l in "0123456789" for l in s) and any(l in "!\"#$%&'()*+,-./:;<=>?#[\]^_`{|}~" for l in s) and not " " in s))
Module import
from string import digits, punctuation
def validate_password(p):
if not 5 <= len(p) <= 10:
return False
if not any(c in digits for c in p):
return False
if not any(c in punctuation for c in p):
return False
if ' ' in p:
return False
return True
for p in ('DJjkdklkl', 'John Doe'
, '$kldfjfd9'):
print(p, ': ', ('invalid', 'valid')[validate_password(p)], sep='')
Sure with regex there are easier answers, but this one of the simplest ways
from string import punctuation as p
s = 'Vishwasrocks#23' #or user input is welcome
lis = [0, 0, 0, 0]
for i in s:
if i.isupper():
lis[0] = 1
elif i.islower():
lis[1] = 1
elif i in p:
lis[2] = 1
elif i.isdigit():
lis[3] = 1
print('Valid') if 0 not in lis and len(s) > 8 else print('Invalid')
The simplest python validation using normal methods
password = '-'
while True:
password = input(' enter the passwword : ')
lenght = len(password)
while lenght < 6:
password = input('invalid , so type again : ')
if len(password)>6:
break
while not any(ele.isnumeric() for ele in password):
password = input('invalid , so type again : ')
while not any(ele.isupper() for ele in password):
password = input('invalid , so type again : ')
while not any(ele not in "[#_!#$%^&*()<>?/|}{~:]" for ele in password):
password = input('invalid , so type again : ')
break
isdigit() checks the whole string is a digit, not if the string contains a digit
Return true if all characters in the string are digits and there is at least one character, false otherwise.
isupper() checks the whole string is in uppercase, not if the string contains at least one uppercase character.
Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
What you need is using the any built-in function:
any([x.isdigit() for x in password]) will return True if at least one digit is present in password
any([x.isupper() for x in password]) will return True if at least one character is considered as uppercase.
Maybe you can use regex expression:
re.search(r"[A-Z]", password)
Check uppercase letters.
re.search(r"[0-9]", password)
Check digits in password.
Python 2.7
The for loop will assign a condition number for each character. i.e. Pa$$w0rd in a list would = 1,2,4,4,2,3,2,2,5. Since sets only contains unique values, the set would = 1,2,3,4,5; therefore since all conditions are met the len of the set would = 5. if it was pa$$w the set would = 2,4 and len would = 2 therefore invalid
name = raw_input("Enter a Password: ")
list_pass=set()
special_char=['#','$','#']
for i in name:
if(i.isupper()):
list_pass.add('1')
elif (i.islower()):
list_pass.add('2')
elif(i.isdigit()) :
list_pass.add('3')
elif(i in special_char):
list_pass.add('4')
if len(name) >=6 and len(name) <=12:
list_pass.add('5')
if len(list_pass) is 5:
print ("Password valid")
else: print("Password invalid")
uppercase_letter = ['A', 'B','C', 'D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z']
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
import re
info ={}
while True:
user_name = input('write your username: ')
if len(user_name) > 15:
print('username is too long(must be less than 16 character)')
elif len(user_name) < 3 :
print('username is short(must be more than 2 character)')
else:
print('your username is', user_name)
break
while True:
password= input('write your password: ')
if len(password) < 8 :
print('password is short(must be more than 7 character)')
elif len(password) > 20:
print('password is too long(must be less than 21 character)')
elif re.search(str(uppercase_letter), password ) is None :
print('Make sure your password has at least one uppercase letter in it')
elif re.search(str(number), password) is None :
print('Make sure your password has at least number in it')
else:
print('your password is', password)
break
info['user name'] = user_name
info['password'] = password
print(info)
Password Complexity conditions:
Must include at least one uppercase character
Must include at least one lowercase character
Must include at least one number
Must include at least one special character
Must have a length of at least 8 and a max of 20
import re
def validate():
while True:
password = raw_input("Enter a password: ")
re_exp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$!%*#?&])[A-Za-z\d#$!#%*?&]{8,20}$"
if re.search(re.compile(regex_exp),password):
print("Your password seems fine")
else:
print("Password doees not matches with password complexity conditions")
break
Or you can use this to check if it got at least one digit:
min(passwd).isdigit()