Skip to content
Snippets Groups Projects
Commit 1ba0f6fe authored by Ziyan Lu's avatar Ziyan Lu
Browse files

main

parent b22a2126
No related branches found
No related tags found
No related merge requests found
ex3.py 0 → 100644
def merge(list1, list2):
list = []
i = 0
j = 0
while(len(list1) > i and len(list2) > j):
list.append(list1[i])
i+=1
list.append(list2[j])
j+=1
if (len(list1) > len(list2)):
list.extend(list1[i:])
else:
list.extend(list2[j:])
return list
print(merge([1,2,3,4],['a','b','c',2,9]))
ex5.py 0 → 100644
def first_n_fibonacci(n):
fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
return fib
print(first_n_fibonacci(100))
ex6.py 0 → 100644
from functools import cmp_to_key
def compare_first_digit(a, b):
a_first_digit = int(str(a)[0])
b_first_digit = int(str(b)[0])
if a_first_digit < b_first_digit:
return 1
elif a_first_digit > b_first_digit:
return -1
else:
if len(str(a)) == 1 and len(str(b)) > 1:
return compare_first_digit(a, int(str(b)[1:]))
elif len(str(a)) > 1 and len(str(b)) == 1:
return compare_first_digit(int(str(a)[1:]), b)
elif len(str(a)) > 1 and len(str(b)) > 1:
return compare_first_digit(int(str(a)[1:]), int(str(b)[1:]))
else:
return 0
def largest_number(numbers):
sorted_numbers = sorted(numbers, key=cmp_to_key(compare_first_digit))
return int(''.join(map(str, sorted_numbers)))
numbers = [5, 2, 1, 90, 57]
print(largest_number(numbers))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment