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..5c0ef18 --- /dev/null +++ "b/\345\256\236\346\223\215\346\274\224\347\273\203/solutions/s001-jhhuang96.py" @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- + + +def get_first_duplicated_number(numbers): + ''' + 输入参数: + numbers: 输入的数组 + duplication: 输出数组,判题程序会取duplication[0] 作为重复的数字 + ''' + # write code here + #if numbers == None: + if not numbers: + return -1 + dict = {} + duplication =[] + for num in numbers: + if num not in dict: + dict[num] = 0 + else: + duplication.append(num) + 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]) 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