Skip to content

[김한기] Week 5 Solutions #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions 3sum/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Solution do
@spec three_sum(nums :: [integer]) :: [[integer]]
def three_sum(nums) do
length_of_nums = length nums
num_index_map = nums |> Enum.with_index |> Map.new
frequencies_of_num = nums |> Enum.frequencies()
tuple_nums = nums |> List.to_tuple
num_indexs_map = nums |>
Enum.with_index() |>
Enum.group_by(fn {v, _} -> v end, fn {_, i} -> i end)

Stream.unfold({0, 1}, fn {i, j} ->
if j < length_of_nums - 1,
do: {{i, j}, {i, j + 1}},
else: {{i, j}, {i + 1, i + 2}}
end) |>
Stream.take_while(fn {i, _} ->
i < length_of_nums - 1
end) |>
Stream.map(fn {i, j} ->
a = elem tuple_nums, i
b = elem tuple_nums, j
c = -(a + b)

case frequencies_of_num[c] do
nil -> nil
count when count >= 3 -> [a, b, c] |> Enum.sort()
_ ->
if num_indexs_map[c] |> Enum.filter(& &1 != i && &1 != j) |> Enum.at(0),
do: [a, b, c] |> Enum.sort(),
else: nil
end
end) |>
Stream.reject(& &1 == nil) |>
Stream.uniq |>
Enum.to_list
end
end
68 changes: 68 additions & 0 deletions encode-and-decode-strings/han.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
return '-'.join([
'.'.join([
str(ord(c))
for c in s
])
for s in strs
])

"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
return [
''.join([
chr(int(c))
for c in s.split('.')
])
for s in str.split('-')
]


# 아래는 간단한 테스트 코드
import random

def generate_random_string(min_length=5, max_length=20):
# Choose a random string length between min_length and max_length
string_length = random.randint(min_length, max_length)

# Define the character categories
alphabets = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = '0123456789'
hanguls = '가나다라마바사아자차카타파하'
emojis = ['😀', '😂', '😉', '😍', '😎', '😭', '😊', '😜', '😢', '😱']

# Probability of selecting from each category
weights = [0.25, 0.25, 0.25, 0.25]

# Combine all categories into a single list
all_chars = list(alphabets + numbers + hanguls) + emojis

# Select characters based on the weights and create the final string
random_chars = random.choices(all_chars, k=string_length)
random_string = ''.join(random_chars)

return random_string

def generate_multiple_random_strings():
strings_list = []
# Generate a random number of strings, between 1 and 10
number_of_strings = random.randint(1, 10)

for _ in range(number_of_strings):
random_string = generate_random_string()
strings_list.append(random_string)

return strings_list

strs = generate_multiple_random_strings()
print(strs)
print(strs==Solution().decode(Solution().encode(strs)))
10 changes: 10 additions & 0 deletions top-k-frequent-elements/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Solution do
@spec top_k_frequent(nums :: [integer], k :: integer) :: [integer]
def top_k_frequent(nums, k) do
nums |>
Enum.frequencies |>
Enum.sort_by(fn {_, v} -> v end, :desc) |>
Enum.take(k) |>
Enum.map(fn {k, _} -> k end)
Comment on lines +5 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elixir스러운 직관적인 FP 코드네요! 이 문제는 Elixir가 아니더라도 다른 언어를 쓰시는 분들도 이렇게 많이 짜시더라고요 :)

end
end