Skip to content

Commit bec17ee

Browse files
author
Jessica Yung
committed
chore: sync and correct typo in Project Euler p7
1 parent 8fb79ac commit bec17ee

17 files changed

+550
-4
lines changed

.DS_Store

6 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
solveMeFirst a b = a + b
2+
3+
main = do
4+
val1 <- readLn
5+
val2 <- readLn
6+
let sum = solveMeFirst val1 val2
7+
print sum
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- print "Hello World"
2+
hello_world = putStrLn "Hello World"
3+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
def is_pivot_index(current_word, previous_word):
2+
"""Return a boolean indicating whether the current word is
3+
ordered before the previous word, i.e. if the current word
4+
is the rotation point.
5+
"""
6+
if current_word < previous_word:
7+
return True
8+
else:
9+
return False
10+
11+
def brute_force_one(words):
12+
"""Returns the index of the 'rotation point'.
13+
Time complexity: O(n)
14+
Space complexity: O(1)
15+
"""
16+
if is_pivot_index(words[0], words[-1]):
17+
print(i, words[i])
18+
return i
19+
else:
20+
number_of_words = len(words)
21+
for i in range(1, number_of_words):
22+
if is_pivot_index(words[i], words[i-1]):
23+
print(i, words[i])
24+
return i
25+
break
26+
27+
def binary_search(words):
28+
"""Returns the index of the 'rotation point'.
29+
Time complexity: O(logn)
30+
Space complexity: O(1)
31+
"""
32+
if is_pivot_index(words[0], words[-1]):
33+
print(i, words[i])
34+
return i
35+
else:
36+
# Binary tree to speed things up.
37+
floor_index = 0
38+
ceiling_index = len(words)
39+
40+
# Todo: Check if int ruins alg
41+
found_pivot = False
42+
while found_pivot = False:
43+
halfway_distance = int((ceiling_index - floor_index)/2)
44+
current_index = floor_index + halfway_distance
45+
floor_word = word[floor_index]
46+
current_word = word[current_index]
47+
if prev_word > current_word:
48+
# Then rotation point is in first half of current interval
49+
50+
# Check if current word is rotation point
51+
if is_pivot_index(current_word, words[current - 1]):
52+
# If it is, we are done.
53+
found_pivot = True
54+
return current
55+
56+
else:
57+
# Reset the distance
58+
ceiling_index = current_index
59+
current_temp = current
60+
current = current - (current - prev) / 2
61+
prev = current_temp
62+
else:
63+
# Pivot is in second half of current interval
64+
floor_index = current_index
65+
66+
# Get index of the first lettor of the first word, init_index
67+
# Get the index of the first letter of the middle word
68+
# Do is_pivot_index
69+
# If
70+
#
71+
72+
# Test case
73+
words_one = [
74+
'ptolemaic',
75+
'retrograde',
76+
'supplant',
77+
'undulate',
78+
'xenoepist',
79+
'asymptote', # <-- rotates here!
80+
'babka',
81+
'banoffee',
82+
'engender',
83+
'karpatka',
84+
'othellolagkage',
85+
]
86+
87+
print(brute_force_one(words_one))

interview-cake/binary-search.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def binary_search(target, nums):
2+
# see if target appears in nums
3+
4+
# we think of floor_index and ceiling_index as "walls" around
5+
# the possible positions of our target so by -1 below we mean
6+
# to start our wall "to the left" of the 0th index
7+
# (we /don't/ mean "the last index")
8+
floor_index = -1
9+
ceiling_index = len(nums)
10+
11+
# if there isn't at least 1 index between floor and ceiling,
12+
# we've run out of guesses and the number must not be present
13+
while floor_index + 1 < ceiling_index:
14+
15+
# find the index ~halfway between the floor and ceiling
16+
# we use integer division, so we'll never get a "half index"
17+
distance = ceiling_index - floor_index
18+
half_distance = distance / 2
19+
guess_index = floor_index + half_distance
20+
21+
guess_value = nums[guess_index]
22+
23+
if guess_value == target:
24+
return True
25+
26+
if guess_value > target:
27+
28+
# target is to the left
29+
# so move ceiling to the left
30+
ceiling_index = guess_index
31+
32+
else:
33+
34+
# target is to the right
35+
# so move floor to the right
36+
floor_index = guess_index
37+
38+
return False
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Confirm PEP8 says var[i+1] as opposed to var[i + 1]
2+
3+
def string_to_array(string):
4+
"""
5+
6+
"""
7+
array = []
8+
for i in range(len(string)):
9+
array.append(string[i])
10+
return array
11+
12+
def remove_whitespaces(array):
13+
for i in range(len(array)):
14+
if array[i] == ' ':
15+
del array[i]
16+
return array
17+
18+
def sort_array(array):
19+
# Quicksort
20+
pass
21+
22+
def check_if_perm_of_palindrome(sorted_array):
23+
"""Checks if a sorted array is a permutation of a palindrome."""
24+
i = 0
25+
singles = 0
26+
while i < len(sorted_array):
27+
if i == len(sorted_array) - 1:
28+
if singles == 0:
29+
return True
30+
else:
31+
return False
32+
elif sorted_array[i] == sorted_array[i+1]:
33+
i = i + 2
34+
elif sorted_array[i] != sorted_array[i+1]:
35+
if singles == 0:
36+
i = i + 1
37+
else:
38+
return False
39+
40+
# Read input
41+
string = str(input())
42+
43+
# Make string an array
44+
array = string_to_array(string)
45+
46+
# Tidy up string by removing whitespaces and making all lower case.
47+
array = remove_whitespaces(array).lower()
48+
49+
# Sort string. E.g. input 'Tact Coa' -> 'aaccott' after this stage.
50+
sorted_array = sort_array(string)
51+
52+
#
53+
check_if_perm_of_palindrome(sorted_array)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
2.1 Remove Duplicates
3+
4+
Write code to remove duplicates from an unsorted linked list.
5+
6+
How would you solve this problem if a temporary buffer is not allowed?
7+
"""
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
def ll_length(node):
3+
pass
4+
5+
def sum_lists(ll_one_node, ll_two_node):
6+
if ll_length(ll_one_node) != ll_length(ll_two_node):
7+
pass
8+
#
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Linked Lists in Python
3+
4+
Taken from InterviewCake August 2016
5+
"""
6+
7+
class LinkedListNode():
8+
"""Singly Linked List Nodes"""
9+
def __init__(self, value):
10+
# super(LinkedListNode, self).__init__()
11+
self.value = value
12+
self.next = None
13+
14+
15+
# Build a singly linked list
16+
17+
a = LinkedListNode(5)
18+
b = LinkedListNode(1)
19+
c = LinkedListNode(9)
20+
21+
a.next = b
22+
b.next = c
23+
24+
class DLinkedListNode():
25+
"""Doubly Linked List Nodes"""
26+
def __init__(self, arg):
27+
# super(DLinkedListNode, self).__init__()
28+
self.arg = arg
29+
self.next = None
30+
self.previous = None
31+
32+
# Build a doubly linked list
33+
34+
# Put b after a
35+
a.next = b
36+
b.previous = a
37+
38+
# Put c after b
39+
b.next = c
40+
c.previous = b
41+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Creating a Linked List
2+
3+
class Node {
4+
Node next = null;
5+
int data;
6+
7+
public Node(int d) {
8+
data = d;
9+
}
10+
11+
void appendToTail(int d) {
12+
Node end = new Node(d);
13+
Node n = this;
14+
while (n.next != null) {
15+
n = n.next;
16+
}
17+
n.next = end;
18+
}
19+
}

0 commit comments

Comments
 (0)