↳
int raise(num, power){ if(power==0) return 1; if(power==1) return num; return(raise(num, power-1)*num); } Less
↳
small mistake function power(x, n) { if n == 1 return x; // Even numbers else if (n%2 == 0) return square( power (x, n/2)); // Odd numbers else return power(x, n-1) * x; } Less
↳
double Power(int x, int y) { double ret = 1; double power = x; while (y > 0) { if (y & 1) { ret *= power; } power *= power; y >>= 1; } return ret; } Less
↳
O(size of array) time & space: First, realize that saying the element should be the product of all other numbers is like saying it is the product of all the numbers to the left, times the product of all the numbers to the right. This is the main idea. Call the original array A, with n elements. Index it with C notation, i.e. from A[0] to A[n - 1]. Create a new array B, also with n elements (can be uninitialized). Then, do this: Accumulator = 1 For i = 0 to n - 2: Accumulator *= A[i] B[i + 1] = Accumulator Accumulator = 1 For i = n - 1 down to 1: Accumulator *= A[i] B[i - 1] *= Accumulator Replace A with B It traverses A twice and executes 2n multiplicates, hence O(n) time It creates an array B with the same size as A, hence O(n) temporary space Less
↳
Create two more arrays. One array contains the products of the elements going upward. That is, B[0] = A[0], B[1] = A[0] * A[1], B[2] = B[1] * A[2], and so on. The other array contains the products of the elements going down. That is, C[n] = A[n], C[n-1] = A[n] * A[n-1], and so on. Now A[i] is simply B[i-1] * C[i+1]. Less
↳
Here are my 2 cents to do this in memory without creating temporary arrays. The simple solution , if division was allowed, was multiple all the elements of the array i.e. tolal = A[0]*A[1]]*....*A[n-1] now take a loop of array and update element i with A[i] = toal/A[i] Since division is not allowed we have to simulate it. If we say X*Y = Z, it means if X is added Y times it is equal to Z e.g. 2*3 = 6, which also means 2+2+2 = 6. This can be used in reverse to find how mach times X is added to get Z. Here is my C solution, which take pointer to array head A[0] and size of array as input void ArrayMult(int *A, int size) { int total= 1; for(int i=0; i< size; ++i) total *= A[i]; for(int i=0; i< size; ++i) { int temp = total; int cnt = 0; while(temp) { temp -=A[i]; cnt++; } A[i] = cnt; } } Speed in O(n) and space is O(1) Less
↳
Merge sort and heapsort are always guaranteed to be n*log(n). Quicksort is usually faster on the average but can be as bad as O(n^2), although with very low probability. Heapsort also does it sorting in-place, without needing an extra buffer, like mergesort. Lastly, heapsort is much easier to implement and understand than balancing trees mentioned by earlier posts. Less
↳
for something like this you generally want bubble sort or insertion sort. It's not about being fast it's about being consistent. Make it do exactly the same thing every time. Less
↳
Use a sorting network. There's some precomputation time, but runtime will be very consistent (the only variability is branch prediction performance) Less
↳
I was thinking geographically distributed servers.
↳
Cf
↳
There's probably no real correct answer, though the solutions go from common to esoteric in a pretty normal progression: caching, shared-cache like memcache, optimize usage, prefetch, then get creative. This is more about testing reasoning and how far you'll go to solve a problem. Less
↳
import datetime c = 0 for y in range(1901,2001): for m in range(1,13): d = datetime.datetime(y,m,1) if d.weekday() == 6: c += 1 print('Number of Sundays: ',c) Less
↳
import datetime count=0 for i in range(1901,2001): for j in range(1, 13): if datetime.date(i,j,1).weekday() == 6: count+=1 print(count) Less
↳
Two lines of code in matlab: Answer is 171 sundays fall on the first day of the month from 1 Jan 1901 to 31 Dec 2000 dt = datenum(1901,1,1):datenum(2001,1,1)-1; sum(day(dt(weekday(dt) == 1)) == 1) Less
↳
fib = {1:1, 2:1} def calc_fib(n): if n in fib.keys(): return fib[n] else: fib[n]=calc_fib(n-1)+calc_fib(n-2) return fib[n] print(calc_fib(9)) Less
↳
remember how to approximate as N is large
↳
The matlab example sets N = 12 (the 12th fab. number) which happens to be 144. N = 12; f = ones(N,1); for i = 3:N; f(i) = f(i-1) + f(i-2); end; f(end) Less
↳
Obviously, it is an easy answer though. First you want to check the network connection. If that seems to be working fine with other websites, check the code of the website. If the code is too cluttered then it will have problems loading. Make sure to organize the HTML so you can easily find clutter or useless text. Next, use a website validator - http://validator.w3.org/ . This website helps you find any errors on your page that may be causing a slow down. Hope this helps any, I use this for any problem I have. Less
↳
This is a very good question to ask anyone who is interested in web development. it gets them thinking and start working on problem solving. Less
↳
Various infrastructure, logic, and fail safe issues. Caching, redundancy, customer experience handling in case of failures. Less
↳
I would identify and address all of the various infrastructure, logic, and fail safe issues as well as design systems to handle caching, redundancy, and customer experience handling in case of failures. Less
↳
def spiral(mat): mat = np.array(mat) arr = [] if mat.shape == (1, 1): arr.append(mat[0][0]) return arr else: arr.extend(mat[0, :]) arr.extend(spiral(np.rot90(mat[1:, :]))) return arr Less
↳
def print_spiral(matrix): print_spiral_help(matrix, 0, 0, 0, len(matrix[0]) - 1, len(matrix) - 1) def print_spiral_help(matrix, dir, top, left, right, bot): if left > right or top > bot: return # top if dir == 0: for i in range(left, right + 1): print matrix[top][i], print_spiral_help(matrix, 1, top + 1, left, right, bot) # Right elif dir == 1: for i in range(top, bot + 1): print matrix[i][right], print_spiral_help(matrix, 3, top, left, right - 1, bot) # Left elif dir == 2: for i in range(bot, top - 1, -1): print matrix[i][left], print_spiral_help(matrix, 0, top, left + 1, right, bot) # Bottom elif dir == 3: for i in range(right, left - 1, -1): print matrix[bot][i], print_spiral_help(matrix, 2, top, left, right, bot - 1) Less