Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is this syntax?
#1
Hi all,

Can anyone please explain me whats going on here? Ive never seen this syntax before.

    def judgeCircle(cls, moves):

       def moves_xy(moves):
            return {
                'R': [1, 0],
                'L': [-1, 0],
                'U': [0, 1],
                'D': [0, -1]
            }[moves]

        x = 0
        y = 0

        for move_temp in moves:
            coor = moves_xy(move_temp)
            x = x + coor[0]
            y = y + c[icode]oor[1]
Im really puzzled with the syntax moves_xy. Can anyone explain me understand this new format?
Reply
#2
It's a dictionary defined and then a value is immediately pulled from it. The dictionary could be moved out into a global constant, and then "moves" fetched from it and it would be semantically the same.
Reply
#3
Ofcourse. Apparently this approach is much faster compared to others. In fact very fast. Not sure how.
Reply
#4
It uses a hash, so that helps. The if/elif/elif/elif alternative has to check the first three values anytime it turns out it needs the fourth.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020