Exemplo n.º 1
0
def reachable(dictionary):
    '''reachable has a dict parameter (representing the graph) and a str start node in the graph 
    (technically a key in the dict; it returns a set of all the nodes reachable from it by following edges in the graph
    '''
    init_list = list(dictionary.keys())
    init_list.sort()
    
    k = prompt.for_char('Enter starting node', default=None, legal=str(init_list), error_message='Please enter one char in the range (if specified)')

    while k != 'quit':
        reached_set = set()
        lst = [k]
        lst.extend(list(dictionary[k]))
    #     a = lst[0]
        while len(lst) > 0 and lst[0] not in reached_set :
    #         popthing = lst.pop(0)
            
            for items in dictionary[lst[0]]:
                if items not in lst and items in list(dictionary.keys()):
                    lst.append(items)
                elif items not in list(dictionary.keys()):
                    reached_set.add(items)
 
                reached_set.add(lst.pop(0))
        
        print("From " +k+ " the reachable nodes are:" + str(reached_set) + '\n')
        k = prompt.for_char('Enter starting node', default=None, legal=str(init_list), error_message='Please enter one char in the range (if specified)')
Exemplo n.º 2
0
def reachable(dictionary):
    '''reachable has a dict parameter (representing the graph) and a str start node in the graph 
    (technically a key in the dict; it returns a set of all the nodes reachable from it by following edges in the graph
    '''
    init_list = list(dictionary.keys())
    init_list.sort()

    k = prompt.for_char(
        'Enter starting node',
        default=None,
        legal=str(init_list),
        error_message='Please enter one char in the range (if specified)')

    while k != 'quit':
        reached_set = set()
        lst = [k]
        lst.extend(list(dictionary[k]))
        #     a = lst[0]
        while len(lst) > 0 and lst[0] not in reached_set:
            #         popthing = lst.pop(0)

            for items in dictionary[lst[0]]:
                if items not in lst and items in list(dictionary.keys()):
                    lst.append(items)
                elif items not in list(dictionary.keys()):
                    reached_set.add(items)

                reached_set.add(lst.pop(0))

        print("From " + k + " the reachable nodes are:" + str(reached_set) +
              '\n')
        k = prompt.for_char(
            'Enter starting node',
            default=None,
            legal=str(init_list),
            error_message='Please enter one char in the range (if specified)')
Exemplo n.º 3
0
def mini_Backwardable_test(i):
    print(
        '\n\nmini_Backwardable_test: enter sequences of next/prev operations (or quit)'
    )
    while True:
        print('\ni =', i)
        command = prompt.for_char(
            'Enter 1-character Command: [n]ext, [p]rev, or [q]uit',
            legal='npq')
        try:
            if command == 'n':
                print(' ', next(i))
            elif command == 'p':
                print(' ', prev(i))
            else:
                break
        except:
            print('  command raised exception')
Exemplo n.º 4
0
def mini_Backwardable_test(i):
    print(
        '\n\nmini_Backwardable_test: enter sequences of next/prev operations (or quit)'
    )
    while True:
        print('\ni =', i)
        command = prompt.for_char(
            'Enter 1-character Command: [n]ext, [p]rev, or [q]uit',
            legal='npq')
        try:
            if command == 'n':
                print(' ', next(i))
            elif command == 'p':
                print(' ', prev(i))
            else:
                break
        except:
            # Error message may make it look like you cannot continue entering commands, but you can
            print('  command raised exception')
            traceback.print_exc()
if __name__ == "__main__": 
    import prompt
    print('Begin testing PriorityQueue') 
    command_prompt = \
