-
-
Notifications
You must be signed in to change notification settings - Fork 244
Week 1 solutions - 김한기 #29
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 1 commit
Commits
Show all changes
2 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,10 @@ | ||
| defmodule Solution do | ||
| @spec max_profit(prices :: [integer]) :: integer | ||
| def max_profit([head | tail]) do | ||
| {_, answer} = Enum.reduce(tail, {head, 0}, fn x, {buy, profit} -> | ||
| {min(buy, x), max(profit, x - buy)} | ||
| end) | ||
|
|
||
| answer | ||
| 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,10 @@ | ||
| defmodule Solution do | ||
| @spec contains_duplicate(nums :: [integer]) :: boolean | ||
| def contains_duplicate(nums) do | ||
| if nums |> Enum.into(MapSet.new) |> Enum.to_list |> length == length(nums) do | ||
| false | ||
| else | ||
| true | ||
| end | ||
| 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,9 @@ | ||
| defmodule Solution do | ||
| def two_sum([head | tail], target, index\\0) do | ||
| second_index = Enum.find_index(tail, fn element -> | ||
| head + element == target | ||
| end) | ||
|
|
||
| if second_index, do: [index, second_index + index + 1], else: two_sum(tail, target, index + 1) | ||
| 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,6 @@ | ||
| defmodule Solution do | ||
| @spec is_anagram(s :: String.t, t :: String.t) :: boolean | ||
| def is_anagram(s, t) do | ||
| s |> String.split("", trim: true) |> Enum.sort == t |> String.split("", trim: true) |> Enum.sort | ||
| 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,25 @@ | ||
| defmodule Solution do | ||
| @spec is_palindrome(s :: String.t) :: boolean | ||
| def is_palindrome(s) do | ||
| non_alphanumeric_ascii_codes = [?a..?z, ?0..?9] |> | ||
| Enum.reduce([], fn x, acc -> | ||
| acc ++ (x |> Enum.to_list) | ||
| end) | ||
| converted = s | ||
| |> String.downcase | ||
| |> String.replace(~r/./, fn char -> | ||
| if :binary.first(char) in non_alphanumeric_ascii_codes, do: char, else: "" | ||
| end) | ||
|
|
||
| length = converted |> String.length | ||
|
|
||
| if length < 2, do: true | ||
|
|
||
| center = length |> div(2) | ||
|
|
||
| converted |> String.slice(0..center) == | ||
| converted | ||
| |> String.slice(-(center+1)..-1) | ||
| |> String.reverse | ||
| 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.
엘릭서는 확장자를
exe를 쓰나요? 문법 강조가 하나도 안 되서 코드를 읽기가 쉽지가 않네요 😅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.
저도 궁금해서 마크다운 안에 넣어봤는데 마크다운 안에서는 인식이 되는군요...!
Uh oh!
There was an error while loading. Please reload this page.
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.
헛 그렇군요... 하이라이팅이 왜 안되는지 모르겠네요 좀 찾아보겠습니다.
exe와 ex 두개를 사용하는거같은데... 🤔🧐
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.
앗 exe가 아니라 exs 였군요... 수정해서 다시 올리겠습니다 ㅎㅎ
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.
문법 강조 잘 되네요 ㅎㅎ