Put each first column element the number of times that appear on second column - bash

I don't have much experience using Unix tools and I was wondering how to do this:
I have a file with 2 columns like this (space tab):
Agent 2
Person 3
Place 1
Location 4
Each different element of first column will be a number (Agent -> 1, Person -> 2, Place -> 3, Location -> 4).
Thus, I want to have each first column numeric element the number of times that appear on the second column. In this case:
1
1
2
2
2
3
4
4
4
4
Explanation: Agent (1) appears 2 times, Person (2) appears 3 times, etc.
Hope you can help me. Thanks in advance.

$ awk '{for (i=1; i<=$2; i++) print NR}' file
1
1
2
2
2
3
4
4
4
4

Related

Algorithm to distribute evenly products value into care packages

i'm currently solving a problem that states:
A company filed for bankruptcy and decided to pay the employees with the last remaining valuable items in the company only if it can be distributed evenly among them so that all of them have at least received 1 item and that the difference between the employee carrying the most valuable items and the employee carrying the least valuable items can not exceed a certain value x;
Input:
First row contains number of employee;
Second row contains the x value so that the the difference between the employee carrying the most valuable items and the employee carrying the least valuable items can not exceed;
Third row contains all the items with their value;
Output:
First number is the least valuable basket of items value and the second is the most valuable basket;
Example:
Input:
5
4
2 5 3 11 4 3 1 15 7 8 10
Output:
13 15
Input:
5
4
1 1 1 11 1 3 1 2 7 8
Output:
NO (It's impossible to distribute evenly)
Input:
5
10
1 1 1 1
Output:
NO (It's impossible to distribute evenly)
My solution to resolve this problem taking the first input is to, sort the items in ascending or descending order so from
2 5 3 11 4 3 1 15 7 8 10 --> 1 2 3 3 4 5 7 8 10 11 15
then create an adjacency list or just store it in simple variables where we add the biggest number to the lowest basket while iterating the item values array
Element 0: 15
Element 1: 11 <- 3 (sum 14)
Element 2: 10 <- 3 (sum 13)
Element 3: 8 <- 4 <- 1 (sum 13)
Element 4: 7 <- 5 <- 2 (sum 14)
So that my solution will have O(nlogN + 2n), first part using merge sort and then finding max e min value, what do you guys think about this solution?

Increase the numbers in apl

I have the following data:
a b c d
5 9 6 0
3 1 3 2
Characters in the first row, numbers in the second row.
How do I get the character corresponding to the highest number in the second row, and how do I increase the corresponding number in the second row? (For example, here, column b has the highest number, 9, so increase that number by 10%.)
I use Dyalog version 17.1.
With:
⎕←data←3 4⍴'a' 'b' 'c' 'd' 5 9 6 0 3 1 3 2
a b c d
5 9 6 0
3 1 3 2
You can extract the second row with:
2⌷data
5 9 6 0
Now grade it descending, that is, find the indices that would sort it from highest to lowest:
⍒2⌷data
2 3 1 4
The first number is the column we're looking for:
⊃⍒2⌷data
2
Now we can use this to extract the character from the first row:
data[⊂1,⊃⍒2⌷data]
b
But we only need the column index, not the actual character. The full index of the number we want to increase is:
2,⊃⍒2⌷data
2 2
Extracting the data to see that we got the right index:
data[⊂2,⊃⍒2⌷data]
9
Now we can either create a new array with the target value increased by 10%:
1.1×#(⊂2,⊃⍒2⌷data)⊢data
a b c d
5 9.9 6 0
3 1 3 2
Or change it in-place:
data[⊂2,⊃⍒2⌷data]×←1.1
data
a b c d
5 9.9 6 0
3 1 3 2
Try it online!

count the number of split points

I just got a question about counting the split points in a integer array, to ensure there is at least one duplicated integer on the two sides.
ex:
1 1 4 2 4 2 4 1
we can either split it into:
1 1 4 2 | 4 2 4 1
or
1 1 4 2 4 | 2 4 1
so that there is at least one '1', '2' ,and '4' are in both sides.
The integer can range from 1 to 100,000
The complexity requires O(n). How to solve this question?
Make one pass over the array and build count[i] = how many times the value i appears in the array. The problem is only solvable if count[i] >= 2 for all non-zero values. You can use this array to tell how many distinct values you have in your array.
Next, make another pass and using another array count2[i] (or you can reuse the first one), keep track of when you have visited each value at least once. Then use that position as your split point.
Example:
1 1 4 2 4 2 4 1
count = [3, 2, 0, 4] => 3 distinct values
1 1 4 2 4 2 4 1
^ => 1 distinct value so far
^ => 1 distinct value so far
^ => 2 distinct values so far
^ => 3 distinct values so far => this is your split point
There might be cases for which there is no solution, for example if the last 1 was at the beginning as well. To detect this, you can just make another pass over the rest of the array after you have decided on the split point and see if you still have all the values on that side.
You can avoid this last pass by using the count and count2 arrays to detect when you can no longer have a split point. This is left as an exercise.

Permutation Game - 2nd input case - explanation

Permutation Game (30 Points)
Alice and Bob play the following game:
1) They choose a permutation of the first N numbers to begin with.
2) They play alternately and Alice plays first.
3) In a turn, they can remove any one remaining number from the permutation.
4) The game ends when the remaining numbers form an increasing sequence. The person who played the last turn (after which the sequence becomes increasing) wins the game.
Assuming both play optimally, who wins the game?
Input:
The first line contains the number of test cases T. T test cases follow. Each case contains an integer N on the first line, followed by a permutation of the integers 1..N on the second line.
Output:
Output T lines, one for each test case, containing "Alice" if Alice wins the game and "Bob" otherwise.
Constraints:
1 <= T <= 100
2 <= N <= 15
The permutation will not be an increasing sequence initially.
Sample Input:
2
3
1 3 2
5
5 3 2 1 4
Sample Output:
Alice
Bob
Explanation: For the first example, Alice can remove the 3 or the 2 to make the sequence increasing and wins the game.
Can someone please help me out on the second input case: 5 3 2 1 4
The increasing sequences possible are:
1) 3 4 - Removing 5 , 2 , 1 in any sequence
2) 2 4 - Removing 5 , 3 , 1 in any sequence
3) 1 4 - Removing 5 , 3 , 2 in any sequence
So the output should be Alice?
Please do not share any code. Thanks
If Alice removes any of 5,3,2,1 then Bob removes 4. So, the increasing sequence can be of only one element, elements can be removed in any order. Hence, Bob wins.
If Alice removes 4, then also the increasing sequence has to be of one element. Bob wins.
So, Bob wins.
A possible case might be 4 or 5 is considered as increasing seq
As the input parameters are n>=2
But Alice would play optimally and remove 5 to win
NOTE: This isn't a programming problem and really doesn't belong on this site...
It sure looks like Alice should be the winner of the second test case.
Flow:
// Start state
5 3 2 1 4
// Alice remove 5
3 2 1 4
// Bob remove 3, 2, or 1
(2 1 4) or (3 1 4) or (3 2 4)
// Alice remove first number remaining
(1 4) or (2 4)
// Alice won!

Selection sort count number of swaps

http://www.cs.pitt.edu/~kirk/cs1501/animations/Sort1.html is this applet counting right? Selection sort for 5 4 3 2 1, I see 2 swaps, but the applet is counting 4 exchanges....
I guess it's a matter of definition. He is doing a swap in the end of every loop, even if he is swapping one element against itself. In his case, the swaps will be:
Original: 5 4 3 2 1
Swap pos 1 and 5: 1 4 3 2 5
Swap pos 2 and 4: 1 2 3 4 5
Swap pos 3 and 3: 1 2 3 4 5
Swap pos 4 and 4: 1 2 3 4 5
(No swap is done for the last element since that will always be in the correct place)
A simple if statement could be used to eliminate the two last swaps.

Resources