For coding test
So unlike list, where if u put end index range beyond the length of list and get an index out of range error, for string it wont cause error.
U can put end index that exceeds the length of the string but the sliced index will read up to the end of the string
word = "gh"
print(word[1:3]) # Outputs: "h"
for list comprehenstion, when u wanna return a boolean we use all()
for perm_a in permutations(a):
for perm_b in permutations(b):
check_a = all(perm_a[i][0]>=perm_a[i-1][0] for i in range(1,n))
If you have a list of strings, specifically numbers in string form, the sort doesnt work like you would expect for a list of integers. But if it is character string, then sort works as lexicological order.
look at here (https://velog.io/@whitehousechef/%EB%B0%B1%EC%A4%80-5052%EB%B2%88-%EC%A0%84%ED%99%94%EB%B2%88%ED%98%B8-%EB%AA%A9%EB%A1%9D)
this lexciographical q is also v hard
https://velog.io/@whitehousechef/Leetcode-440.-K-th-Smallest-in-Lexicographical-Order-HARD
you can also sort on string timestamps
If you somewhere need to get the frequence of characters in your string, the easy way to get it is to use Counter. This sorts the keys.
If you want to sort via the values instead of keys like the above way
This checks if the string's characters are all alphabets or numbers (alphanumeric).
string1 = "Python3.11"
string2 = "Python is fun!"
string3 = "12345"
print(string1.isalnum()) # Output: True
print(string2.isalnum()) # Output: False (due to spaces and exclamation mark)
print(string3.isalnum()) # Output: True
converts string or a single character to lower alphabet form.
So unlike list, which has an in built function of reverse(), strings dont have reverse() method. Instead, it is string[::-1]
If you want to replace a character or a sub string within your string, use .replace(‘some char or subtring you want to replace’, ‘some char or substring you want to replace with’)
but this has time complextiy of nm where n and m are lengths of strings.
v impt that it doesnt modify the original string in-place but instead, creates a new one with the replaced characters/strings.
look at https://velog.io/@whitehousechef/Leetcode-2566.-Maximum-Difference-by-Remapping-a-Digit
Finds the first character in the string that matches a pattern that we are looking for
https://velog.io/@whitehousechef/Leetcode-2566.-Maximum-Difference-by-Remapping-a-Digit
if you want to strip/divide your string into a list of substrings that is split via a pattern like an empty whitespace or a common pattern char or substring, you can do string.split(‘X’).
If no delimiter is put, it splits on any whitespace. That is why baekjoon i kept doing input().split().
VERY IMPT: the most important diff between split and strip is since splitting is splitting on a specific pattern, we return the results as a list. So like map(int,list) will not work.
text = "Hello world, how are you?"
words = text.split() # Splits by whitespace
print(words) # Output: ['Hello', 'world,', 'how', 'are', 'you?']
csv_line = "John,Doe,30"
data = csv_line.split(",") # Splits by comma
print(data) # Output: ['John', 'Doe', '30']
It removes any leading or trailing whitespace or specified characters from the input string. If no argument is given, it removes front and back whitespace (\n). Normally if there is string AB and you just do hola = input(), hola is gonna be 'AB\n'. So you have to strip this whitespace via input().strip().
text = " Hello world "
cleaned_text = text.strip() # Removes leading and trailing whitespace
print(cleaned_text) # Output: 'Hello world'
messy_data = "!!!Important!!!"
stripped_data = messy_data.strip("!") # Removes leading and trailing '!'
print(stripped_data) # Output: 'Important'
for reading graph like
7
0110100
0110101
1110101
0000111
0100000
0111110
0111000
graph=[]
for _ in range(n):
graph.append(list(map(int,input().strip())))
we SHOULD NOT use split() cuz it already returns the result as a list. Instead, we should just remove any trailing and leading whitespace and on that clean string, we map each character as a string and append them up as a list.
It is same for strings and lists to check if an element is in a string
If your string is given in a list and you want to concantentate them into 1 single string, you can do
" ".join(lst_string()). join() method is used to concatenate the elements of an iterable (such as a list) into a single string, with elements separated by a specified separator that you can specify.
s = ['Hello', 'world', 'this', 'is', 'a', 'list']
hola = " ".join(s)
print(hola) # Output: 'Hello world this is a list'
s = ['January', 'February', 'March']
result = "-".join(s)
print(result) # Output: 'January-February-March'
converts a character to ascii value.
https://velog.io/@whitehousechef/%EB%B0%B1%EC%A4%80-1987%EB%B2%88-%EC%95%8C%ED%8C%8C%EB%B2%B3
ascii_value = ord('a') # 'a' -> 97
if u wanna convert to 0~26 range, u should minus ord('a')
value = ord(char) - ord('a')
ord('a') - ord('a') # 0
ord('b') - ord('a') # 1
...
ord('z') - ord('a') # 25
but if u converted ascii value to 0~25, u should **add the ord('a) back**
value = 0
char_back = chr(value + ord('a')) # 'a'
result = "{:.2f}".format(ans[0])
.2f means we want to represent a float up to 2 d.p..
You first start with f, followed by quotation marks, and whatever u want to print out. The variable needs to be enclosed in curly braces {}, before ending with quotation marks.
print(f"char_count2: {char_count2}")
lst of strings = [f"{payer} : {point} for payer, point in dic.items()]