-
-
Notifications
You must be signed in to change notification settings - Fork 195
[김한기] 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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elixir스러운 직관적인 FP 코드네요! 이 문제는 Elixir가 아니더라도 다른 언어를 쓰시는 분들도 이렇게 많이 짜시더라고요 :)