  ## 每日一练 | Data Scientist & Business Analyst & Leetcode 面试题 293

2018-02-10 数据应用学院 大数据应用 大数据应用

Day 193

DS Interview Questions

What is the difference between tuples and lists in Python?

BA Interview Questions

R language:

You have the data.frame ‘mydf’ with four columns like below.

a = c(3,7,NA, 9)

b = c(2,NA,9,3)

f = c(5,2,5,6)

d = c(NA,3,4,NA)

mydf = data.frame(a=a,b=b,f=f,d=d)

mydf

## You want to add another column ‘5’:

## the 5th column contains the value of col b if col a is NA;

## the 5th column contains the value of col d if col b is NA;

## the 5th column contains the value of col f in all other cases.

LeetCode Questions

Description:

• Say you have an array for which the ith element is the price of a given stock on day i.

• Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Input: [1,2,3,1,6]

Output: 7

Day 192 答案揭晓

DS Interview Questions

Write the code to sort an array in NumPy by the nth column?

Using argsort () function this can be achieved. If there is an array X and you would like to sort the nth column then code for this will be x[x [: n-1].argsort ()]

BA Interview Questions

R language:

Write a for loop that prints the Displacement (‘disp’) of the ‘mtcars’ dataset.

a. This loop will only print observations of 160 or higher in ‘disp’.

b. This loop will stop as soon as an observation is smaller than 160 in ‘disp’.

data(mtcars)

names(mtcars)

dim(mtcars)

x=mtcars\$disp

for(i in 1:length(x)){

if(x[i]>=160){

print(x[i])

}

}

for(i in 1:length(x)){

if(x[i]>=160){

print(x[i])

}else if(x[i]<160){

break

}

}

for (i in 1:length(mtcars\$disp)){

if(mtcars\$disp[i] < 160) break;

print(mtcars\$disp[i])

i = i +1

}

Leetcode Questions

Description:

• Say you have an array for which the ith element is the price of a given stock on day i.

• If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Input: [7, 1, 5, 3, 6, 4]

Output: 5

Solution:

• 记录当前遇到的最小值，每次将当前卖出与最小值买入所得利润与最大利润进行比较

Code:

Time Complexity: O(n)

Space Complexity: O(1)