From 1b336bf09a7a09a4cdfef75ff4b4ac6f9afdf7d4 Mon Sep 17 00:00:00 2001 From: Mia von Steinkirch Date: Mon, 13 May 2019 14:42:48 -0700 Subject: [PATCH] so easy --- interview_cake/math/in_place_shuffle.py | 33 ++++++++++ interview_cake/sort_and_search/big_words.py | 70 +++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 interview_cake/math/in_place_shuffle.py create mode 100644 interview_cake/sort_and_search/big_words.py diff --git a/interview_cake/math/in_place_shuffle.py b/interview_cake/math/in_place_shuffle.py new file mode 100644 index 0000000..850c48f --- /dev/null +++ b/interview_cake/math/in_place_shuffle.py @@ -0,0 +1,33 @@ +#!/bin/python + +""" +Write a function for doing an in-place shuffle of a list. + +The shuffle must be "uniform," meaning each item in the original list must have the same probability of ending up in each spot in the final list. + +Assume that you have a function get_random(floor, ceiling) for getting a random integer that is >= floor and <= ceiling. +""" + +import random + +def get_random(floor, ceiling): + return random.randrange(floor, ceiling + 1) + +def shuffle(the_list): + + if len(the_list) <= 1: + return the_list + + last_index_in_the_list = len(the_list) - 1 + + for i in range(len(the_list) - 1): + random_choice_index = get_random(i, + last_index_in_the_list) + if random_choice_index != i: + the_list[i], the_list[random_choice_index] = \ + the_list[random_choice_index], the_list[i] + + +seed_list = [5, 2, 6, 2, 6] +shuffle(seed_list) +print seed_list \ No newline at end of file diff --git a/interview_cake/sort_and_search/big_words.py b/interview_cake/sort_and_search/big_words.py new file mode 100644 index 0000000..dde0af3 --- /dev/null +++ b/interview_cake/sort_and_search/big_words.py @@ -0,0 +1,70 @@ +#!/bin/python + +""" +I want to learn some big words so people think I'm smart. + +I opened up a dictionary to a page in the middle and started flipping through, looking for words I didn't know. I put each word I didn't know at increasing indices in a huge list I created in memory. When I reached the end of the dictionary, I started from the beginning and did the same thing until I reached the page I started at. + +Now I have a list of words that are mostly alphabetical, except they start somewhere in the middle of the alphabet, reach the end, and then start from the beginning of the alphabet. In other words, this is an alphabetically ordered list that has been "rotated." For example: + + words = [ + 'ptolemaic', + 'retrograde', + 'supplant', + 'undulate', + 'xenoepist', + 'asymptote', # <-- rotates here! + 'babka', + 'banoffee', + 'engender', + 'karpatka', + 'othellolagkage', +] + +Write a function for finding the index of the "rotation point," which is where I started working from the beginning of the dictionary. This list is huge (there are lots of words I don't know) so we want to be efficient here. +""" + +def find_index(words): + + for i, word in enumerate(words): + if word[0] > words[i+1][0]: + return i+1, words[i+1] + + return "Not found" + + + +def find_index_bs(words): + first_word = words[0] + floor_index = 0 + ceiling_index = len(words) - 1 + + while floor_index < ceiling_index: + guess_index = floor_index + ((ceiling_index - floor_index) / 2) + + if words[guess_index] >= first_word: + floor_index = guess_index + else: + ceiling_index = guess_index + + if floor_index + 1 == ceiling_index: + return ceiling_index + + +words = [ + 'ptolemaic', + 'retrograde', + 'supplant', + 'undulate', + 'xenoepist', + 'asymptote', + 'babka', + 'banoffee', + 'engender', + 'karpatka', + 'othellolagkage', +] + +print find_index(words) +print +print find_index_bs(words) \ No newline at end of file