LeetCode Record

Image for post
Image for post
LeetCode

Question 1: 243 Shortest Word Distance I

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

Note:

You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Example:

Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].

Input:word1 = 「coding」, word2 = 「practice」Output:3

Input:word1 = “makes”, word2 = “coding”Output:1

此方法需要traversal整個array來建立hash table, 之後再去比較其value中的最小距離

給予兩個array, 內容為兩個input值出現在words中的index值, 需要traversal整個array來建立此兩個array, 之後再去比較其value中的最小距離, Time complxity與 Thought1 差不多

設index1, index2 = -1, -1 , traversal整個array 若word1出現且index2為-1則word1紀錄index1=i; 若word2出現且index1為-1則記錄index2=i; 若index1 != -1 且 index2 != -1 則找最短距離與目前最優解做比較 取最小值

def getShortest(self, word1, word2): i1…

Tell you how to use the same code to run in Python 2 and 3.

After packing my module to wheel, I started to test it and found that the code below couldn’t run in Python 3 but it’s fine in Python 2.

from modulename import *

This is the folder structure

modulename
├── __init__.py
└── modulename.so

modulename.so is the file providing functionalities and I needed to wrap some class names as another. The __init__.py is like this:

from modulename import *
ClsPublic = ClsPersonal # export it as ClsPublic

Then the import error came out. So frustrating!

The fact…


Image for post
Image for post
A Bug

I tried to install a wheel package file by pip, but it showed the error.

$ pip install dist/mypkg-1.1.0-cp27-cp27mu-linux_x86_64.whl

I got:

pyzeal-1.1.0-cp27-cp27mu-linux_x86_64.whl is not a supported wheel on this platform.

Renaming the wheel file to fit the supported wheel format. To get the supported format in your environment.

Python 2.7.11 (default, May 8 2019, 02:10:04) [GCC 5.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pip; print(pip.pep425tags.get_supported()) [('cp27', 'cp27m', 'manylinux1_x86_64'), ('cp27', 'cp27m', 'linux_x86_64'), ('cp27', 'none', 'manylinux1_x86_64'), ('cp27', 'none', 'linux_x86_64'), ('py2', 'none', 'manylinux1_x86_64'), ('py2', 'none', 'linux_x86_64'), ('cp27', 'none', 'any'), ('cp2', 'none', 'any'), ('py27', 'none', 'any'), ('py2'…

I got started to use protobuf in Python , after installed google.protobuf compiler.

$ yum install protobuf-compiler

Problem

Traceback (most recent call last):
File "test_proto.py", line 1, in <module>
import test_simple_service_pb2
File "/app/build_py3/test_simple_service_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
ModuleNotFoundError: No module named 'google'

Solution

If you’re using pip3.

$ pip3 install protobuf

Pip2

$ pip install protobuf

Pybind11 is a very powerful library that is able to program between Python and C++.

Yesterday I wanted to use C++ abstract class in Python, read and tried the code on the official document.

But it ran into a weird error at PYBIND11_OVERRIDE_PURE when compiling.

"pybibd11" error: expected primary-expression before ',' token

Can’t find any solutions from the official doc.

Replacing PYBIND11_OVERRIDE_PURE with PYBIND11_OVERLOAD_PURE can get over the compiling.


Image for post
Image for post
Mock bird

This article will bring you some simple examples for learning the mock module in unit tests.

System environment

  • OS: CentOs 8
  • Python version: 3.6.8

Motivation

These days I was assigned a task to make the unit tests completed. To make the tests simple, I learned to utilize the mock module in Python. I had heard about this module for a while but didn’t have a chance to understand it clearly. Now I regret that I didn’t learn it early. (Yeah, it’s my fault.)

What can the mock module offer to you?

It’s a very useful tool to cooperate with the unittest module. Assumed that you knew how to write unit tests…


Image for post
Image for post
English learning

這是本人第一次發中文文章, 目的是為了介紹一些我自己在近期常常使用的英文學習資源, 大部分都是免費的, 如果有興趣請往下閱讀…

在台灣, 大家都是號稱從國小甚至幼稚園就開始學習英文, 但事實呢? 大部分的人所訓練到的也不過是 Reading 與 Writing 居多, 而且偏以考試導向去做學習, 導致於真正需要用上時就捉襟見肘。

以我自己的例子而言, 我住在台灣最南端的鄉鎮, 從小在一個小農村長大, 一路照著學校的英語課程一路學習上來, 直到研究所畢業, 我的認知是我的英文是「堪用」程度 — 可以讀一些文章, 能基本信件的往來

But, 就是這個But

去年夏天我因公出差到歐洲大約一個月, 出國後我才發現我的語文能力完全派不上用場, 在餐廳點餐時的無力感, 還有跟國外同事以英文討論時, 我只能聽得 …


Image for post
Image for post
debugging

Introduction

Today was the first day I got started to learn how to CI/CD on Gitlab. Everything was fine until I tried to push my first change. I’m a VsCode lover, and it provides a great user interface to do git actions. I clicked the push button, and Gitlab requested me to log in. Okay, but actually this was the first time to do it. So I used the correct account. Then the error message jumped out. And the logging box never appeared again.

Error message


Introduction

Have you ever tried to search for some tools to make you program more happily? Do you like to change the theme or background to write code in a more comfortable way? Right now, a really cool extension in VsCode can make you want to do more coding — Power Mode.

Using this extension, you can type the words with a cool animation behind, like the picture below.

Image for post
Image for post

It provides many options to choose the amination you like, such as particles, fireworks, flames, etc.

Installation

Open the extension market and search for it in terms of “power mode”. …

Jaime Lin

From Taiwan, a beautiful island. Learning English and sharing code experience.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store