Advertisement
Guest User

Code

a guest
Dec 3rd, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. __author__ = 'Zoap'
  2.  
  3. import math
  4.  
  5. def output_style(text):
  6.  
  7.     print("\n" + text)
  8.  
  9. def info():
  10.  
  11.     text = "This program allows you to search for the number of possible combinations of a pin number of length x, " \
  12.         "given that you know one of the digits"
  13.  
  14.     output_style(text)
  15.  
  16. def search():
  17.  
  18.     loop = True
  19.  
  20.     while loop is True:
  21.  
  22.         text = "Input number to search for: "
  23.         output_style(text)
  24.         search_x = str(input())
  25.  
  26.         if search_x.isdigit():
  27.             loop = False
  28.  
  29.         else:
  30.             text ="Number to search for must be a number!"
  31.             output_style(text)
  32.  
  33.     return search_x
  34.  
  35. def pin_length(search_x):
  36.  
  37.     loop = True
  38.  
  39.     while loop is True:
  40.  
  41.         text = "Input length of pin code: "
  42.         output_style(text)
  43.         pin = input()
  44.  
  45.         if pin.isdigit() is False:
  46.             text = "Length must be a number!"
  47.             output_style(text)
  48.  
  49.         elif int(pin) > 7:
  50.             text = "Length can not be more than 7 digits!"
  51.             output_style(text)
  52.  
  53.         elif search_x.isdigit() <= 0:
  54.             text = "Length can not be equal to, or less than zero!"
  55.             output_style(text)
  56.  
  57.         else:
  58.             loop = False
  59.  
  60.     return pin
  61.  
  62. def check_stop(search_x, pin):
  63.  
  64.     loop = True
  65.  
  66.     while loop is True:
  67.  
  68.         stop = int((math.pow(10, int(pin))))
  69.  
  70.         if stop < int(search_x):
  71.             text = "Length of pin makes the number to search for bigger than the possible number of variations!"
  72.             output_style(text)
  73.             search()
  74.  
  75.         else:
  76.             loop = False
  77.  
  78.     return stop
  79.  
  80. def calc_forloop(search_x, stop):
  81.  
  82.     number_list = []
  83.  
  84.     # For every integer in range add int to list and convert to string
  85.     for i in range(0, (stop + 1)):
  86.         number_list.append(str(i))
  87.  
  88.     #For every str in list containing x, count x
  89.     x_numbers = [a for a in number_list if search_x in a]
  90.  
  91.     # Percent of total numbers/digits in range containing digit x
  92.     percent = len(x_numbers) / (stop / 100)
  93.  
  94.     print_total = "That is a total of " + str("%.2f" % percent) + "%" " of all numbers between 0 and " + str(stop)
  95.  
  96.     if int(search_x) >= 10:
  97.         text ="There are a total of " + str(len(x_numbers)) +\
  98.                 " numbers between 0 and " + str(stop) + " containing the number " + search_x
  99.         output_style(text)
  100.  
  101.     elif int(len(x_numbers)) is 1:
  102.         text = "There are a total of " + str(len(x_numbers)) +\
  103.                  " number between 0 and " + str(stop) + " containing the digit " + search_x
  104.         output_style(text)
  105.  
  106.     else:
  107.         text = "There are a total of " + str(len(x_numbers)) +\
  108.                  " numbers between 0 and " + str(stop) + " containing the digit " + search_x
  109.         output_style(text)
  110.     text = print_total
  111.     output_style(text)
  112.  
  113. def run_again():
  114.  
  115.     loop = None
  116.  
  117.     while loop is None:
  118.  
  119.         text = "Run again? y/n: "
  120.         output_style(text)
  121.  
  122.         choice = input()
  123.         if choice in ["y", "Y"]:
  124.             loop = True
  125.             running = True
  126.  
  127.         elif choice in ["n", "N"]:
  128.             loop = False
  129.             running = False
  130.  
  131.  
  132.         else:
  133.             text = "Invalid input"
  134.             output_style(text)
  135.  
  136.     return running
  137.  
  138. def main():
  139.  
  140.     running = True
  141.  
  142.     while running is True:
  143.         info()
  144.         search_x = search()
  145.         pin = pin_length(search_x)
  146.         stop = check_stop(search_x, pin)
  147.         calc_forloop(search_x, stop)
  148.         running = run_again()
  149. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement