Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'Zoap'
- import math
- def output_style(text):
- print("\n" + text)
- def info():
- text = "This program allows you to search for the number of possible combinations of a pin number of length x, " \
- "given that you know one of the digits"
- output_style(text)
- def search():
- loop = True
- while loop is True:
- text = "Input number to search for: "
- output_style(text)
- search_x = str(input())
- if search_x.isdigit():
- loop = False
- else:
- text ="Number to search for must be a number!"
- output_style(text)
- return search_x
- def pin_length(search_x):
- loop = True
- while loop is True:
- text = "Input length of pin code: "
- output_style(text)
- pin = input()
- if pin.isdigit() is False:
- text = "Length must be a number!"
- output_style(text)
- elif int(pin) > 7:
- text = "Length can not be more than 7 digits!"
- output_style(text)
- elif search_x.isdigit() <= 0:
- text = "Length can not be equal to, or less than zero!"
- output_style(text)
- else:
- loop = False
- return pin
- def check_stop(search_x, pin):
- loop = True
- while loop is True:
- stop = int((math.pow(10, int(pin))))
- if stop < int(search_x):
- text = "Length of pin makes the number to search for bigger than the possible number of variations!"
- output_style(text)
- search()
- else:
- loop = False
- return stop
- def calc_forloop(search_x, stop):
- number_list = []
- # For every integer in range add int to list and convert to string
- for i in range(0, (stop + 1)):
- number_list.append(str(i))
- #For every str in list containing x, count x
- x_numbers = [a for a in number_list if search_x in a]
- # Percent of total numbers/digits in range containing digit x
- percent = len(x_numbers) / (stop / 100)
- print_total = "That is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " + str(stop)
- if int(search_x) >= 10:
- text ="There are a total of " + str(len(x_numbers)) +\
- " numbers between 0 and " + str(stop) + " containing the number " + search_x
- output_style(text)
- elif int(len(x_numbers)) is 1:
- text = "There are a total of " + str(len(x_numbers)) +\
- " number between 0 and " + str(stop) + " containing the digit " + search_x
- output_style(text)
- else:
- text = "There are a total of " + str(len(x_numbers)) +\
- " numbers between 0 and " + str(stop) + " containing the digit " + search_x
- output_style(text)
- text = print_total
- output_style(text)
- def run_again():
- loop = None
- while loop is None:
- text = "Run again? y/n: "
- output_style(text)
- choice = input()
- if choice in ["y", "Y"]:
- loop = True
- running = True
- elif choice in ["n", "N"]:
- loop = False
- running = False
- else:
- text = "Invalid input"
- output_style(text)
- return running
- def main():
- running = True
- while running is True:
- info()
- search_x = search()
- pin = pin_length(search_x)
- stop = check_stop(search_x, pin)
- calc_forloop(search_x, stop)
- running = run_again()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement