Hoping to sort (below left) by sector but distribute evenly (below right):
Name
Sector.
Name.
Sector
A
1
A
1
B
1
E
2
C
1
H
3
D
4
D
4
E
2
B
1
F
2
F
2
G
2
J
3
H
3
I
4
I
4
C
1
J
3
G
2
Real data is 70+ rows with 4 sectors.
I've worked around it manually but would love to figure out how to do it with a formula in excel.
Here's a more complete (and hopefully more accurate) idea - the carouselOrder is the column I'd like to generate via a formula.
guestID
guestSector
carouselOrder
1
1
1
2
1
5
3
1
9
4
1
13
5
2
2
6
2
6
7
2
10
8
2
14
9
3
3
10
3
7
11
3
11
12
2
18
13
1
17
14
1
20
15
1
23
16
2
21
17
2
24
18
2
27
19
1
26
20
1
29
21
1
30
22
1
31
23
3
15
24
3
19
25
3
22
26
3
25
27
3
28
28
1
32
29
4
4
30
4
8
31
4
12
32
4
16
When using Office 365 you can use the following in D2: =MOD(SEQUENCE(COUNTA(A2:A11),,0),4)+1
This create the repetitive counter of the sectors 1 to 4 to the total count of rows in your data.
In C2 use the following:
=BYROW(D2#,LAMBDA(x,
INDEX(
FILTER($A$2:$A$11,$B$2:$B$11=x),
SUM(--(D$2:x=x)))))
This filters the Names that equal the sector of mentioned row and indexes it to show only the result where the row in the filter result equals the count of the same sector (D2#) up to current row.
Let's try the following approach that doesn't require to create a helper column. I would like to explain first the logic to build the recurrence, then the excel formula that builds such recurrence.
If we sort the input data Name and Sector. by Sector. in ascending order, the new positions of the Name values (letters) can be calculated as follow (Table 1):
Name
Sector.Sorted
Position
A
1
1+4*0=1
B
1
1+4*1=5
C
1
1+4*2=9
E
2
2+4*0=2
F
2
2+4*1=6
G
2
2*4*2=10
H
3
3+4*0=3
J
3
3+4*1=7
D
4
4+4*0=4
I
4
4+4*1=8
The new positions of Name (letters) follows this pattern (Formula 1):
position = Sector.Sorted + groupSize * factor
where groupSize is 4 in our case and factor counts how many times the same Sector.Sorted value is repeated, starting from 0. Think about Sector.Sorted as groups, where each set of repeated values represents a group: 1,2,3 and 4.
If we are able to build the Position values we can sort Name, based on the new positions via SORTBY(array, by_array1) function. Check SORTBY documentation for more information how this function works.
Here is the formula to get the Name sorted in cell E2:
=LET(groupSize, 4, sorted, SORT(A2:B11,2), sName,
INDEX(sorted,,1),sSector, INDEX(sorted,,2),
seq0, SEQUENCE(ROWS(sSector),,0), mapResult,
MAP(sSector, seq0, LAMBDA(a,b, IF(b=0, "SAME",
IF(a=INDEX(sSector,b), "SAME", "NEW")))), factor,
SCAN(-1,mapResult, LAMBDA(aa,c,IF(c="SAME", aa+1,0))),
pos,MAP(sSector, factor, LAMBDA(m,n, m + groupSize*n)),
SORTBY(sName,pos)
)
Here is the output:
Explanation
The name sorted represents the input data sorted by Sector. in ascending order, i.e.: SORT(A2:B11,2). The names sName and sSector represent each column of sorted.
To identify each group we need the following sequence (seq0) starting from 0, i.e. SEQUENCE(ROWS(sSector),,0).
Now we need to identify when a new group starts. We use MAP function for that and the result is represented by the name mapResult:
MAP(sSector, seq0, LAMBDA(a,b, IF(b=0, "SAME",
IF(a=INDEX(sSector,b), "SAME", "NEW"))))
The logic is the following: If we are at the beginning of the sequence (first value of seq0), then returns SAME otherwise we check current value of sSector (a) against the previous one represented by INDEX(sSector,b) if they are the same, then we are in the same group, otherwise a new group started.
The intermediate result of mapResult is:
Name
Sector Sorted
mapResult
A
1
SAME
B
1
SAME
C
1
SAME
E
2
NEW
F
2
SAME
G
2
SAME
H
3
NEW
J
3
SAME
D
4
NEW
I
4
SAME
The first two columns are shown just for illustrative purpose, but mapResult only returns the last column.
Now we just need to create the counter based on every time we find NEW. In order to do that we use SCAN function and the result is stored under the name factor. This value represents the factor we use to multiply by 4 within each group (see Table 1):
SCAN(-1,mapResult, LAMBDA(aa,c,IF(c="SAME", aa+1,0)))
The accumulator starts in -1, because the counter starts with 0. Every time we find SAME, it increments by 1 the previous value. When it finds NEW (not equal to SAME), the accumulator is reset to 0.
Here is the intermediate result of factor:
Name
Sector Sorted
mapResult
factor
A
1
SAME
0
B
1
SAME
1
C
1
SAME
2
E
2
NEW
0
F
2
SAME
1
G
2
SAME
2
H
3
NEW
0
J
3
SAME
1
D
4
NEW
0
I
4
SAME
1
The first three columns are shown for illustrative purpose.
Now we have all the elements to build our pattern for the new positions represented with the name pos:
MAP(sSector, factor, LAMBDA(m,n, m + groupSize*n))
where m represents each element of Sector.Sorted and factor the previous calculated values. As you can see the formula in Excel represents the generic formula (Formula 1 see above). The intermediate result will be:
Name
Sector Sorted
mapResult
factor
pos
A
1
SAME
0
1
B
1
SAME
1
5
C
1
SAME
2
9
E
2
NEW
0
2
F
2
SAME
1
6
G
2
SAME
2
10
H
3
NEW
0
3
J
3
SAME
1
7
D
4
NEW
0
4
I
4
SAME
1
8
The previous columns are shown just for illustrative purpose. Now we have the new positions, so we are ready to sort based on the new positions for Name via:
SORTBY(sName,pos)
Update
The first MAP can be removed creating an array as input for SCAN that has the information of sSector and the index position to be used for finding the previous element. SCAN only allows a single array as input argument, so we can combine both information in a new array. This is the formula can be used instead:
=LET(groupSize, 4, sorted, SORT(A2:B11,2), sName,
INDEX(sorted,,1),sSector, INDEX(sorted,,2),
factor, SCAN(-1,sSector&"-"&SEQUENCE(ROWS(sSector),,0),
LAMBDA(aa,b, LET(s, TEXTSPLIT(b,"-"),item, INDEX(s,,1),
idx, INDEX(s,,2), IF(aa=-1, 0, IF(1*item=INDEX(sSector, idx), aa+1,0))))),
pos,MAP(sSector, factor, LAMBDA(m,n, m + groupSize*n)),
SORTBY(sName,pos)
)
We use inside of SCAN a LET function to calculate all required elements for doing the comparison as part of the calculation of the corresponding LAMBDA function. We extract the item and the idx position used to find previous element of sSector via:
1*item=INDEX(sSector, idx)
we are able to compare each element of sSector with previous one, starting from the second element of sSector. We multiply item by 1, because TEXTSPLIT converts the result to text, otherwise the comparison will fail.
Problem
Isyana is given the number of visitors at her local theme park on N consecutive days. The number of visitors on the i-th day is Vi. A day is record breaking if it satisfies both of the following conditions:
The number of visitors on the day is strictly larger than the number of visitors on each of the previous days.
Either it is the last day, or the number of visitors on the day is strictly larger than the number of visitors on the following day.
Note that the very first day could be a record breaking day!
Please help Isyana find out the number of record breaking days.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Vi.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of record breaking days.
Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
0 ≤ Vi ≤ 2 × 105.
Test set 1
1 ≤ N ≤ 1000.
Test set 2
1 ≤ N ≤ 2 × 105 for at most 10 test cases.
For the remaining cases, 1 ≤ N ≤ 1000.
Sample
Input
Output
4
8
1 2 0 7 2 0 2 0
6
4 8 15 16 23 42
9
3 1 4 1 5 9 2 6 5
6
9 9 9 9 9 9
Case #1: 2
Case #2: 1
Case #3: 3
Case #4: 0
In Sample Case #1, the bold and underlined numbers in the following represent the record breaking days: 1 2 0 7 2 0 2 0.
In Sample Case #2, only the last day is a record breaking day.
In Sample Case #3, the first, the third, and the sixth days are record breaking days.
In Sample Case #4, there is no record breaking day.
-------------------------------------------------------------------------
This python code which i submitted in 'Kickstart Round D problem 1: Record Broker'. I executed this code on my local machine and there wasn't any run time error on top of that I couldn't brute force any test case that could break the code or give wrong answer. But while doing submission in kickstart it gives me runtime error. What could be the issue for getting run time error on kickstart? Please help!
cases = int(input().strip())
for q in range(1, cases + 1):
l = int(input().strip())
ls = list(map(int, input().split(" ")))
l = len(ls)
local_max = 0
count = 0
for i in range(l):
if i == 0:
if ls[1] < ls[0]:
local_max = ls[0]
count += 1
continue
if i == l - 1:
if ls[i] > ls[i - 1] and ls[i] > local_max:
count += 1
break
if ls[i] > ls[i - 1] and ls[i] > ls[i + 1] and ls[i] > local_max:
count += 1
local_max = ls[i]
continue
print("Case #{}: {}".format(q, count))
this line:
if ls[1] < ls[0]:
What if ls only has 1 element?
After fixing that your code still returns the wrong answer if there's only 1 element.
Also, consider this test case:
"65 87 87 34 64 59 93 20 95 85 24 99 62 100 60 19 100"
64 is not valid, but you count it.
Given a dataframe as follows:
id area total_price unit
0 1 185.00 14840 m
1 2 215.00 15050 m
2 3 233.23 46799 d
3 4 122.00 17000 d
4 5 540.00 70000 d
5 6 415.00 78000 d
6 7 170.00 12270 m
7 8 410.00 30750 m
8 9 196.00 13787 m
9 10 55.00 3100 m
I would like to create a new column unit_price with numerical value based on the following conditions:
a. if unit is m, then unit_price is calculated by total_price/area/30;
b. if unit is d, then unit_price is calculated by total_price/area
This code works:
m = (df['unit'] == 'm')
df['unit_price'] = np.where(m, df['total_price']/df['area']/30, df['total_price']/df['area'])
I have used also the code below but it raises an error: ValueError: Wrong number of items passed 256, placement implies 1
def unit_price(x):
if x['unit'] == 'm':
return x['total_price']/x['area']/30
if x['unit'] == 'd':
return x['total_price']/x['area']
df['unit_price'] = df.apply(unit_price, axis = 1)
Anyone know why I get this error, and how to fix it? Thanks.
With np.select you can do:
c1, c2 = df['unit']=='m', df['unit']=='d'
df['unit_price'] = np.select((c1,c2),
(df['total_price']/df['area']/30, df['total_price']/df['area']),
np.nan)
However, with this case, it's better to do a map:
units = {'m':30, 'd':1, 'y':365}
df['unit_price'] = df['total_price']/df['area'] / df['unit'].map(units)
Output:
id area total_price unit unit_price
0 1 185.00 14840 m 2.673874
1 2 215.00 15050 m 2.333333
2 3 233.23 46799 d 200.656005
3 4 122.00 17000 d 139.344262
4 5 540.00 70000 d 129.629630
5 6 415.00 78000 d 187.951807
6 7 170.00 12270 m 2.405882
7 8 410.00 30750 m 2.500000
8 9 196.00 13787 m 2.344728
9 10 55.00 3100 m 1.878788
Im trying to have a formula, that gets the first result of entry, for every line.
An Example Table would be like this:
Column A Column B Column C Excepted Output from Formula
3 99 P 18 P 4
4 88 P 144 P 1
2 77 P 2
2 77 P 2
1 88 P 1 P 1
1 99 P 4 P 4
2 44 P 5
3 22 P 7
1 88 P 99 P 1
Now, on Column D it should always show the first time it finds Coulmn A = 1, and Column B the same value as the own row (99 for the first row, 88 for the second, 77 for the 3rd...), and Display the Column C of it.
I tried it with the following Formula:
=CALCULATE(
FIRSTNONBLANK('Table'[Column C]; TRUE());
FILTER('Table';'Table'[Column A]=1);
FILTER('Table';'Table'[Column B]='Table'[Column B])
)
Which doesnt work. No errors, but it ignores the second filter.
If i now replace the "='Table'[Column B]" with a number that it should take (99,88,77...) it shows the correct result. But since its now a static number, it shows the same Result in every line, instead of calc it always new.
Can someone help?
Try this:
= CALCULATE(FIRSTNONBLANK('Table'[Column C], TRUE()),
FILTER(FILTER('Table','Table'[Column A]=1),'Table'[Column B] = earlier('Table'[Column B])))
Bacteria B replicates itself each 2 minutes, write a program that asks users to enter two numbers: the initial B bacteria number and a period of time (in minutes). Calculate and print out the total number of B bacteria after this period.
How to do. I did like below but the result is wrong. Because bateria replicates itself each 2 minutes then I multiply it with time.
b = int(input('How many B Bacterias are there? '))
t = int(input('How much time will we waits (minutes)? '))
r = b * 2 * t
print ('After',t,'minutes(s) we would have',r,'B Bacterias')
See the image here
You have b bacterias at the t time like this:
t b
0 b
1 b
2 b * 2
3 b * 2
4 b * 4
5 b * 4
6 b * 8
7 b * 8
8 b * 16
and so on.
So your formula is r = b * 2 ** math.floor(t/2) and the code will be:
import math
b = int(input('How many B Bacterias are there? '))
t = int(input('How much time will we waits (minutes)? '))
r = b * 2 ** math.floor(t/2)
print ('After',t,'minutes(s) we would have',r,'B Bacterias')
Your function to calculate the number of bacteria is wrong, this is a textbook example of an exponential function.
should be r = b * 2 ** (t/2)
You need to take the time t and divide it by the division time of 2 minutes to get the number of divisions that occurred.
By raising 2 to this number you get the total offspring of one bacterial cell and this multiplied by the initial number of bacteria is your answer