-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpractice_random.py
More file actions
41 lines (35 loc) · 1002 Bytes
/
practice_random.py
File metadata and controls
41 lines (35 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# import time
# timerStart = time.time()
# def fib_bottom_up(n=None):
# if n == 1 or n == 2:
# return 1
# fib_list = [0]*(n+1)
# fib_list[1] = 1
# fib_list[2] = 1
# for i in range(3,n+1):
# fib_list[i] = fib_list[i-2]+fib_list[i-1]
# return fib_list[n]
# print(fib_bottom_up(100000))
# # time.sleep(1)
# timerEnd = time.time()
# print(round(timerEnd-timerStart,2),'seconds')
def longestWord(words) -> str:
# helper function to check if all prefixes are in dictionary:
def check_pre(word):
for i in range(1, len(word)):
if word[:i] not in words:
return False
return True
# sort by reversed lexicographical order:
words.sort(reverse = True)
# sort by length of word:
words.sort(key = lambda a: len(a))
print(words)
# go through all words from end and check prefixes:
for word in words[::-1]:
print(word)
if check_pre(word) == True:
return word
return ""
words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
print(longestWord(words))