Calvin (Deutschbein)
W14Mon: 24 Nov
def check_word(my_word, word_list):
while len(word_list) > 1:
half_length = len(word_list) // 2
if my_word < word_list[half_length]:
# keep only the first half
word_list = word_list[:half_length]
else:
# keep only the second half
word_list = word_list[half_length:]
return my_word == word_list[0]
def check_word(my_word, word_list):
while len(word_list) > 1:
half_length = len(word_list) // 2
if my_word < word_list[half_length]:
word_list = word_list[:half_length]
else:
word_list = word_list[half_length:]
return my_word == word_list[0]
...
word_list = word_list[:half_length]
...
word_list = word_list[half_length:]
import requests, json
URL = "https://gist.githubusercontent.com/cd-public/0a09043d500a9bc3397ebcfeb5f7a4f5/raw/41d37a509cc19e0609a1637370096f30ff4a1ea3/english.json"
r = requests.get(URL)
ENGLISH_WORDS = r.json()
import sys
sys.getsizeof(ENGLISH_WORDS)
word_list = word_list[:half_length]
head = 0
tail = len(word_list) - 1
while tail - head > 1:
midl = head + (tail - head) // 2
if my_word < word_list[midl]:
# keep only the first half
tail = midl
else:
# keep only the second half
head = midl
def check_word(my_word):
head, tail = 0, len(ENGLISH_WORDS) - 1
while tail - head > 1:
midl = head + (tail - head) // 2
if my_word < ENGLISH_WORDS[midl]:
tail = midl
else:
head = midl
return my_word == ENGLISH_WORDS[head]
Return the time in seconds since the epoch as a floating-point number.
The epoch is the point where the time starts, the return value of `time.gmtime(0)`. It is January 1, 1970, 00:00:00 (UTC) on all platforms.
python -c "import time; print(time.time())"
1763869688.1104693
from time import time
start = time()
print("After you press enter, the amount of time since this text appeared will be printed.")
input()
end = time()
print(end-start)
from time import time
test_set = ENGLISH_WORDS[50::100]
funcs = [by_in, by_for, by_copy, by_index]
for f in funcs:
start = time()
for word in test_set:
f(word), f(word[::-1])
print(f, time()-start)
<function by_in at 0x7aa67a763d90> 1.5537760257720947
<function by_for at 0x7aa679869c60> 4.493199586868286
<function by_copy at 0x7aa679915630> 1.0388541221618652
<function by_index at 0x7aa6799156c0> 0.0052835941314697266