Skip to content

Instantly share code, notes, and snippets.

@humbroll
humbroll / diamond.rb
Created October 28, 2014 07:51
draw empty body diamond
# Requirements
# * 속이 빈 다이아몬드를 그린다.
# * size는 다이아몬드의 높이를 나타낸다.
# * 높이가 짝수이든 홀수이든 잘 그려져야 한다.
def diamond(size = 8)
str = ""
# up side
c = size / 2
c.downto(1) do |i|
@humbroll
humbroll / fibonacci.rb
Created October 28, 2014 08:52
fibonacci
require 'benchmark'
# O(2^n) - exponential
def exp_fib(c = 0)
return 1 if c < 2
return exp_fib(c-1) + exp_fib(c-2)
end
# O(n) - linear
def lin_fib(s = 0)
@humbroll
humbroll / top_twenty_frequent_words.rb
Created March 2, 2015 02:05
top_twenty_frequent_words
# Download this file - The Adventures of Sherlock Holmes
# http://www.gutenberg.org/cache/epub/1661/pg1661.txt
#
# Write a program to print the 20 most frequent words in the document, in
# descending order,
# with counts. Output format looks like:
#
# 9213 the
# 3223 i
def top_twenty_frequent_words(text)
def creditcard_number_luhn_validator(card_num, check_num)
s1 = ""
card_num.split(//).map(&:to_i).reverse.each_with_index do |n, i|
s1 << ((i%2 == 1)? (n*2) : n)
end
s2 = s1.split(//).inject(0){|sum,c| sum + c.to_i}
s3 = (s2.divmod(10)[0]+1)*10 - s2
@humbroll
humbroll / amicable_pair.rb
Created November 19, 2012 07:49
extract amicable pair
=begin
두 수 220과 284는 약수를 통해 매우 친근한 관계를 맺고 있다.
220의 진약수(자신을 제외한 약수)는 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110인데, 이것들의 합은 284이다.
또 284의 진약수는 1, 2, 4, 71, 142인데, 이것들의 합은 220이다.
서로 다른 친구를 ‘또 다른 나’라고 역설한 피타고라스는 이 두 수에서 우정의 표상을 발견했으며, 이런 수들을 ‘우애수’의 쌍이라고 불렀다.
10000 이하의 숫자들 중에서 우애수 쌍의 목록을 구하라.
=end
require 'benchmark'
def amicable_pair(max_num)
@humbroll
humbroll / amicable_pair_improved_versoin.rb
Created November 20, 2012 04:58
extract amicable pair - improved version
=begin
두 수 220과 284는 약수를 통해 매우 친근한 관계를 맺고 있다.
220의 진약수(자신을 제외한 약수)는 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110인데, 이것들의 합은 284이다.
또 284의 진약수는 1, 2, 4, 71, 142인데, 이것들의 합은 220이다.
서로 다른 친구를 ‘또 다른 나’라고 역설한 피타고라스는 이 두 수에서 우정의 표상을 발견했으며, 이런 수들을 ‘우애수’의 쌍이라고 불렀다.
10000 이하의 숫자들 중에서 우애수 쌍의 목록을 구하라.
=end
# require parallel gem, https://github.com/grosser/parallel, for improve performance
require 'parallel'
@humbroll
humbroll / find_mines.rb
Created December 6, 2012 09:02
find mines
require 'benchmark'
# 출처 : http://howproject.net/ps/wiki.php/FindMines
# 정수 n과 m 에 대하여, n x m 크기의 지뢰밭에서 모든 지뢰의 힌트 위치를 출력
# 예) 4X4 지뢰밭에 지뢰가 두개 있고, 각각을 '*' 로 표시한다고 하면 지뢰밭은 다음과 같이 표시할 수 있다.
# * . . .
# . . . .
# . * . .
# . . . .
# 각 칸에서 최대 8개의 방위에 대해 인접한 칸에 몇개의 지뢰가 있는지(힌트위치)를 출력한다고 하면
# * 1 0 0
@humbroll
humbroll / refrigerator.rb
Last active December 11, 2015 23:59
refrigerator
# 출처: http://220.81.36.44/30stair/refrigerator/refrigerator.php?pname=refrigerator
# 우리나라 아이스크림 공장은 여러가지 종류의 아이스크림을 생산하고 있다. 그런데 공장에서생산된 아이스크림의 형태를 그대로 보존하기 위해서는 일정한 온도를 유지해 주어야만 한다. 아이스크림은 종류에 따라 유지해야할 온도의 최소값과 최대값이 정해져 있다. 즉 , A 라는 아이스크림의 유지 온도가 -20 oc ~ 12 oc라면, 냉장고의 온도가 -20 oc에서 -12oc 사이일 때만 아이스크림은 형태가 변형되지 않고 보관될 수 있다. 냉장고의 온도는 고정된 값을 정할 수 있으며 하나의 냉장고에는 여러 종류의 아이스크림이 들어 갈 수 있다.
# 이 아이스크림 공장에서 생산되는 여러 종류의 아이스크림을 모두 보관 할 수 있도록 냉장고를 준비할 때 사용하는 냉장고의 수가 최소가 되도록 각 냉장고의 온도를 결정하는 프로그램을 작성하라.
# 예를들어 유지온도가 각각 -20 oc~ -15 oc, -14 oc~ -5 oc , -18 oc~ -13 oc, -5 oc~ -3 oc인 네 개의 아이스크림이 있다고 하면 , -15 oc도 맞추어 놓은 냉장고에 첫 번째와 세 번째 아이스크림을 넣고, -5 oc로 맞추어 놓은 냉장고에 두 번째와 네 번째 아이스크림을 넣으면 두 대의 냉장고로 모든 아이스크림을 보관할 수 있다. 물론 첫 번째 냉장고는 -15 oc대신 -16 oc혹은 -18 oc로 맞추어도 상관없다.
#
# 입력 형식
# 첫 째 줄에 아이스크림 종류의 개수 N( 1 <= N <= 100000 )이 주어지며, 두 번째 줄부터 N+1 번째 줄까지는 각 아이스크림이 그대로 보존되는 최저 보관 온도( -30000 <= Xi <= 30000 ) 와 최고 보관온도 ( -30000 <= Yi <= 30000 ) 가 주어진다. 모든 온도는 정수이다.
# 출력 형식
# 필요한 냉장고의 최소 개수 k 를 출력한다.
# 입출력 예
@humbroll
humbroll / Detect and Remove Loop in a Linked List
Created July 31, 2016 04:37
Detect and Remove Loop in a Linked List
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Question.
# Detect and Remove Loop in a Linked List
# Write a function detectAndRemoveLoop() that checks whether a given Linked List contains loop and
# if loop is present then removes the loop and returns true. And if the list doesn’t contain loop
# then returns false. Below diagram shows a linked list with a loop. detectAndRemoveLoop() must change
# the below list to 1->2->3->4->5->NULL.
@humbroll
humbroll / string_compression_1_6.py
Created August 7, 2016 05:29
String Compression
#!/usr/bin/env python
# String Compression
# Implement a method to perform basic string compression using the counts of repeated characters.
# For example, the string aabcccccaaa would become a2b1c5a3. If the "compressed" string would not
# become smaller than the original string, your method should return the original string.
# You can assume the string has only uppercase and lowercase letters(a-z)
import sys