Saturday, 17 February 2018

Python Dictionaries CopyUpdate- Day 9(After 6 days)

  • Dictionary Copy Update Concept
  •   
    # filmsvar1 = {"muthu":"one of the movie in which rajini comes in double role",
    #          "basha":"Don of a don movie,a cult hit",
    #          "enthiran":"rajini's scifi movie",
    #          "murattu kalai":"movie taken in paganeri"}
    #
    # filmsvar2 = {"BLoodStonte":"Rajini's Hollywood Movie",
    #              "enthiran2":"Upcoming Movie"}
    #
    # # print(filmsvar1)
    # # print(filmsvar2)
    # # #this will update the filmsvar1 wont create a separate object
    # # filmsvar1.update(filmsvar2)
    # # print(filmsvar1)
    # # print(filmsvar2)
    #
    # #to make a copy of dictionary
    #
    # filmsvar3 = filmsvar2.copy()
    # print(filmsvar2)
    # print(filmsvar3)
    # filmsvar2.update(filmsvar1)
    # print(filmsvar2)
    # print(filmsvar3)
    #
    # #Use the old challenge and make it work even if the user types valley,road directly
    #
    # location = {0:"You are Sitting in front of comp",
    #             1:"You are in road",
    #             2:"At the top of the hill",
    #             3:"Building",
    #             4:"You are across Valley",
    #             5:"Roaming at forest"}
    # exits = {0:{"Q":0},
    #          1:{"N":5,"5":5,"S":4,"4":4,"E":3,"3":3,"Q":0},
    #          2:{"N":5,"5":5,"Q":0},
    #          3:{"W":1,"1":1,"Q":0},
    #          4:{"W":2,"2":2,"N":1,"1":1,"Q":0},
    #          5:{"S":1,"1":1,"W":2,"2":2,"Q":0}}
    #
    # vocabularyvar = {"NORTH":"N","SOUTH":"S","EAST":"E","WEST":"W","QUIT":"Q",
    #                  "ROAD":"1","HILL":"2","BUILDING":"3","VALLEY":"4","FOREST":"5"}
    #
    # loc=1
    # while True:
    #     availablexits = ""
    #     #we can use join to do the fllowing
    #     # for direction in exits[loc].keys():
    #     #     availablexits +=direction+ ","
    #     availablexits = ','.join(exits[loc].keys())
    #
    #     print(location[loc])
    #
    #     if loc == 0:
    #         break
    #
    #     direction = input("Available Exits are "+ availablexits).upper()
    #     if len(direction)>0:
    #         words = direction.split()
    #         for word in words:
    #             if word in vocabularyvar:
    #                 direction = vocabularyvar[word]
    #                 print(direction)
    #     print()
    #     if direction in exits[loc]:
    #         loc = exits[loc][direction]
    #     else:
    #         print("You cannot go in that direction")
    
    #the above code will work,but it will unnecessarily will show no's 1,2,3,4,5 in the
    # available exits,so we can use the copy and update funtions in dictionary to solve those issues
    
    location = {0:"You are Sitting in front of comp",
                1:"You are in road",
                2:"At the top of the hill",
                3:"Building",
                4:"You are across Valley",
                5:"Roaming at forest"}
    exits = {0:{"Q":0},
             1:{"N":5,"S":4,"E":3,"Q":0},
             2:{"N":5,"Q":0},
             3:{"W":1,"Q":0},
             4:{"W":2,"N":1,"Q":0},
             5:{"S":1,"W":2,"Q":0}}
    
    namedexits  = {1:{"5":5,"4":4,"3":3,"Q":0},
                   2:{"5":5,"Q":0},
                   3:{"1":1,"Q":0},
                   4:{"2":2,"1":1,"Q":0},
                   5:{"1":1,"2":2,"Q":0}}
    
    vocabularyvar = {"NORTH":"N","SOUTH":"S","EAST":"E","WEST":"W","QUIT":"Q",
                     "ROAD":"1","HILL":"2","BUILDING":"3","VALLEY":"4","FOREST":"5"}
    
    loc=1
    while True:
        availablexits = ""
        #we can use join to do the fllowing
        # for direction in exits[loc].keys():
        #     availablexits +=direction+ ","
        availablexits = ','.join(exits[loc].keys())
    
        print(location[loc])
    
        if loc == 0:
            break
        else:
            allexits = exits[loc].copy()
            allexits.update(namedexits[loc])
    
        direction = input("Available Exits are "+ availablexits).upper()
        if len(direction)>0:
            words = direction.split()
            for word in words:
                if word in vocabularyvar:
                    direction = vocabularyvar[word]
                    print(direction)
        print()
        #previously we used index ,since it it dictionary under dictionary
        if direction in allexits:
            loc = allexits[direction]
        else:
            print("You cannot go in that direction")
    
     
    

    Python Dictionaries Challenge - Day 8(After 1 Week)


  • the below example is like a game,there are 4 places,like road,hill,valley,forest and quit, and some are uni direction some are bidirectional,n,s,e,w,q are the basic options the user may choose based on the place he is currently in.
  •   
    # location = {0:"You are Sitting in front of comp",
    #             1:"You are in road",
    #             2:"At the top of the hill",
    #             3:"Building",
    #             4:"You are across Valley",
    #             5:"Roaming at forest"}
    # exits = [{"Q":0},{"N":5,"S":4,"E":3,"Q":0},{"N":5,"Q":0},{"W":1,"Q":0},
    {"W":2,"N":1,"Q":0},{"S":1,"W":2,"Q":0}]
    # loc=1
    # while True:
    #     availablexits = ""
    #     #we can use join to do the fllowing
    #     # for direction in exits[loc].keys():
    #     #     availablexits +=direction+ ","
    #     availablexits = ','.join(exits[loc].keys())
    #
    #     print(location[loc])
    #
    #     if loc == 0:
    #         break
    #
    #     direction = input("Available Exits are "+ availablexits).upper()
    #     print()
    #     if direction in exits[loc]:
    #         loc = exits[loc][direction]
    #     else:
    #         print("You cannot go in that direction")
    
    
    #the 1st challenge here is to convert the exit list to dictionary,
    #list index can be converted to dictionary ,by making the list index no
    # to corresponding dictionary key in this case
    
    #
    #
    # location = {0:"You are Sitting in front of comp",
    #             1:"You are in road",
    #             2:"At the top of the hill",
    #             3:"Building",
    #             4:"You are across Valley",
    #             5:"Roaming at forest"}
    # exits = {0:{"Q":0},
    # 1:{"N":5,"S":4,"E":3,"Q":0},
    # 2:{"N":5,"Q":0},
    # 3:{"W":1,"Q":0},
    # 4:{"W":2,"N":1,"Q":0},
    # 5:{"S":1,"W":2,"Q":0}}
    # loc=1
    # while True:
    #     availablexits = ""
    #     #we can use join to do the fllowing
    #     # for direction in exits[loc].keys():
    #     #     availablexits +=direction+ ","
    #     availablexits = ','.join(exits[loc].keys())
    #
    #     print(location[loc])
    #
    #     if loc == 0:
    #         break
    #
    #     direction = input("Available Exits are "+ availablexits).upper()
    #     print()
    #     if direction in exits[loc]:
    #         loc = exits[loc][direction]
    #     else:
    #         print("You cannot go in that direction")
    
    
    #the 2nd challenge is to make use of the vocabulary,
    #ie user may not type in the exact letters like N,S,W,E,
    #user can type north,south as well
    #
    # location = {0:"You are Sitting in front of comp",
    #             1:"You are in road",
    #             2:"At the top of the hill",
    #             3:"Building",
    #             4:"You are across Valley",
    #             5:"Roaming at forest"}
    # exits = {0:{"Q":0},
    #          1:{"N":5,"S":4,"E":3,"Q":0},
    #          2:{"N":5,"Q":0},
    #          3:{"W":1,"Q":0},
    #          4:{"W":2,"N":1,"Q":0},
    #          5:{"S":1,"W":2,"Q":0}}
    #
    # vocabularyvar = {"North":"N","South":"S","East":"E","WEST":"W","QUIT":"Q"}
    #
    # loc=1
    # while True:
    #     availablexits = ""
    #     #we can use join to do the fllowing
    #     # for direction in exits[loc].keys():
    #     #     availablexits +=direction+ ","
    #     availablexits = ','.join(exits[loc].keys())
    #
    #     print(location[loc])
    #
    #     if loc == 0:
    #         break
    #
    #     direction = input("Available Exits are "+ availablexits)
    #     if len(direction)>0:
    #         if direction in vocabularyvar:
    #             direction = vocabularyvar[direction].upper()
    #             print(direction)
    #     print()
    #     if direction in exits[loc]:
    #         loc = exits[loc][direction]
    #     else:
    #         print("You cannot go in that direction")
    
    #
    # split command,will split the string by default by space,the result will be of list type
    # location = {0:"You are Sitting in front of comp",
    #             1:"You are in road",
    #             2:"At the top of the hill",
    #             3:"Building",
    #             4:"You are across Valley",
    #             5:"Roaming at forest,which is wild"}
    #
    # print(location[0])
    # print(location[0].split())
    # print(location[5].split(","))
    # print(" ".join(location[0].split()))
    
    #using the split command in the above example and checking whether one of the user entered words,
    #matches with the exits,like if the user enters "I Prefer South"/"I want to go north"
    location = {0:"You are Sitting in front of comp",
                1:"You are in road",
                2:"At the top of the hill",
                3:"Building",
                4:"You are across Valley",
                5:"Roaming at forest"}
    exits = {0:{"Q":0},
             1:{"N":5,"S":4,"E":3,"Q":0},
             2:{"N":5,"Q":0},
             3:{"W":1,"Q":0},
             4:{"W":2,"N":1,"Q":0},
             5:{"S":1,"W":2,"Q":0}}
    
    vocabularyvar = {"NORTH":"N","SOUTH":"S","EAST":"E","WEST":"W","QUIT":"Q"}
    
    loc=1
    while True:
        availablexits = ""
        #we can use join to do the fllowing
        # for direction in exits[loc].keys():
        #     availablexits +=direction+ ","
        availablexits = ','.join(exits[loc].keys())
    
        print(location[loc])
    
        if loc == 0:
            break
    
        direction = input("Available Exits are "+ availablexits).upper()
        if len(direction)>0:
            words = direction.split()
            for word in words:
                if word in vocabularyvar:
                    direction = vocabularyvar[word]
                    print(direction)
        print()
        if direction in exits[loc]:
            loc = exits[loc][direction]
        else:
            print("You cannot go in that direction")