Good evening. I have managed to bubble sort listOne. ListTwo will also need sorting. Is there a way to add listTwo into the bubble sort that I already have so that it gets sorted as well.
Or do I need to write another loop?
listOne = [3, 9, 2, 6, 1]
listTwo = [4, 8, 5, 7, 0]
def bubbleSort (inList):
moreSwaps = True
while (moreSwaps):
moreSwaps = False
for element in range(len(listOne)-1):
if listOne[element]> listOne[element+1]:
moreSwaps = True
temp = listOne[element]
listOne[element]=listOne[element+1]
listOne[element+1]= temp
return (inList)
print ("List One = ", listOne)
print ("List One Sorted = ", bubbleSort (listOne))
print ("List Two = ", listTwo)
print ("List Two Sorted = ", bubbleSort (listTwo))
I think you just need one method and then call the call it on the two list you can try this:
That is one method to do two jobs for you.
listOne = [3, 9, 2, 6, 1]
listTwo = [4, 8, 5, 7, 0]
def bubblesort(array):
for i in range(len(array)):
for j in range(len(array) - 1):
if array[j] > array[j + 1]:
swap = array[j]
array[j] = array[j + 1]
array[j + 1] = swap
print(array)
bubblesort(listOne)
bubblesort(listTwo)
[1, 2, 3, 6, 9]
[0, 4, 5, 7, 8]
Related
Develop a Python method change(amount) that for any integer amount in the range from 24 to 1000 returns a list consisting of numbers 5 and 7 only, such that their sum is equal to amount. For example, change(28) may return [7, 7, 7, 7], while change(49) may return [7, 7, 7, 7, 7, 7, 7] or [5, 5, 5, 5, 5, 5, 5, 7, 7] or [7, 5, 5, 5, 5, 5, 5, 5, 7].
To solve this quiz, implement the method change(amount) on your machine, test it on several inputs, and then paste your code in the field below and press the submit quiz button. Your submission should contain the change method only (in particular, make sure to remove all print statements).
Just started programming, quite proud of this. Here you go:
To use: print(change(amount))
def change(amount):
if amount < 24 or amount > 1000:
return 'error'
array = []
while True:
if (amount/5).is_integer():
for i in range(int(amount/5)):
array.append(5)
return array
array.append(7)
amount += -7
while amount > 0:
break
I am working on a school project with Octave for calculating and plotting velocity/acceleration graphs.
I have been trying to create a subplot function so that I won't have to hardcore it for every subplot as such
subplot(3, 1, 1);
plot(time, accn);
grid;
title('Acceleration vs Time')
xlabel('Time, (s)')
ylabel('Acceleration, (m/s^2)')
subplot(3, 1, 2);
plot(time, velocity);
grid;
title('Velocity vs Time');
xlabel('Time, (s)');
ylabel('Velocity, (m/s)');
Is it possible to create a function akin to this
subplot = subplotFunction(row, column, xaxis, yaxis, header, xaxisLabel,
yaxisLabel)
subplot(3, row, column);
plot(xaxis, yaxis);
grid;
title('header')
xlabel('xaxisLabel')
ylabel('yaxisLabel')
endfunction
And then call it like this?
subplot = subplotFunction(1, 1, time, accn, 'Acceleration vs Time', 'Time, (s)', 'Acceleration, (m/s^2)')
I am quite new to using functions so my apologies :(
1;
function subplotFunction(row, column, idx, xaxis, yaxis, header, xaxisLabel, yaxisLabel)
subplot (row, column, idx);
plot (xaxis, yaxis);
grid on;
title (header)
xlabel (xaxisLabel)
ylabel (yaxisLabel)
endfunction
subplotFunction (3, 1, 1, 1:10, 11:20, "foo", "bar", "baz")
subplotFunction (3, 1, 2, 1:10, 11:20, "huhu", "haha", "hoho")
x = linspace (0, 10, 100);
subplotFunction (3, 1, 3, x, sin(x), "world", "boo", "doo")
print ("out.png")
gives
I am new to Deep Learning, I think I've got the point of this Understanding NumPy's Convolve
.
I tried this in numpy
np.convolve([3, 4], [1, 1, 5, 5], 'valid')
the output is
array([ 7, 19, 35])
According to the link the second element of the output should be 23.
[3 4]
[1 1 5 5]
= 3 * 1 + 4 * 5 = 23
It seems that the second element (19) is wrong in my case, though I have no idea how and why. Any responses will be grateful.
I think you are confused with convolution implementation in neural networks, which is actually cross-corellation. However if you refer to mathematical definition of the convolution, you will see that the the second function has to be time-reversed (or mirrored). Also, note that numpy swaps araguments if the second element has bigger size (as in your case). So the result your get is obtained as following:
[1*4+3*1,1*4+3*5,5*4+3*5]
In case you want numpy to perform calculations as you did, you should use:
np.correlate([3, 4], [1, 1, 5, 5], 'valid')
Here is useful illustration for convolution and cross-correlation:
the reason is that numpy reverse the shorter array, here [3, 4] becomes [4,3]. It is done because of the definition of the convolution (you can find more informations in the section definition of wikipedia here https://en.wikipedia.org/wiki/Convolution).
So in fact : np.convolve([3, 4], [1, 1, 5, 5], 'valid')
makes :
[4 3]
[1 1 5 5]
= 4 * 1 + 3 * 5 = 19
:)
I am trying to show a graph which I am trying to plot in Matplotlib and then showing it with some hard coding in HTML template.But out of 3 attempts, it is working only once else it is throwing TCL Out of stack space error. Below is my coding.
def similar_images(request):
n_groups = 5
means_men = (20, 35, 30, 35, 27)
std_men = (2, 3, 4, 1, 2)
means_women = (25, 32, 34, 20, 25)
std_women = (3, 5, 2, 3, 3)
fig, ax = plt.subplots() # somwhere here it is throwing this error
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.4
error_config = {'ecolor': '0.3'}
rects1 = plt.bar(index, means_men, bar_width,
alpha=opacity,
color='b',
yerr=std_men,
error_kw=error_config,
label='Men')
rects2 = plt.bar(index + bar_width, means_women, bar_width,
alpha=opacity,
color='r',
yerr=std_women,
error_kw=error_config,
label='Women')
plt.xlabel('Shoe')
plt.ylabel('Week')
plt.title('Last Year sale details')
#plt.xticks(index + bar_width / 2, ('A', 'B', 'C', 'D', 'E'))
plt.legend()
plt.savefig('/graph.png')
plt.close()
return render(request,'sales/Details.html')
Please, can someone help me here?
Below is the error.
Exception Type: TclError at /sales/similar_images/
Exception Value: out of stack space (infinite loop?)
I have some data files with content
a1 b1 c1 d1
a1 b2 c2 d2
...
[blank line]
a2 b1 c1 d1
a2 b2 c2 d2
...
I plot this with gnuplot using
splot 'file' u 1:2:3:4 w pm3d.
Now, I want to use a binary file. I created the file with Fortran using unformatted stream-access (direct or sequential access did not work directly). By using gnuplot with
splot 'file' binary format='%float%float%float%float' u 1:2:3
I get a normal 3D-plot. However, the pm3d-command does not work as I don't have the blank lines in the binary file. I get the error message:
>splot 'file' binary format='%float%float%float%float' u 1:2:3:4 w pm3d
Warning: Single isoline (scan) is not enough for a pm3d plot.
Hint: Missing blank lines in the data file? See 'help pm3d' and FAQ.
According to the demo script in http://gnuplot.sourceforge.net/demo/image2.html, I have to specify the record length (which I still don't understand right). However, using this script from the demo page and the command with pm3d obtains the same error message:
splot 'scatter2.bin' binary record=30:30:29:26 u 1:2:3 w pm3d
So how is it possible to plot this four dimensional data from a binary file correctly?
Edit: Thanks, mgilson. Now it works fine. Just for the record: My fortran code-snippet:
open(unit=83,file=fname,action='write',status='replace',access='stream',form='unformatted')
a= 0.d0
b= 0.d0
do i=1,200
do j=1,100
write(83)real(a),real(b),c(i,j),d(i,j)
b = b + db
end do
a = a + da
b = 0.d0
end do
close(83)
The gnuplot commands:
set pm3d map
set contour
set cntrparam levels 20
set cntrparam bspline
unset clabel
splot 'fname' binary record=(100,-1) format='%float' u 1:2:3:4 t 'd as pm3d-projection, c as contour'
Great question, and thanks for posting it. This is a corner of gnuplot I hadn't spent much time with before. First, I need to generate a little test data -- I used python, but you could use fortran just as easily:
Note that my input array (b) is just a 10x10 array. The first two "columns" in the datafile are just the index (i,j), but you could use anything.
>>> import numpy as np
>>> a = np.arange(10)
>>> b = a[None,:]+a[:,None]
>>> b
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
[ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
[ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
[ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
[ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
[ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]])
>>> with open('foo.dat','wb') as foo:
... for (i,j),dat in np.ndenumerate(b):
... s = struct.pack('4f',i,j,dat,dat)
... foo.write(s)
...
So here I just write 4-floating point values to the file for each data-point. Again, this is what you've already done using fortran. Now for plotting it:
splot 'foo.dat' binary record=(10,-1) format='%float' u 1:2:3:4 w pm3d
I believe that this specifies that each "scan" is a "record". Since I know that each scan will be 10 floats long, that becomes the first index in the record list. The -1 indicates that gnuplot should keep reading records until it finds the end of the file.