Check Permutation: Given two strings, write a method to decide if one is a permutation of the other. p.90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
defcheck_permutation(s1, s2): iflen(s1) != len(s2): returnFalse sd1 = {} for i in s1: if sd1.get(i): sd1[i] = sd1[i] + 1 else: sd1[i] = 1 for j in s2: if sd1.get(j): if sd1.get(j) > 0: sd1[i] = sd1[i] - 1 else: returnFalse else: returnFalse returnTrue
samples
dad, bda
abc, bca
sss, sss
sas, ssa
abcd, abc
bbba, bbbb
URLify: Write a method to replace all spaces in a string with ‘%20’. You may assume that the string has sufficient space at the end to hold the additional characters, and that you are given the “true” length of the string. (Note: If implementing in Java, please use a character array so that you can perform this operation in place.)
錯誤1: 忘記回傳值
Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palin-drome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import copy
defpalindrome(input): input = input.lower() char_dict = {} for c ininput: if c != " ": if char_dict.get(c): char_dict.pop(c) else: char_dict[c] = 1 iflen(char_dict) > 1: returnFalse returnTrue
錯誤1: 沒有考慮到大小寫問題
One Away: There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
defone_edit(str_a, str_b): iflen(str_a) - len(str_b) == -1: for c in str_a: if c notin str_b: returnFalse eliflen(str_a) - len(str_b) == 1: for c in str_b: if c notin str_a: returnFalse eliflen(str_a) - len(str_b) == 0: counter = 0 for c in str_a: if c notin str_b: counter +=1 if counter > 1: returnFalse else: returnFalse returnTrue
Posted Updated a few seconds read (About 58 words)
systemctl disable [servicename] sudo trash /etc/systemd/system/[servicename] sudo trash /etc/systemd/system/[servicename] # and symlinks that might be related sudo trash /usr/lib/systemd/system/[servicename] sudo trash /usr/lib/systemd/system/[servicename] # and symlinks that might be related systemctl daemon-reload systemctl reset-failedbash