From 39322737f2466e26d57e37f8452f92373826fb13 Mon Sep 17 00:00:00 2001 From: jhhuang Date: Wed, 28 Nov 2018 23:32:24 +0800 Subject: [PATCH 1/2] Add files via upload --- .../solutions/s001-jhhuang96.py" | 51 ++++++++++++++++++ .../solutions/s002-jhhuang96.py" | 25 +++++++++ .../solutions/s003-jhhuang96.py" | 53 +++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 "\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" create mode 100644 "\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s002-jhhuang96.py" create mode 100644 "\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s003-jhhuang96.py" diff --git "a/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" new file mode 100644 index 0000000..73f453e --- /dev/null +++ "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- + + +def get_first_duplicated_number(numbers): + ''' + 输入参数: + numbers: 输入的数组 + duplication: 输出数组,判题程序会取duplication[0] 作为重复的数字 + ''' + # write code here + #if numbers == None: + if not numbers: + return -1 + uniquenums = list(set(numbers)) + counts = {} + for num in uniquenums: + counts.setdefault(num,0) + + for num in numbers: + for keys in counts: + if num == keys: + counts[keys] += 1 + + d = counts.copy() + for key, vals in counts.items(): + if vals == 1: + del d[key] + duplication = list(d.keys()) + if duplication: + return duplication[0] + else: + return -1 + + +if __name__ == "__main__": + + assert 2 == get_first_duplicated_number([2, 3, 1, 0, 2, 5, 3]) + + assert 1 == get_first_duplicated_number([2, 3, 4, 0, 9, 8, 7, 1, 1]) + + assert -1 == get_first_duplicated_number([2, 3, 4, 0, 9, 8, 7, 10, 13]) + + assert -1 == get_first_duplicated_number([]) + + assert -1 == get_first_duplicated_number(None) + + assert 1 == get_first_duplicated_number([2, 1, 1, 2]) + + assert -1 == get_first_duplicated_number([1]) + + assert 1 == get_first_duplicated_number([1, 1]) \ No newline at end of file diff --git "a/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s002-jhhuang96.py" "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s002-jhhuang96.py" new file mode 100644 index 0000000..74d03f1 --- /dev/null +++ "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s002-jhhuang96.py" @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + + +def fibonacci(n): + if n==0: + return 1 + elif n == 1 or n == 2: + return n + else: + return fibonacci(n-1) + fibonacci(n-2) + + +if __name__ == "__main__": + + assert 1 == fibonacci(0) + + assert 1 == fibonacci(1) + + assert 2 == fibonacci(2) + + assert 8 == fibonacci(5) + + assert 89 == fibonacci(10) + + assert 10946 == fibonacci(20) \ No newline at end of file diff --git "a/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s003-jhhuang96.py" "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s003-jhhuang96.py" new file mode 100644 index 0000000..7898958 --- /dev/null +++ "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s003-jhhuang96.py" @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + + +def partition(alist, start, end): + if end <= start: + return + base = alist[start] + while start < end: + while start < end and alist[end] >= base: + end -= 1 + alist[start] = alist[end] + while start < end and alist[start] <= base: + start += 1 + alist[end] = alist[start] + alist[start] = base + return start + + +def smallest_k_numbers(k, alist): + if k==0 or not alist : + return [] + length = len(alist) + if length == k: + return alist + if not alist or k <=0 or k > length: + return + start = 0 + end = length - 1 + index = partition(alist, start, end) + while index != k: + if index > k: + index = partition(alist, start, index - 1) + elif index < k: + index = partition(alist, index + 1, end) + return alist[:k] + + + +if __name__ == "__main__": + + assert [1,2,3,4] == smallest_k_numbers(4, [4,5,1,6,2,7,3,8]) + + assert [] == smallest_k_numbers(0, []) + + assert [1] == smallest_k_numbers(1, [1]) + + assert [] == smallest_k_numbers(0, [4,5,1,6,2,7,3,8]) + + assert [4,5,1,6,2,7,3,8] == smallest_k_numbers(8, [4,5,1,6,2,7,3,8]) + + #assert [4,5,1,6,2,7,3,8] == smallest_k_numbers(8, [11,33,4,5,1,6,99,384,27,2,7,3,8,10]) + + assert [1,99] == smallest_k_numbers(2, [1, 99]) \ No newline at end of file From d2e72fa14f2b8f6705880e9e45b9b25c67ac545e Mon Sep 17 00:00:00 2001 From: jhhuang Date: Thu, 6 Dec 2018 14:52:16 +0800 Subject: [PATCH 2/2] Update s001-jhhuang96.py --- .../solutions/s001-jhhuang96.py" | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git "a/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" index 73f453e..5c0ef18 100644 --- "a/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" +++ "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" @@ -11,21 +11,13 @@ def get_first_duplicated_number(numbers): #if numbers == None: if not numbers: return -1 - uniquenums = list(set(numbers)) - counts = {} - for num in uniquenums: - counts.setdefault(num,0) - + dict = {} + duplication =[] for num in numbers: - for keys in counts: - if num == keys: - counts[keys] += 1 - - d = counts.copy() - for key, vals in counts.items(): - if vals == 1: - del d[key] - duplication = list(d.keys()) + if num not in dict: + dict[num] = 0 + else: + duplication.append(num) if duplication: return duplication[0] else: @@ -48,4 +40,4 @@ def get_first_duplicated_number(numbers): assert -1 == get_first_duplicated_number([1]) - assert 1 == get_first_duplicated_number([1, 1]) \ No newline at end of file + assert 1 == get_first_duplicated_number([1, 1])