"""\nTesting PriorityQueue:
Commands        Queries             Other
  a - add...      ? - in_same         . - exec(...)
  m - merge...    e - is_singleton    i - iterator
  c - clear       s - size            q - quit
                  * - all_classes__
                  _ - __str__
               
Command""" 
    e = eval('EquivalenceClass('+input('e = EquivalenceClass('))
    while True:
        action = prompt.for_char(command_prompt, legal='amc?es*_.iq')
        try:
            if   action == 'a': e.add_singleton(prompt.for_string('  Enter value to add as singleton'))
            elif action == 'm':
                a = prompt.for_string('  Enter first  value for merge')
                b = prompt.for_string('  Enter second value for merge')
                e.merge_classes_containing(a,b)
            elif action == 'c': e.clear()
            
            elif action == '?': print('  in_same_classe =',e.in_same_class())
            elif action == 'e': print('  is_singleton =',e.is_singleton())
            elif action == 's': print('  size =',e.size())
            elif action == '_': print('  str =',e)
            elif action == '*': print('  all_classes =',e.all_classes())
            
            elif action == '.': exec(prompt.for_string('  Enter command to exec (instance=e)'))
Exemplo n.º 6
0
    command_prompt = \
"""\nTesting Dice:
Commands     Queries                  Other
  r - roll     n - number_of_dice       . - exec(...)
               m - all_pip_maximums     D - standard_rolls_for_debugging
               s - rolls                q - quit
               o - pips_on
               a - all_pips
               ? - pip_sum
               =  - pips_same
               _  - __str__
Command"""
    d = Dice(eval(prompt.for_string('Enter list of max pips',
                                    default='[6,6]')))
    while True:
        action = prompt.for_char(command_prompt, legal='rnmsoa?=_.Dq')
        try:
            if action == 'r': d.roll()
            elif action == 'n': print('  Number of dice =', d.number_of_dice())
            elif action == 'm':
                print('  all pip maximums =', d.all_pip_maximums())
            elif action == 's':
                print('  rolls =', d.rolls())
            elif action == 'o':
                i = prompt.for_int_between('  Enter die #', 0,
                                           d.number_of_dice() - 1)
                print('  pips on die #', str(i), ' =', d.pips_on(i))
            elif action == 'a':
                print('  all pips =', d.all_pips())
            elif action == '?':
                print('  pip sum =', d.pip_sum())
    print('Begin testing Dice') 
    command_prompt = \
"""\nTesting Dice:
Commands     Queries                  Other
  r - roll     n - number_of_dice       . - exec(...)
               m - all_pip_maximums     D - standard_rolls_for_debugging
               s - rolls                q - quit
               o - pips_on
               a - all_pips
               ? - pip_sum
               =  - pips_same
               _  - __str__
Command"""                            
    d = Dice(eval(prompt.for_string('Enter list of max pips', default='[6,6]')))
    while True:
        action = prompt.for_char(command_prompt, legal='rnmsoa?=_.Dq')
        try:
            if   action == 'r': d.roll()
            elif action == 'n': print('  Number of dice =',d.number_of_dice())
            elif action == 'm': print('  all pip maximums =',d.all_pip_maximums())
            elif action == 's': print('  rolls =',d.rolls())
            elif action == 'o':
                i = prompt.for_int_between('  Enter die #', 0, d.number_of_dice()-1)
                print('  pips on die #',str(i),' =',d.pips_on(i))
            elif action == 'a': print('  all pips =',d.all_pips())
            elif action == '?': print('  pip sum =',d.pip_sum())
            elif action == '=': print('  pips all the same =',d.pips_same())
            elif action == '_': print('  str =',str(d))
            elif action == '.': exec(prompt.for_string('  Enter command to exec (instance=d)'))
            elif action == 'D': d.standard_rolls_for_debugging()
            elif action == 'q': break
Exemplo n.º 8
0

        
if __name__ == "__main__": 
    import prompt
    print("Begin testing Stopwatch") 
    commandPrompt = \
