def _ask(threshold, matcher, facts): """ Interactive option to ask a question. :type threshold: float :type matcher: app.match.Matcher :type facts: list of app.types.fact.Fact """ io.reply('What do you want to ask me?') while True: message = io.prompt_without_parse('ask') response = ask(threshold, matcher, facts, message) type = response['type'] if type == 'invalid': io.reply(response['validation_mesage']) elif type == 'no_match': # TODO: how to give detailed information on why no match was found? io.reply( 'Sorry. My digital brain is not yet fully evolved. I did not understand it.' ) io.reply( 'Could you rephrase the question, so I might be able to understand it?' ) return elif type == 'select_match': _select_match(response['matches']) else: io.reply(response['answer']) return
def analytics_cycle(args): while True: if analytics_func(args): print(analytics_func(args)) args.envelope_a = args.envelope_b = args.envelope_c = args.envelope_d = 'default' else: args.envelope_a = input( 'Enter a-side of the first envelope (number or float): ') args.envelope_b = input( 'Enter b-side of the first envelope (number or float): ') args.envelope_c = input( 'Enter a-side of the second envelope (number or float): ') args.envelope_d = input( 'Enter b-side of the second envelope (number or float): ') if analytics_func(args): print(analytics_func(args)) args.envelope_a = args.envelope_b = args.envelope_c = args.envelope_d = 'default' else: print('Parameters must be (number or float): ') continue if ask(): continue else: break
def numeric_sequence(args): def sequence(number): lst = [] for i in range(int(math.sqrt(int(number)) + 1)): if i * i >= int(number): break else: lst.append(str(i)) return print(', '.join(lst)) def _check_args(): if args.n is not None and args.n.isdigit() and args.n != 'default': return True else: return False while True: if _check_args(): sequence(args.n) args.n = 'default' else: args.n = input('Set n: ').lower() if _check_args(): sequence(args.n) else: print( 'Enter the correct parameters. N - should be positive number' ) continue if ask(): continue else: break
def chess(args): def board(height, width): Chess_board = [(' ', '*'), ('*', ' ')] for _ in range(int(height)): print(*Chess_board[1] * int(width)) print(*Chess_board[0] * int(width)) def _check_args(): if args.width is not None and args.height is not None and args.width.isdigit() and args.height.isdigit() and \ int(args.width) % 2 != 1 and int(args.height) % 2 != 1 and args.width != 'default' and args.height != 'default': return True else: return False while True: if _check_args(): board(str(int(int(args.height) / 2)), str(int(int(args.width) / 2))) args.height = 'default' else: args.width = input('Enter a numeric value of the width of the chessboard: ') args.height = input('Enter a numeric value of the height of the chessboard: ') if _check_args(): board(str(int(int(args.height) / 2)), str(int(int(args.width) / 2))) args.height = 'default' else: print('Enter the correct parameters. Width and height should be even positive number') continue if ask(): continue else: break
def _main(): config = _parse_config() thresholds = config['threholds'] memory = PersistentMemory(config['memory_path']) nlp = get_language() matcher = Matcher(nlp) print(json.dumps({'type': 'finished_loading'})) while True: user_input = sys.stdin.readline() request = json.loads(user_input) message_type = request['type'] if message_type == 'exit': break elif message_type == 'is_loading': response = {'type': 'finished_loading'} elif message_type == 'ask': response = ask(thresholds['question'], matcher, memory.facts, request['message']) elif message_type == 'ask_select_match': response = _ask_select_match(memory.facts, request['match']) elif message_type == 'list_facts': response = _list_facts(memory.facts) elif message_type == 'tell': response = tell(thresholds['fact'], matcher, memory, request['message']) else: raise Exception(f'Unknown message type: {message_type}') print(json.dumps(response)) sys.stdout.flush()
def digit_to_string(args): def numbers(number): FIRST_TEN = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" ] SECOND_TEN = [ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ] OTHER_TENS = [ "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ] HUNDRED = "hundred" n = number // 100 t = [FIRST_TEN[n - 1], HUNDRED] if n > 0 else [] n = (number // 10) % 10 t += [OTHER_TENS[n - 2]] if n > 1 else [] n = number % (10 if n > 1 else 20) t += [(FIRST_TEN + SECOND_TEN)[n - 1]] if n > 0 else [] return print(' '.join(t)) def _check_args(): if args.num is not None and args.num.isdigit( ) and args.num != 'default' and int(args.num) < 1000: return True else: return False while True: if _check_args(): numbers(int(args.num)) args.num = 'default' else: args.num = input('Set your number: ').lower() if _check_args(): numbers(int(args.num)) args.num = 'default' else: print( 'Enter the correct parameters. N - should be positive number and less than 1000' ) continue if ask(): continue else: break
def fibo_range(args): def fibonacci(num): a, b = 0, 1 for i in range(num): a, b = b, a + b if a < num: yield a else: break def count_fib(first, second): lst = [] for fib in fibonacci(int(second)): if fib >= int(first): lst.append(fib) else: continue return print(lst) def _check_args(): if args.arg_1 is not None and args.arg_2 is not None and args.arg_1.isdigit() and args.arg_2.isdigit() \ and args.arg_1 != 'default' and args.arg_2 != 'default': return True else: return False while True: if _check_args(): count_fib(args.arg_1, args.arg_2) args.arg_1 = args.arg_2 = 'default' else: args.arg_1 = input('Set start: ') args.arg_2 = input('Set end: ') if _check_args(): count_fib(args.arg_1, args.arg_2) args.arg_1 = args.arg_2 = 'default' else: print( 'Enter the correct parameters. Should be positive number') continue if ask(): continue else: break
def tickets(): def moscow(): count = 0 for ticket in itertools.product(range(0, 10), repeat=6): if sum(ticket[:3]) == sum(ticket[3:]): count += 1 return print(count) def saint_petersburg(): count = 0 for ticket in itertools.product(range(0, 10), repeat=6): even = [] odd = [] for numbers in range(len(ticket)): if numbers % 2 == 0: even.append(ticket[numbers]) else: odd.append(ticket[numbers]) if sum(even) == sum(odd): count += 1 return print(count) while True: try: file_name = input('<File name.format>: ') BASE_DIR = os.path.join(os.getcwd(), file_name) with open(BASE_DIR) as file: word = file.read().lower() if 'moscow' in word: moscow() elif 'piter' in word: saint_petersburg() else: print('ERROR') except IndexError: print('Enter the correct parameters <File-name.format>:') continue if ask(): continue else: break
def check_files(args): while True: if check_args(args): print(files_func(args)) args.mod = 'def' else: mod = input('Which mod do you want to choose? (type C to count / type R to replace ' '/ type anything else to exit): ').lower() if mod == 'c': direction_count = input('<direction of your file>, <counted string>: ').replace(' ', '').split(',') args = argparse.Namespace(files_name=direction_count[0], mod=mod, string_for_count=direction_count[1], string_for_search='def', string_for_replace='def') if len(direction_count) > 2: print('Enter the correct parameters') args.string_for_count = 'def' continue print(files_func(args)) args.string_for_count = 'def' elif mod == 'r': direction_replace = input('<direction of your file>, <search string>, <replace string>: ')\ .replace(' ', '').split(',') if len(direction_replace) > 3: print('Enter the correct parameters') args.string_for_search = args.string_for_replace = 'def' continue args = argparse.Namespace(files_name=direction_replace[0], string_for_search=direction_replace[1], string_for_replace=direction_replace[2], mod=mod, string_for_count='def') print(files_func(args)) args.string_for_search = args.string_for_replace = 'def' else: break if ask(): continue else: break
def geron_triangle(args): def triangle(a, b, c): p = (float(a) + float(b) + float(c)) / 2 return sqrt( float(p) * (float(p) - float(a)) * (float(p) - float(b)) * (float(p) - float(c))) def _check_args(): if args.triangle_name is not None and args.triangle_a is not None and args.triangle_b is not None \ and args.triangle_c is not None and not args.triangle_a.isalpha() \ and not args.triangle_b.isalpha() and not args.triangle_c.isalpha() \ and args.triangle_name != 'def' and args.triangle_a != 'def' and args.triangle_b != 'def' \ and args.triangle_c != 'def' and (float(args.triangle_c) < (float(args.triangle_a) + float(args.triangle_b)) or float(args.triangle_c) > (float(args.triangle_a) - float(args.triangle_b))): return True else: return False def _check_triangle_list(triangles): if len(triangles) == 4 and triangles[1].replace('.', '', 1).isdigit() and \ triangles[2].replace('.', '', 1).isdigit() and triangles[3].replace('.', '', 1).isdigit() and \ (float(triangles[3]) < (float(triangles[1]) + float(triangles[2])) or float(triangles[3]) > (float(triangles[1]) - float(triangles[2]))): return True else: return False while True: if _check_args(): print(triangle(args.triangle_a, args.triangle_b, args.triangle_c)) args.triangle_name = args.triangle_a = args.triangle_b = args.triangle_c = 'def' else: while True: result = {} arg_list = [] while True: triangles = input( 'Enter <triangle name>, <side a>, <side b>, ' '<side c> (Parties must be number or float): ' ).replace(' ', '').split(',') if _check_triangle_list(triangles): arg_list.append(triangles) else: print('Parameters not entered correctly') continue ask_triangle = input('Add triangle? (Y / YES): ').lower() if ask_triangle == 'y' or ask_triangle == 'yes': continue else: break for i in arg_list: result[i[0]] = result.get(i[0], 0) + triangle( i[1], i[2], i[3]) for name, value in sorted(result.items(), key=lambda x: x[1], reverse=True): print(f'{name}: {value} cm') if ask(): continue else: break break