python basic knowledge test -- 01 string

python basic knowledge test

In the process of using python, many basic knowledge does not need to be gradually unfamiliar. In order to enhance the familiarity, we do corresponding tests for each basic knowledge point of python, and constantly summarize and review.
Knowledge points:
Character string
list
Dictionaries
File read and write
Cyclic traversal
class
Source: LeetCode
Links: https://leetcode-cn.com/problems/unique-morse-code-words
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.

Title:
1.

Problem solving:

class Solution(object):
    def uniqueMorseRepresentations(self, words):
        """
        :type words: List[str]
        :rtype: int
        """
        mes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
        op = []
        for word in words:
            mes_j = ""
            for i in word:
                j = ord(i)-97 # ord(i) convert letters to ascii
                mes_j = mes_j + mes[j]
            if mes_j not in op:
                op.append(mes_j)
        return len(op)

class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """
        # if len(moves)%2 != 0:
        #     return False
        # if moves.count("R") != moves.count("L"):
        #     return False
        # if moves.count("U") != moves.count("D"):
        #     return False
        # else:
        #     return True
        return  moves.count("R") == moves.count("L") and moves.count("U") == moves.count("D")

 class Solution(object):
    def reverseString(self, s):
        """
        :type s: List[str]
        :rtype: None Do not return anything, modify s in-place instead.
        """
        i = 0
        j = len(s) - 1
        while i < j: # Key here
            s[i], s[j] = s[j], s[i]
            i = i+1
            j = j-1
        return s

class Solution:
    def reverseWords(self, s: str) -> str:
        temp = s.split(' ')
        res = ''
        for each in temp:
            res += each[::-1] + ' '  # Clever use of slices
        return res[:-1] #Last space no

Keywords: Python network ascii

Added by shu on Tue, 22 Oct 2019 22:34:19 +0300