Couldn't understand the behaviour of initial parameter in np.min() function: - python-3.x

print('min using where parameter column wise with initial parameter as 10: ',
np.min([[9,13],[12,11]], where=[False, True], initial=10))
output:
min using where parameter column-wise with initial parameter as 10: 10
According to my understanding, the initial parameter is something that will be the output value in the output array when the minimum value in the array is greater than the 'initial' parameter value. (In other words, maximum value to printed as output)
But here, it is showing 10 as output even when we have 9 as the minimum value of the array.

Sorry, my bad guys.
The thing is I completely forgot about the where parameter, as I have
provided input to the where parameter = [False, True]. so the actual output with the where parameter is 11. So it is printing 10 as output.

Related

Groovy expression to filter strings randomly with specified probability

The input is a random string (not uniformly distributed).
I need a one-liner groovy expression that, based on the input, with specified probability, returns me true or false.
For an input of int value i would use intValue % 100 < p where p is desired probability percentage.
How do I do this for a string? I am thinking of using a hash function, e.g. md5() to make my input more uniform. But the output of string.md5() in groovy is still a string, so what can I do with it?
I thought of comparing last character, e.g. ['0','1'].contains(myString.md5()[31]) but it doesn't allow for decimal granularity and is quite cumbersome to update.
This works just right:
new BigInteger(myString.md5(), 16) % 100 < p

Inner workings of map() in a specific parsing situation

I know there are already at least two topics that explain how map() works but I can't seem to understand its workings in a specific case I encountered.
I was working on the following Python exercise:
Write a program that computes the net amount of a bank account based a
transaction log from console input. The transaction log format is
shown as following:
D 100
W 200
D means deposit while W means withdrawal. Suppose the following input
is supplied to the program:
D 300
D 300
W 200
D 100
Then, the output should be:
500
One of the answers offered for this exercise was the following:
total = 0
while True:
s = input().split()
if not s:
break
cm,num = map(str,s)
if cm=='D':
total+=int(num)
if cm=='W':
total-=int(num)
print(total)
Now, I understand that map applies a function (str) to an iterable (s), but what I'm failing to see is how the program identifies what is a number in the s string. I assume str converts each letter/number/etc in a string type, but then how does int(num) know what to pick as a whole number? In other words, how come this code doesn't produce some kind of TypeError or ValueError, because the way I see it, it would try and make an integer of (for example) "D 100"?
first
cm,num = map(str,s)
could be simplified as
cm,num = s
since s is already a list of strings made of 2 elements (if the input is correct). No need to convert strings that are already strings. s is just unpacked into 2 variables.
the way I see it, it would try and make an integer of (for example) "D 100"?
no it cannot, since num is the second parameter of the string.
if input is "D 100", then s is ['D','100'], then cm is 'D' and num is '100'
Then since num represents an integer int(num) is going to convert num to its integer value.
The above code is completely devoid of error checking (number of parameters, parameters "type") but with the correct parameters it works.
and map is completely useless in that particular example too.
The reason is the .split(), statement before in the s = input().split(). This creates a list of the values D and 100 (or ['D', '100']), because the default split character is a space ( ). Then the map function applies the str operation to both 'D' and '100'.
Now the map, function is not really required because both values upon input are automatically of the type str (strings).
The second question is how int(num) knows how to convert a string. This has to do with the second (implicit) argument base. Similar to how .split() has a default argument of the character to split on, so does num have a default argument to convert to.
The full code is similar to int(num, base=10). So as long as num has the values 0-9 and at most 1 ., int can convert it properly to the base 10. For more examples check out built in int.

How to divide string's length by 2 and then print sliced version based on result in one line

my_daily_thought = "I am the first half I am the second half"
print(len(my_daily_thought)//2)
How can i print "I am the second half" after finding my_daily_thought variable's divided length in one line?
Output should be "I am the second half" instead of 20.
#dreadnaught thank you for your answer.
You can treat strings in python as a list of characters and thus slice it as such:
my_daily_thought = "I am the first half I am the second half"
print(my_daily_thought[:len(my_daily_thought)//2])
print(my_daily_thought[len(my_daily_thought)//2:])
note that you can leave the 0 out in: my_daily_thought[0:10] to express that you slice from the start. Just like you can slice until the end as in the second slice
In your code you call the print() function on the statement: len(my_daily_thought)//2 but this statement evaluates to a number (as len() evaluates to the length of the array and dividing it by 2 also returns a number) and so that is why you print the number 20

Cannot return a float value of -1.00

I am currently doing an assignment for a computer science paper at university. I am in my first year.
in one of the questions, if the gender is incorrect the function is suppose to return a value of -1. But in the testing column, it says the expected value is -1.00. And I cannot seem to be able to return the value of '-1.00', it will always return a value of -1.0 (with one zero). I used the .format to make the value 2sf (so it will appear with two zero's) but when converting it to a float the value always returns "-1.0".
return float('{:.2f}'.format(-1))
This isn’t as clear as it could be. Does your instructor or testing
software expect a string '-1.00'? If so, just return that. Is a
float type expected? Then return -1.0; the number of digits shown does
not affect the value.
I don't know exactly what you have done, but i had tried this way and output what you expect.
b = -1
>>> print("%.2f" % (b))
-1.00
>>> print("%.2f" % (-1))
-1.00
What does the following code do?
print(float('{:.2f}'.format(-1)))
The '{:.2f}'.format(-1) creates some string representation of -1. defined by the format string. The float(...) converts this string to the float 1. The print command converts this float to a sting, using some default format, and prints this string to the screen. I think that isn't what you expected because the format you used does not effect the print command in formatting the string.
I assume you want
print('{:.2f}'.format(float(-1)))
and this actually does what you want, it prints
1.00
http://ideone.com/GyINQR
It is not necessary to convert -1 explicitely to float
print('{:.2f}'.format(-1))
gives the desired result:
http://ideone.com/U2RTMX

Difference between `print(9)` and `print(str(9))`

What is the difference between print(9) and print(str(9)) in Python when the output is the same for both functions?
print will always first try to call __str__ on the object you give it. In the first case the __str__ of the int instance 9 is '9'.
In the second case, you first explicitly call str on 9 (which calls its __str__ and yields '9'). Then, print calls '9''s __str__ which, if supplied with a string instance, returns it as it is resulting in '9' again.
So in both cases, in the end print will print out similar output.
From the documentation:
All non-keyword arguments are converted to strings like str() does
print(str(9))
print(9)
Output:
9
9
There is no change in output. But if we check the data type by executing,
print(type(str(9)))
print(type(9))
Then we get output as,
<class 'str'>
<class 'int'>
So, you can see, the types are different but the output is same.
In simple terms:
An integer is a variable that specifically holds a numerical value. Whereas a string is a variable that can hold a range of characters (including numbers).
print(9) says it should print the NUMERICAL value 9
print(str(9)) says it should print the character 9,
So if you were to do additions on both types for instance:
9 + 9 will always return 18
str(9) + str(9) will always return 99

Resources