"""\nTesting Stopwatch:
Commands                Queries        Other
  > - start               ? - read       . - exec(...)
  s - stop                : - status     q - quit
  < - start_backwards     _ - __str__
  r - reset\nCommand"""                            
    s = Stopwatch()
    while True:
        action = prompt.for_char(commandPrompt, legal=">s<r?:_.q")
        try:
            if   action == ">": s.start()
            elif action == "s": s.stop()
            elif action == "<": s.start_backwards()
            elif action == "r": s.reset()
            elif action == "?": print("  Elapsed =", s.read(), "seconds")
            elif action == ":": print("  Status =", s.status())
            elif action == "_": print("  __str__ =", str(s))
            elif action == ".": exec(prompt.for_string("  Enter command to exec (instance=s)"))
            elif action == "q": break
            else: print("  Unknown command")
        except AssertionError as report:
            print("  AssertionError exception caught:", report)
        except Exception as report:
            import traceback
if __name__ == "__main__":
    import prompt

    print("Begin testing PriorityQueue")
    command_prompt = """\nTesting PriorityQueue:
Commands     Queries         Other
  a - add      p - peek        . - exec(...)
  r - remove   e - is_empty    i - iterator
  c - clear    s - size        q - quit
  m - merge    _ - __str__
               
Command"""
    pq = eval("PriorityQueue(" + input("pq = PriorityeQueue("))
    while True:
        action = prompt.for_char(command_prompt, legal="arcmpes_.iq")
        try:
            if action == "a":
                pq.add(prompt.for_string("  Enter value to add"))
            elif action == "r":
                print("  remove =", pq.remove())
            elif action == "c":
                pq.clear()
            elif action == "m":
                exec("pq.merge(" + input("pq.merge("))

            elif action == "p":
                print("  peek =", pq.peek())
            elif action == "e":
                print("  is_empty =", pq.is_empty())
            elif action == "s":
Exemplo n.º 10
0
        
if __name__ == '__main__': 
    import prompt
    print('Begin testing Stack') 
    command_prompt = \
"""\nTesting Stack:
Commands     Queries         Other
  a - add      p - peek        . - exec(...)
  r - remove   e - is_empty    i - iterator
  c - clear    s - size        q - quit
               _ - __str__
               
Command""" 
    s = eval('Stack('+input('s = Stack('))
    while True:
        action = prompt.for_char(command_prompt, legal='arcpes_.iq')
        try:
            if   action == 'a': s.add(prompt.for_string('  Enter value to add'))
            elif action == 'r': print('  remove =',s.remove())
            elif action == 'c': s.clear()
            
            elif action == 'p': print('  peek =',s.peek())
            elif action == 'e': print('  is_empty =',s.is_empty())
            elif action == 's': print('  size =',s.size())
            elif action == '_': print('  str =',s)
            
            elif action == '.': exec(prompt.for_string('  Enter command to exec (instance=s)'))
            elif action == 'i': print('  iteration order =',[i for i in s])
            elif action == 'q': break
            else: print('  Unknown command')
        except AssertionError as report:
Exemplo n.º 11
0
if __name__ == '__main__':
    import prompt
    print('Begin testing Stack')
    command_prompt = \
"""\nTesting Stack:
Commands     Queries         Other
  a - add      p - peek        . - exec(...)
  r - remove   e - is_empty    i - iterator
  c - clear    s - size        q - quit
               _ - __str__
               
Command"""
    s = eval('Stack(' + input('s = Stack('))
    while True:
        action = prompt.for_char(command_prompt, legal='arcpes_.iq')
        try:
            if action == 'a': s.add(prompt.for_string('  Enter value to add'))
            elif action == 'r': print('  remove =', s.remove())
            elif action == 'c': s.clear()

            elif action == 'p': print('  peek =', s.peek())
            elif action == 'e': print('  is_empty =', s.is_empty())
            elif action == 's': print('  size =', s.size())
            elif action == '_': print('  str =', s)

            elif action == '.':
                exec(prompt.for_string('  Enter command to exec (instance=s)'))
            elif action == 'i':
                print('  iteration order =', [i for i in s])
            elif action == 'q':
Exemplo n.º 12
0
if __name__ == "__main__":
    import prompt
    print('Begin testing PriorityQueue')
    command_prompt = \
"""\nTesting PriorityQueue:
Commands        Queries             Other
  a - add...      ? - in_same         . - exec(...)
  m - merge...    e - is_singleton    i - iterator
  c - clear       s - size            q - quit
                  * - all_classes__
                  _ - __str__
               
Command"""
    e = eval('EquivalenceClass(' + input('e = EquivalenceClass('))
    while True:
        action = prompt.for_char(command_prompt, legal='amc?es*_.iq')
        try:
            if action == 'a':
                e.add_singleton(
                    prompt.for_string('  Enter value to add as singleton'))
            elif action == 'm':
                a = prompt.for_string('  Enter first  value for merge')
                b = prompt.for_string('  Enter second value for merge')
                e.merge_classes_containing(a, b)
            elif action == 'c':
                e.clear()

            elif action == '?':
                print('  in_same_classe =', e.in_same_class())
            elif action == 'e':
                print('  is_singleton =', e.is_singleton())
Exemplo n.º 13
0

        
if __name__ == "__main__": 
    import prompt
    print("Begin testing Stopwatch") 
    commandPrompt = \
"""\nTesting Stopwatch:
Commands                Queries        Other
  > - start               ? - read       . - exec(...)
  s - stop                : - status     q - quit
  < - start_backwards     _ - __str__
  r - reset\nCommand"""                            
    s = Stopwatch()
    while True:
        action = prompt.for_char(commandPrompt, legal=">s<r?:_.q")
        try:
            if   action == ">": s.start()
            elif action == "s": s.stop()
            elif action == "<": s.start_backwards()
            elif action == "r": s.reset()
            elif action == "?": print("  Elapsed =", s.read(), "seconds")
            elif action == ":": print("  Status =", s.status())
            elif action == "_": print("  __str__ =", str(s))
            elif action == ".": exec(prompt.for_string("  Enter command to exec (instance=s)"))
            elif action == "q": break
            else: print("  Unknown command")
        except AssertionError as report:
            print("  AssertionError exception caught:", report)
        except Exception as report:
            import traceback
Exemplo n.º 14
0

if __name__ == '__main__': 
    import prompt
    print('Begin testing Modular') 
    command_prompt = \
"""\nTesting Modular:
Commands     Queries          Other
  i - inc      v - value        . - exec(...)
  d - dec      m - modulus      q - quit
  c - clear    _  - __str__
  
Command"""                            
    m = Modular(prompt.for_int('Enter value'),prompt.for_int('Enter modulus'))
    while True:
        action = prompt.for_char(command_prompt, legal='idc_.q')
        try:
            if   action == 'i': m.inc()
            elif action == 'd': m.dec()
            elif action == 'c': m.clear()
            elif action == 'v': print('  value =', m.value())
            elif action == 'm': print('  modulus =', m.modulus())
            elif action == 'c': m.clear()
            elif action == '_': print('  str =',m)
            elif action == '.': exec(prompt.for_string('  Enter command to exec (instance=m)'))
            elif action == 'q': break
            else: print('  Unknown command')
        except AssertionError as report:
            print('  AssertionError exception caught:', report)
        except Exception as report:
            import traceback
Exemplo n.º 15
0

if __name__ == '__main__':
    import prompt
    print('Begin testing Modular')
    command_prompt = \
"""\nTesting Modular:
Commands     Queries          Other
  i - inc      v - value        . - exec(...)
  d - dec      m - modulus      q - quit
  c - clear    _  - __str__
  
Command"""
    m = Modular(prompt.for_int('Enter value'), prompt.for_int('Enter modulus'))
    while True:
        action = prompt.for_char(command_prompt, legal='idc_.q')
        try:
            if action == 'i': m.inc()
            elif action == 'd': m.dec()
            elif action == 'c': m.clear()
            elif action == 'v': print('  value =', m.value())
            elif action == 'm': print('  modulus =', m.modulus())
            elif action == 'c': m.clear()
            elif action == '_': print('  str =', m)
            elif action == '.':
                exec(prompt.for_string('  Enter command to exec (instance=m)'))
            elif action == 'q':
                break
            else:
                print('  Unknown command')
        except AssertionError as report: