From 20c1067de93a462f094c7a02f02538d8deb7ce0f Mon Sep 17 00:00:00 2001 From: h Date: Fri, 31 May 2024 11:03:27 +0900 Subject: [PATCH 1/2] top_k_frequent, 3sum --- 3sum/han.exs | 38 +++++++++++++++++++++++++++++++++ top-k-frequent-elements/han.exs | 10 +++++++++ 2 files changed, 48 insertions(+) create mode 100644 3sum/han.exs create mode 100644 top-k-frequent-elements/han.exs diff --git a/3sum/han.exs b/3sum/han.exs new file mode 100644 index 000000000..3c90ca29e --- /dev/null +++ b/3sum/han.exs @@ -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 diff --git a/top-k-frequent-elements/han.exs b/top-k-frequent-elements/han.exs new file mode 100644 index 000000000..ff330f894 --- /dev/null +++ b/top-k-frequent-elements/han.exs @@ -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 From 1c119cfc9f956e677cf146b53b4dc7f34388061b Mon Sep 17 00:00:00 2001 From: han Date: Sat, 1 Jun 2024 20:32:23 +0900 Subject: [PATCH 2/2] encode decode --- encode-and-decode-strings/han.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 encode-and-decode-strings/han.py diff --git a/encode-and-decode-strings/han.py b/encode-and-decode-strings/han.py new file mode 100755 index 000000000..afaaa0b85 --- /dev/null +++ b/encode-and-decode-strings/han.py @@ -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)))