I tried "Five programming problems every Software Engineer should be able to solve in less than 1 hour" in Python.
This file contains 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
#! /usr/bin/env python | |
# -*- coding:utf-8 -*- | |
num_list = range(10) | |
# use for roop | |
list_sum = 0 | |
for num in num_list : | |
list_sum += num | |
print 'list_sum counted with for roop :', list_sum | |
# use while roop | |
list_sum, i = 0, 0 | |
while i < len(num_list) : | |
list_sum += num_list[i] | |
i += 1 | |
print 'list_sum counted with while roop :', list_sum | |
# use recursive function | |
def count_sum (num_list, i=0) : | |
if i == len(num_list) - 1 : | |
return num_list[i] | |
else : | |
return num_list[i] + count_sum(num_list, i+1) | |
print 'list_sum counted with recursive function :', count_sum(num_list) |
This file contains 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
#! /usr/bin/env python | |
# -*- coding:utf-8 -*- | |
def combine_alternately (list1, list2) : | |
new_list = [] | |
for e1, e2 in zip(list1, list2) : | |
new_list += [e1, e2] | |
return new_list | |
def combine_alternately2 (list1, list2) : | |
new_list = [] | |
i, j = 0, 0 | |
while i < len(list1) or j < len(list2) : | |
if i < len(list1) : | |
new_list.append(list1[i]) | |
if j < len(list2) : | |
new_list.append(list2[j]) | |
i += 1 | |
j += 1 | |
return new_list | |
if __name__ == '__main__' : | |
list1, list2 = ['1', '2', '3', '4'], ['a', 'b', 'c'] | |
print combine_alternately(list1, list2) | |
print combine_alternately2(list1, list2) |
This file contains 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
#! /usr/bin/env python | |
# -*- coding:utf-8 -*- | |
def fibonacci_series (size=100) : | |
fibos = [0, 1] | |
while len(fibos) < size : | |
fibos.append(fibos[-1] + fibos[-2]) | |
return fibos | |
print fibonacci_series(size=100) |
This file contains 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
#! /usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import itertools | |
def max_num_combination (num_list) : | |
max_num = 0 | |
for a_list in itertools.permutations(num_list, len(num_list)) : | |
num = int(''.join(map(str, a_list))) | |
if max_num < num : | |
max_num = num | |
return max_num | |
num_list = [50, 2, 1, 9] | |
print max_num_combination(num_list) |
This file contains 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
#! /usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import itertools | |
from exam_2 import combine_alternately2 | |
operations = ['+', '-', ''] | |
for string in itertools.product(operations, repeat=8) : | |
num_list = ['{0}'.format(i+1) for i in range(9)] | |
formula = ''.join(combine_alternately2(num_list, string)) | |
if eval(formula) == 100 : | |
print formula, '=', eval(formula) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment