Skip to content

Commit 3c65a62

Browse files
authored
Merge pull request DaleStudy#106 from meoooh/main
[김한기] Week 5 Solutions
2 parents 410ad39 + 2dc75f9 commit 3c65a62

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

3sum/han.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule Solution do
2+
@spec three_sum(nums :: [integer]) :: [[integer]]
3+
def three_sum(nums) do
4+
length_of_nums = length nums
5+
num_index_map = nums |> Enum.with_index |> Map.new
6+
frequencies_of_num = nums |> Enum.frequencies()
7+
tuple_nums = nums |> List.to_tuple
8+
num_indexs_map = nums |>
9+
Enum.with_index() |>
10+
Enum.group_by(fn {v, _} -> v end, fn {_, i} -> i end)
11+
12+
Stream.unfold({0, 1}, fn {i, j} ->
13+
if j < length_of_nums - 1,
14+
do: {{i, j}, {i, j + 1}},
15+
else: {{i, j}, {i + 1, i + 2}}
16+
end) |>
17+
Stream.take_while(fn {i, _} ->
18+
i < length_of_nums - 1
19+
end) |>
20+
Stream.map(fn {i, j} ->
21+
a = elem tuple_nums, i
22+
b = elem tuple_nums, j
23+
c = -(a + b)
24+
25+
case frequencies_of_num[c] do
26+
nil -> nil
27+
count when count >= 3 -> [a, b, c] |> Enum.sort()
28+
_ ->
29+
if num_indexs_map[c] |> Enum.filter(& &1 != i && &1 != j) |> Enum.at(0),
30+
do: [a, b, c] |> Enum.sort(),
31+
else: nil
32+
end
33+
end) |>
34+
Stream.reject(& &1 == nil) |>
35+
Stream.uniq |>
36+
Enum.to_list
37+
end
38+
end

encode-and-decode-strings/han.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
class Solution:
3+
"""
4+
@param: strs: a list of strings
5+
@return: encodes a list of strings to a single string.
6+
"""
7+
def encode(self, strs):
8+
return '-'.join([
9+
'.'.join([
10+
str(ord(c))
11+
for c in s
12+
])
13+
for s in strs
14+
])
15+
16+
"""
17+
@param: str: A string
18+
@return: decodes a single string to a list of strings
19+
"""
20+
def decode(self, str):
21+
return [
22+
''.join([
23+
chr(int(c))
24+
for c in s.split('.')
25+
])
26+
for s in str.split('-')
27+
]
28+
29+
30+
# 아래는 간단한 테스트 코드
31+
import random
32+
33+
def generate_random_string(min_length=5, max_length=20):
34+
# Choose a random string length between min_length and max_length
35+
string_length = random.randint(min_length, max_length)
36+
37+
# Define the character categories
38+
alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
39+
numbers = '0123456789'
40+
hanguls = '가나다라마바사아자차카타파하'
41+
emojis = ['😀', '😂', '😉', '😍', '😎', '😭', '😊', '😜', '😢', '😱']
42+
43+
# Probability of selecting from each category
44+
weights = [0.25, 0.25, 0.25, 0.25]
45+
46+
# Combine all categories into a single list
47+
all_chars = list(alphabets + numbers + hanguls) + emojis
48+
49+
# Select characters based on the weights and create the final string
50+
random_chars = random.choices(all_chars, k=string_length)
51+
random_string = ''.join(random_chars)
52+
53+
return random_string
54+
55+
def generate_multiple_random_strings():
56+
strings_list = []
57+
# Generate a random number of strings, between 1 and 10
58+
number_of_strings = random.randint(1, 10)
59+
60+
for _ in range(number_of_strings):
61+
random_string = generate_random_string()
62+
strings_list.append(random_string)
63+
64+
return strings_list
65+
66+
strs = generate_multiple_random_strings()
67+
print(strs)
68+
print(strs==Solution().decode(Solution().encode(strs)))

top-k-frequent-elements/han.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule Solution do
2+
@spec top_k_frequent(nums :: [integer], k :: integer) :: [integer]
3+
def top_k_frequent(nums, k) do
4+
nums |>
5+
Enum.frequencies |>
6+
Enum.sort_by(fn {_, v} -> v end, :desc) |>
7+
Enum.take(k) |>
8+
Enum.map(fn {k, _} -> k end)
9+
end
10+
end

0 commit comments

Comments
 (0)