Skip to content

Instantly share code, notes, and snippets.

View yuheiomori's full-sized avatar

Yuhei Omori yuheiomori

View GitHub Profile
@yuheiomori
yuheiomori / detecting_cycles.py
Created May 13, 2012 00:46
CodeEval Detecting Cycles
import sys
def detecting_cycles(l):
while True:
try:
top = l.pop(0)
pos = l.index(top)
candidate = l[0:pos]
sample = l[pos + 1:pos + 1 + len(candidate)]
@yuheiomori
yuheiomori / pangrams.py
Last active October 4, 2015 19:58
CodeEval Pangrams
import sys
import re
def pangrams(s):
alphabet = "abcdefghijklmnopqrstuvwxyz"
s = re.sub(' ', '', s).lower()
for c in s:
@yuheiomori
yuheiomori / first_non_repeated_character.py
Created May 14, 2012 07:20
CodeEval First Non-Repeated Character
import sys
def get_first_non_repeated_character(s):
first_idx_of_non_repeated_character = map(lambda x: s.count(x), s).index(1)
return s[first_idx_of_non_repeated_character]
if __name__ == '__main__':
test_cases = open(sys.argv[1], 'r')
@yuheiomori
yuheiomori / remove_characters.py
Created May 14, 2012 22:56
CodeEval Remove Characters
import sys
def remove_characters(s, s2):
return ''.join([c for c in s if c not in s2])
if __name__ == '__main__':
test_cases = open(sys.argv[1], 'r')
for line in test_cases:
@yuheiomori
yuheiomori / endianness.py
Created May 15, 2012 20:13
CodeEval Endianness
import sys
if sys.byteorder == 'little':
print "LittleEndian"
else:
print "BigEndian"
@yuheiomori
yuheiomori / number_of_ones.py
Created May 16, 2012 23:06
CodeEval Number Of Ones
import sys
def to_bin(n):
result = ''
while(n > 0):
result = str(n % 2) + result
n /= 2
return result
@yuheiomori
yuheiomori / sum_of_integers.py
Last active October 5, 2015 00:47
CodeEval Sum Of Integers
import sys
def sum_of_integers(ints):
max_ending_here = 0
max_so_far = max_element = -sys.maxint - 1
for x in ints:
max_ending_here = max(0, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
max_element = max(max_element, x)
@yuheiomori
yuheiomori / deciaml_to_binary.py
Last active October 5, 2015 00:58
CodeEval Decimal To Binary
# coding=utf-8
import sys
def decimal_to_binary(n):
if n == 0:
return '0'
result = ''
while n > 0:
result = str(n % 2) + result
@yuheiomori
yuheiomori / trailing_string.py
Created May 19, 2012 23:53
CodeEval Traling String
import sys
def trailing_string(ary):
if ary[0].split()[-1].endswith(ary[1]):
return 1
else:
return 0
@yuheiomori
yuheiomori / double_square.py
Created May 21, 2012 01:07
CodeEval Double Square
import sys
from math import sqrt
def double_square(n):
s = int(sqrt(n))
count = 0
for i in range(0, s + 1):
D = sqrt(n - i * i)
if D >= i and D == int(D):