티스토리 뷰
edX ESaaS 수업 첫번째 과제중 하나. word에 대한 palindrome과 단어 반복횟수 체크, 그리고 들어있는 단어에 대한 anagram까지 뽑는게 주제였다.
다른건 쉬웠는데 마지막에 word마다 anagram을 뽑아서 어떻게 처리해야 될지가 약간 고민을 하게 했다.
#!/usr/bin/env ruby
class String def palindrome? original = self.delete("^a-zA-Z") if original.reverse.downcase == original.downcase return true else return false end end def count_words original = self.delete("^a-zA-Z ").downcase dic = original.split(' ') result = Hash.new dic.each do |element| result[element] = 0 end dic.each do |element| if result.has_key?(element) result[element] += 1 end end return result end def anagram_groups length = self.size if length == 0 return [] else words = self.split(' ') dict = Hash.new words.each do |word| key = word.downcase.chars.sort.join if dict.has_key?(key) dict[key].push(word) else dict[key] = [].push(word) end end return dict.values end end end
'Hobby > Code' 카테고리의 다른 글
[C] fifteen game (0) | 2014.05.26 |
---|---|
[C] vigenere cipher (1) | 2014.05.25 |
[C] Caesar Cipher (1) | 2014.05.25 |
[ruby] Rock-Paper-Scissors class (0) | 2014.05.19 |
[ruby] Very simple class for dessert (0) | 2014.05.19 |
[python] count inversion with mergesort (0) | 2014.05.18 |
[C] n squared matrix 덧셈 (0) | 2014.05.14 |
- Total
- Today
- Yesterday
- Pipeline
- Kinect
- Offline RL
- ColorStream
- processing
- 강화학습
- Off-policy
- Python
- Distribution
- Variance
- arduino
- End-To-End
- PowerPoint
- 한빛미디어
- Kinect SDK
- 파이썬
- Windows Phone 7
- Policy Gradient
- dynamic programming
- TensorFlow Lite
- Gan
- Kinect for windows
- SketchFlow
- 딥러닝
- Expression Blend 4
- reward
- RL
- DepthStream
- bias
- windows 8
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |