Creating a nested dictionary comprehension for year and month in python - dictionary-comprehension

I would like to create a nested dictionary with dict comprehension but I am getting syntax error.
years = [2016, 2017, 2018, 2019]
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
my_Dict = {i:{j: for j in months}, for i in years}
I am not sure how to declare this nested dict comprehension without getting a syntax error.

In this case, the correct way would be to use a dictionary with a nested list comprehension because if you use a nested dictionary, it will replace old values. The correct syntax, in this case, would be this one:
years = [2016, 2017, 2018, 2019]
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
my_Dict = {
year: [month for month in months] for year in years
}
print(my_Dict)
>>> {2016: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
2017: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
2018: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
2019: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}

Made some slight changes to the code above and this works
key_years = {2016, 2017, 2018, 2019}
key_months = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
myDict = {i:{j for j in key_months} for i in key_years}
print (myDict)
Output: {2016: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 2017: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 2018: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 2019: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}}

Related

split an array which comtains partially relatively order into two sorted array in O(n) time

Assume I have two arrays, both of them are sorted, for example:
A: [1, 4, 5, 8, 10, 24]
B: [3, 6, 9, 29, 50, 65]
And then I merge these two array into one array and keep original relative order of both two array
C: [1, 4, 3, 5, 6, 9, 8, 29, 10, 24, 50, 65]
Is there any way to split C into two sorted array in O(n) time?
note: not necessarily into the original A and B
Greedily assign your integers to list 1 if they can go there. If they can't, assign them to list 2.
Here's some Ruby code to play around with this idea. It randomly splits the integers from 0 to n-1 into two sorted lists, then randomly merges them, then applies the greedy approach.
def f(n)
split1 = []
split2 = []
0.upto(n-1) do |i|
if rand < 0.5
split1.append(i)
else
split2.append(i)
end
end
puts "input 1: #{split1.to_s}"
puts "input 2: #{split2.to_s}"
merged = []
split1.reverse!
split2.reverse!
while split1.length > 0 && split2.length > 0
if rand < 0.5
merged.append(split1.pop)
else
merged.append(split2.pop)
end
end
merged += split1.reverse
merged += split2.reverse
puts "merged: #{merged.to_s}"
merged.reverse!
greedy1 = [merged.pop]
greedy2 = []
while merged.length > 0
if merged[-1] >= greedy1[-1]
greedy1.append(merged.pop)
else
greedy2.append(merged.pop)
end
end
puts "greedy1: #{greedy1.to_s}"
puts "greedy2: #{greedy2.to_s}"
end
Here's sample output:
> f(20)
input 1: [2, 3, 4, 5, 8, 9, 10, 18, 19]
input 2: [0, 1, 6, 7, 11, 12, 13, 14, 15, 16, 17]
merged: [2, 0, 1, 6, 3, 4, 5, 8, 9, 7, 10, 11, 18, 12, 13, 19, 14, 15, 16, 17]
greedy1: [2, 6, 8, 9, 10, 11, 18, 19]
greedy2: [0, 1, 3, 4, 5, 7, 12, 13, 14, 15, 16, 17]
> f(20)
input 1: [1, 3, 5, 6, 8, 9, 10, 11, 13, 15]
input 2: [0, 2, 4, 7, 12, 14, 16, 17, 18, 19]
merged: [0, 2, 4, 7, 12, 14, 16, 1, 3, 5, 6, 8, 17, 9, 18, 10, 19, 11, 13, 15]
greedy1: [0, 2, 4, 7, 12, 14, 16, 17, 18, 19]
greedy2: [1, 3, 5, 6, 8, 9, 10, 11, 13, 15]
> f(20)
input 1: [0, 1, 2, 6, 7, 9, 11, 14, 15, 18]
input 2: [3, 4, 5, 8, 10, 12, 13, 16, 17, 19]
merged: [3, 4, 5, 8, 10, 12, 0, 13, 16, 17, 1, 19, 2, 6, 7, 9, 11, 14, 15, 18]
greedy1: [3, 4, 5, 8, 10, 12, 13, 16, 17, 19]
greedy2: [0, 1, 2, 6, 7, 9, 11, 14, 15, 18]
Let's take your example.
[1, 4, 3, 5, 6, 9, 8, 29, 10, 24, 50, 65]
In time O(n) you can work out the minimum of the tail.
[1, 3, 3, 5, 6, 8, 8, 10, 10, 24, 50, 65]
And now the one stream is all cases where it is the minimum, and the other is the cases where it isn't.
[1, 3, 5, 6, 8, 10, 24, 50, 65]
[ 4, 9, 29, ]
This is all doable in time O(n).
We can go further and now split into 3 streams based on which values in the first stream could have gone in the last without changing it being increasing.
[ 3, 5, 6, 8, 10, 24, ]
[1, 5, 6, 8, 50, 65]
[ 4, 9, 29, ]
And now we can start enumerating the 2^6 = 64 different ways of splitting the original stream back into 2 increasing streams.

How to duplicate value of array in ruby

I have two arrays of integers, e.g.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [7, 8, 9]
I would like to repeatedly duplicate the value of 'b' to get a perfectly matching array lengths like this:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [7, 8, 9, 7, 8, 9, 7, 8, 9, 7]
We can assume that a.length > b.length
Assuming you mean
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [7, 8, 9]
then you can do:
b.cycle.take(a.length) #=> [7, 8, 9, 7, 8, 9, 7, 8, 9, 7]
<script src="//repl.it/embed/JJ3x/2.js"></script>
See Array#cycle and Enumerable#take for more details.
I would have used Array#cycle had it been available, but since it was taken I thought I'd suggest some alternatives (the first being my fav).
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = [7, 8, 9]
[*b*(a.size/b.size), *b[0, a.size % b.size]]
#=> [7, 8, 9, 7, 8, 9, 7, 8, 9, 7]
Array.new(a.size) { |i| b[i % b.size] }
#=> [7, 8, 9, 7, 8, 9, 7, 8, 9, 7]
b.values_at(*(0..a.size-1).map { |i| i % b.size })
#=> [7, 8, 9, 7, 8, 9, 7, 8, 9, 7]

python appending issues, function keeps changing values of list

I was trying to visualize bubblesort by making an animated plot on some unsorted list, say np.random.permutation(10)
so naturally I would append the list every time it's altered within the bubblesort function until it's completely sorted. Here's the code
def bubblesort(A):
instant = []
for i in range(len(A)-1):
lindex=0
while lindex+1<len(A):
if A[lindex]> A[lindex+1]:
swap(A,lindex,lindex+1)
lindex+=1
else:
lindex+=1
instant.append(A)
return instant
The problem is though, instant only returns
[array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]
which is obviously not right. What has gone wrong? Thanks!
A is being operated on in-place, and bubblesort is returning a list of references to this array. Notice that if you check A now, it is also sorted.
Changing
if A[lindex]> A[lindex+1]:
swap(A,lindex,lindex+1)
to
if A[lindex]> A[lindex+1]:
A = A.copy()
swap(A,lindex,lindex+1)
making a copy before changing anything, should show the progress of the sort.

Performance analysis of openmp code (parallel) vs serial code

How do I compare the performance of parallel code(using OpenMP) vs serial code? I am using the following method
int arr[1000] = {1, 6, 1, 3, 1, 9, 7, 3, 2, 0, 5, 0, 8, 9, 8, 4, 4, 4, 0, 9, 6, 5, 9, 5, 9, 2, 5, 8, 6, 1, 0, 7, 7, 3, 2, 8, 3, 2, 3, 7, 2, 0, 7, 2, 9, 5, 8, 6, 2, 8, 5, 8, 5, 6, 3, 5, 8, 1, 3, 7, 2, 6, 6, 2, 1, 9, 0, 6, 1, 6, 3, 5, 6, 3, 0, 8, 0, 8, 4, 2, 7, 1, 0, 2, 7, 6, 9, 7, 7, 5, 4, 9, 3, 1, 1, 4, 2, 4, 1, 5, 2, 6, 0, 8, 9, 2, 6, 0, 1, 0, 2, 0, 3, 3, 4, 0, 1, 4, 8, 8, 1, 4, 9, 4, 7,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, 8, 2, 9, 3, 5, 9, 2, 8, 4, 2, 0, 2, 5, 3, 2, 2, 6, 7, 9, 3, 0, 6, 7, 1, 5, 1, 0, 2, 2, 9, 0, 2, 1, 2, 7, 7, 3, 0, 7, 9, 4, 8, 1, 9, 3, 4, 1, 1, 3, 2, 6, 3, 9, 3, 6, 6, 7, 6, 1, 1, 6, 1, 3, 9, 3, 2, 6, 8, 2, 6, 7, 6, 4, 1, 5, 9, 5, 9, 2, 0, 3, 8, 5, 2, 4, 2, 9, 3, 8, 0, 6, 6, 3, 1, 6, 9, 3, 2, 7, 6, 0, 7, 2, 6, 8, 0, 5, 5, 9, 9, 5, 4, 8, 0, 7, 4, 2, 8, 9, 3, 0, 5, 9, 3, 6, 5, 4, 9, 0, 2, 7, 2, 9, 0, 9, 9, 2, 6, 4, 3, 6, 9, 7, 6, 1, 6, 0, 6, 4, 9, 9, 6, 6, 0, 2, 2, 6, 6, 3, 8, 8, 1, 0, 9, 3, 9, 8, 5, 6, 4, 8, 4, 3, 5, 0, 7, 2, 2, 3, 8, 3, 2, 5, 9, 2, 7, 1, 0, 5, 6, 0, 4};
clock_t begin, end;
double time_spent;
begin = clock();
/* here, do your time-consuming job */
#pragma omp parallel for private(temp)
for(j=0;j<1000;j++){
temp = arr[j];
for(i=0;i<temp;temp--)
result[j]=result[j]*temp;
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n\n%f",time_spent);
But every time I run the code I get a different output. I want to see how the performance of the code differs for openmp and serial code. What method I should use to achieve the same?
The time the code takes to run will change a little bit due to computer/server usage; however, if you run both the parallel and serial versions you should see a difference in the amount of run time between the two. Also, the size of your parallel operation is pretty small. But you should see and improvement.
int arr[1000] = {1, 6, 1, 3, 1, 9, 7, 3, 2, 0, 5, 0, 8, 9, 8, 4, 4, 4, 0, 9, 6, 5, 9, 5, 9, 2, 5, 8, 6, 1, 0, 7, 7, 3, 2, 8, 3, 2, 3, 7, 2, 0, 7, 2, 9, 5, 8, 6, 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 6, 3, 4, 4, 0, 8, 1, 8, 2, 3, 1, 4, 3, 2, 9, 1, 0, 4, 8, 9, 4, 9, 9, 3, 2, 7, 1, 9, 0, 1, 4, 8, 4, 9, 2, 7, 9, 6, 5, 1, 1, 6, 8, 4, 0, 9, 7, 2, 3, 5, 1, 9, 7, 3, 5, 9, 0, 6, 1, 2, 8, 5, 1, 4, 6, 5, 1, 5, 3, 8, 9, 4, 7, 7, 0, 9, 6, 8, 2, 9, 3, 5, 9, 2, 8, 4, 2, 0, 2, 5, 3, 2, 2, 6, 7, 9, 3, 0, 6, 7, 1, 5, 1, 0, 2, 2, 9, 0, 2, 1, 2, 7, 7, 3, 0, 7, 9, 4, 8, 1, 9, 3, 4, 1, 1, 3, 2, 6, 3, 9, 3, 6, 6, 7, 6, 1, 1, 6, 1, 3, 9, 3, 2, 6, 8, 2, 6, 7, 6, 4, 1, 5, 9, 5, 9, 2, 0, 3, 8, 5, 2, 4, 2, 9, 3, 8, 0, 6, 6, 3, 1, 6, 9, 3, 2, 7, 6, 0, 7, 2, 6, 8, 0, 5, 5, 9, 9, 5, 4, 8, 0, 7, 4, 2, 8, 9, 3, 0, 5, 9, 3, 6, 5, 4, 9, 0, 2, 7, 2, 9, 0, 9, 9, 2, 6, 4, 3, 6, 9, 7, 6, 1, 6, 0, 6, 4, 9, 9, 6, 6, 0, 2, 2, 6, 6, 3, 8, 8, 1, 0, 9, 3, 9, 8, 5, 6, 4, 8, 4, 3, 5, 0, 7, 2, 2, 3, 8, 3, 2, 5, 9, 2, 7, 1, 0, 5, 6, 0, 4};
clock_t begin, end;
double time_spent_omp;
double time_spent;
begin = omp_get_wtime();
/* here, do your time-consuming job */
#pragma omp parallel for private(temp)
for(j=0;j<1000;j++){
temp = arr[j];
for(i=0;i<temp;temp--)
result[j]=result[j]*temp;
}
end = omp_get_wtime();
time_spent_omp = (double)(end - begin) / CLOCKS_PER_SEC;
begin = omp_get_wtime();
/* here, do your time-consuming job */
for(j=0;j<1000;j++){
temp = arr[j];
for(i=0;i<temp;temp--)
result[j]=result[j]*temp;
}
end = omp_get_wtime();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\n\n Time to process: %f --- Time to process with OPENMP %f",time_spent, time_spent_omp);
This should give you a better idea about how it is working.

Reduce array to set number of items? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a Ruby array of 300 items. I want to reduce that array down to a set number of items, evenly picked from the array.
The number of items in the array will not be the same every time, nor will the number of items needed.
Something like this:
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
num_of_items = 4
final_arr = [0, 5, 10, 15]
You can use Enumerable#each_slice
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
num_of_items = 4
#=> 4
arr.each_slice(arr.size/num_of_items + 1).map(&:first)
#=> [0, 5, 10, 15]
arr = (0..16).to_a
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
num_of_items = 5
#=> 5
arr.each_slice(arr.size/num_of_items + 1).map(&:first)
#=> [0, 4, 8, 12, 16]
OR
Numeric#step
arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
num_of_items = 4
#=> 4
arr.first.step(arr.size, arr.size/num_of_items + 1).map { |i| arr[i] }
#=> [0, 5, 10, 15]
arr = (0..16).to_a
#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
num_of_items = 5
#=> 5
arr.first.step(arr.size, arr.size/num_of_items + 1).map { |i| arr[i] }
#=> [0, 4, 8, 12, 16]
If you're picking a given number of items at random from this array, use sample -
$ arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
$ arr.sample(5)
=> [1, 3, 5, 4, 12]
$ arr.sample(5)
=> [15, 6, 13, 5, 11]
If the goal of your algorithm is exactly as shown above, you could use in_groups_of, then grab the first element of each child array:
1.9.3p484 :014 > num_of_items = 5
=> 5
1.9.3p484 :011 > arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
1.9.3p484 :012 > arr.in_groups_of(num_of_items)
=> [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, nil, nil, nil, nil]]
1.9.3p484 :013 > arr.in_groups_of(num_of_items).map(&:first)
=> [0, 5, 10, 15]

Resources