arr=[1,2,3,4,5]
n=len(arr)
temp = n*[None]
flag = True
flag= bool(1-flag)
I'm new to python, so not sure what it really means.
I want to know what all three lines of code do. Thank you
The first line will create an array of five elements
print (arr)
[1, 2, 3, 4, 5]
The second will make a variable named 'n' that will contain the number of elements in your array
print(n)
5
The third line will create an array with a length of 5 that will only contain None.
None is used to define a variable so that it resets to a variable with no value. It is not similar to a NULL or an empty string, None is an object.
print(temp)
[None, None, None, None, None]
The last line will change your flag value to false.
In standard binary conventions, True is equal to 1 and False is equal to 0. By subtracting a 1 with the flag value that is True, you are doing 1-1 which is equal to Zero. With bool(), you obtain a false.
print(flag)
False
Related
I have an array with 2 columns. I want to compare the values of the first column, like this:
a[i+1]-a[i]<0.0025.
If this is true I need to delete the row with a[i+1].
This is my first attempt, but it doesnt work.
a = np.delete(a, np.diff(a[:,0])<0.0025, 0)
I get the following error:
ValueError: boolean array argument obj to delete must be one dimensional and match the axis length of 8628
8628 is the length of the array.
Another code i´ve tried is:
a = a[~(np.diff(a[:,0]))<0.0025]
But then I get this error:
TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
Can somebody help me with this?
You're on the right track. np.diff(a[:, 0]) < 0.0025 creates an array of length 1 less than 8628, which means when you use it in np.delete that the dimension no longer matches with the original array.
I would go with your second attempt. Using < 0.0025 results in a mask of items that you want to delete, which you need to invert using ~ to get a mask of results you would like to keep. You have to make sure to place your parentheses correctly: ~( np.diff(a[:, 0]) < 0.0025 ). Instead, you can also use >= 0.0025 to make a mask of items you would like to keep.
Lastly, you have to make sure to match the dimensions (given that np.diff results in one less element. You can do this by prepending True to signify that you always want to keep the first value. One way to do that is using np.r_.
Final code:
import numpy as np
a = np.random.rand(8628, 2) # Your array here
result = a[ np.r_[True, np.diff(a[:, 0]) >= 0.0025] ]
Detailed example:
Consider the array: [ 1, 3, 2, 5, 3]
np.diff creates: [ 2, -1, 3, -2]
Using threshold creates: [True, False, True, False]
Note that when the next element in the original array is less than the previous,
the thresholding result in False too.
Finally, because there are now 4 values instead of 5, we prepend True.
This has the effect of always including the first element in the result:
Original: [ 1, 3, 2, 5, 3]
Mask [True, True, False, True, False]
^^^^ ~~~~ ~~~~~ ~~~~ ~~~~~
Then using boolean indexing, we get the elements where
the mask contains True to obtain the final result:
[1, 3, 5]
Working on a project to recreate a game Mastermind. I need to compare two arrays, and running into some struggles.
I need to output two integers for the flow of the game to work,
the first integer is the number of correct choices where the index matches. The code I have for this appears to be working
pairs = #code.zip(guess)
correct_position_count = pairs.select { |pair| pair[0] == pair[1] }.count
Where pairs is equal to a 4 element array and the guess is also a 4 element array
The second part I am having a bit of trouble with on how to do the comparison and return an array. The integer should represent where the two arrays index don't match (the above code block but !=) and confirm whether the guess array excluding any exact index matches has any elements included with the code array once again excluding the exact index matches.
Any help would be greatly appreciated!
I am not completely sure to understand your problem but if I understood well, you've two arrays, solution with the solution and guess with the current guess of the player.
Now, let's assume that the solution is 1234 and that the guess is 3335.
solution = [1, 2, 3, 4]
guess = [3, 3, 3, 5]
an element by element comparison produces an array of booleans.
diff = guess.map.with_index { |x,i| x == solution[i] }
# = [false, false, true, false]
Now, you can easily compute the number of good digits diff.count true and the number of wrong digits diff.count false. And, in case you need the index of the false and/or true values you can do
diff.each_index.select { |i| diff[i] } # indexes with true
# = [2]
diff.each_index.select { |i| !diff[i] } # indexes with false
# = [0, 1, 3]
You can count all digit matches ignoring their positions and then subtract exact matches.
pairs = #code.zip(guess)
correct_position_count = pairs.select { |pair| pair[0] == pair[1]}.count
any_position_count = 0
code_digits = #code.clone # protect #code from modifying
guess.each do |digit|
if code_digits.include?(digit)
code_digits.delete_at(code_digits.find_index(digit)) # delete the found digit not to count it more than once
any_position_count += 1
end
end
inexact_position_count = any_position_count - correct_position_count
puts "The first value: #{correct_position_count}"
puts "The second value: #{inexact_position_count}"
I'm not sure where I'm going wrong with this. I have an array and the user is prompted with a question about what number should be deleted from the array. The number is stored and the result is a new array that gets outputted with the deleted value.
def delete(number)
a = [1, 2, 1, 3, 1, 4, 2, 5]
puts "Please type number to be deleted?"
number = gets
result= a.delete(number)
puts result
end
a.delete(number)
maybe do something like this ?
def delete(num,array)
array.reject { |el| el == num }
puts array
end
a = [1, 2, 1, 3, 1, 4, 2, 5]
puts "Please type number to be deleted? from array #{a}"
number = gets
delete(number.to_i,a)
Why Your Code Doesn't Work
In general, Kernel#gets returns a String with a trailing newline. Therefore, Array#delete has no matching elements.
A Working Alternative
You need to convert your input to an Integer for the #gets method. It's also useful to return a value from your method. The following works, and also serves to validate the user's input:
require 'pp'
def delete_from_array array
print 'Number to be deleted: '
array.delete Integer(gets)
pp array
end
array = [1, 2, 1, 3, 1, 4, 2, 5]
delete_from_array array.dup
Note that #delete will modify the original array, rather than returning a copy. In most cases, you will probably want to use #dup to ensure that you aren't modifying your original array. On the other hand, if you want the side-effects, then just elide the call to #dup when passing the array as an argument.
I found this example of using #any? on a hash a bit tricky:
"With a Hash, you can use these in two ways. Either with one argument that is a 2 element array of the key-value pair. candidate[0] is the key and candidate[1] is its value.
{:locke => 4, :hugo => 8}.any? { |candidate| candidate[1] > 4 }
This returns true because the value of the second candidate :hugo is greater than 4."
Could anyone point me someplace that explains what happened here? I couldn't find a relevant question on SO. Thanks a lot in advance.
If you print candidate it will become easy to understand:
{:locke => 4, :hugo => 8}.any? { |candidate| puts candidate.to_s }
# [:locke, 4]
# [:hugo, 8]
The any? method is treating each key-value pair of the hash as an a two-element array, which means the hash will be treated as an array of arrays.
The block passed to any? (i.e., { |candidate| candidate[1] > 4 }) returns true if any of the the second elements (i.e., 4 and 8) is ever > 4 and false otherwise. 8 > 4, so the result is true.
From the official docs, the any? method:
Passes each element of the collection to the given block. The method
returns true if the block ever returns a value other than false or
nil. If the block is not given, Ruby adds an implicit block of { |obj|
obj } that will cause any? to return true if at least one of the
collection members is not false or nil.
What Hash#any? does is yield arrays of two elements to a given block and return true if the evaluation of the block returned something truthy (not false or nil) and false otherwise.
As for why you get two values if you pass a block with two arguments - this is called unpacking.
def foo
yield [42, 6 * 9]
end
# only one argument, array is not unpacked
foo { |numbers| p numbers } # prints [42, 54]
# two arguments, the array is being unpacked
foo { |x, y| p x, y } # prints 42 \n 54
The following are identical:
First method:
puts [1,2,3,4,5,6,6,6,6].select {|number| number == 6 }
Output:
6
6
6
6
Second method:
array = [1,2,3,4,5,6,6,6,6]
array.select do |number|
puts number == 6
end
Output:
false
false
false
false
false
true
true
true
true
Why don't I get the same result for each? How do I go about getting the same result? Please explain what is going on.
Described in the ruby doc,
select → an_enumerator
Returns an array containing all elements of enum for which the given
block returns a true value.
[1,2,3,4,5,6,6,6,6].select {|number| number == 6 }
returns as intended (i.e, return an array where the number == 6)
[6, 6, 6, 6]
In the second method, you're simply outputting the result of the comparison, which will result in a boolean.
Regarding the output, the first method puts the selected array whereas the second method puts each evaluation of the condition (regardless of whether the condition is met).
Regarding the selected array, the block in the first method returns the evaluation of number == 6, which leads to the selected array [6, 6, 6, 6], whereas the block in the second method returns the evaluation of puts, which is nil, which leads to the selected array [].
Regarding the return value of the entire code, the first method returns the evaluation of puts, which is nil, whereas the second method returns the selected array [].