It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
Apparently, many engineers take measures on the site called LetCode.
It is a site that trains the algorithmic power that can withstand the coding test that is done in the early story, and it is an inevitable path for those who want to build a career at an overseas tech company.
I wrote it in a big way, but I have no plans to have such an interview at the moment.
However, as an IT engineer, it would be better to have the same level of algorithm power as a person, so I would like to solve the problem irregularly and write down the method I thought at that time as a memo.
I'm solving it with Python3.
Leet Code Table of Contents Starting from Zero
Last time Leet Code Day67 starting from zero "1486. XOR Operation in an Array"
Right now, I'm prioritizing the Medium of the Top 100 Liked Questions. I solved all Easy, so if you are interested, please go to the table of contents.
Twitter I'm doing it.
** Technical Blog Started! !! ** ** I think the technology will write about LetCode, Django, Nuxt, and so on. ** This is faster to update **, so please bookmark it!
709. To Lower Case The difficulty level is Easy.
The problem is that we implement a function ToLowerCase () that has the string parameter str and returns the same string in lowercase.
Example 1:
Input: "Hello" Output: "hello"
Example 2:
Input: "here" Output: "here"
Example 3:
Input: "LOVELY" Output: "lovely"
class Solution:
def toLowerCase(self, str: str) -> str:
return str.lower()
# Runtime: 24 ms, faster than 91.12% of Python3 online submissions for To Lower Case.
# Memory Usage: 14 MB, less than 15.17% of Python3 online submissions for To Lower Case.
If you know lower
, you can write it like this, but what if you don't know it and you don't have an environment to look it up, or if you solve it without using built-in functions, you will be asked in an actual interview.
Considering such a situation, it is easy, but it seems to use my head unexpectedly.
What came to my mind was how to manage with a dictionary. It stores uppercase letters in key and lowercase letters in value, and turns it with a for statement to change it to the corresponding element of value if there is an element corresponding to key. However, I thought that another thing that came to my mind might be something that uses Unicode conversion, so I implemented that this time.
class Solution:
def toLowerCase(self, str: str) -> str:
ans = ''
for s in str:
if ord(s) >= ord('A') and ord(s) <= ord('Z'):
ans +=chr(ord(s) - (ord('A') - ord('a')))
else:
ans += s
return ans
# Runtime: 28 ms, faster than 71.56% of Python3 online submissions for To Lower Case.
# Memory Usage: 13.8 MB, less than 69.29% of Python3 online submissions for To Lower Case.
Use the ʻord functionto convert to Unicode Unicode points and compare. If that value is within the range, subtract Unicode points, and finally convert it to a character string using the
chr function and assign it to ʻans
. In other cases, the idea is that it is originally lowercase, so you can just substitute it.
As a supplement, the Unicode list is as follows. Unicode list 0000-0FFF
It was very interesting because I thought about it from a perspective that I wouldn't normally think of. So that's it for this time. Thank you for your hard work.
Recommended Posts