I try convert a float to int when the number don't have decimals.
I make this
from math import modf
def float_like_int(n):
if abs(modf(n)[0]) < 1e-6:
return int(n)
return n
print float_like_int(10.1)
print float_like_int(10.00001)
print float_like_int(10.000001)
print float_like_int(10.0)
exist a standard function or a more general way ? (without 1e-6)
I think the following is a bit more readable than your version:
def float_like_int(n):
if round(n, 6) == round(n):
return int(round(n))
return n
Note that this does have a slightly different meaning than your function, since it would also round something like 9.9999999 to 10.
Those approach that you are using trys to make a number integer if a fractional part is less than 1e-6 (0.000001 ). This means if you have a number 4.000001, (which is float actually) your function will throw away fractional part. You can change 1e-6 to another value and those numbers, which meet your criteria will be converted to int.
Here is my code without any extra modules imported. This code will not throw away any fractional part.
def Func(a):
if (a * 10) % 10 == 0:
return int(a)
else:
return a
f = 4.03
print Func(23.45)
print Func(f)
print Func(2.3)
Maybe something like this is more natural?
def float_like_int(n):
if int(n) == float(n):
return int(n)
else:
return float(n)
You can use this function :
def isWhole(x):
if(x%1 == 0):
return True
else:
return False
Related
I've tried to write a simple function, which input is binary number in string format and converts binary to decimal. But in the output I always get the wrong thing: the 'res' value in line 3, no matter what the input is ('1010', '10010111010', etc.). Also, I've tried to debug the code and the function doesn't even start the loop, as if it wasn't there... So, I just don't see my mistake
def bin_to_dec(bin):
bin = bin[::-1]
res = 0
for i in range(len(bin)):
if bin[i] == 0:
res += 2**i
return res
You are comparing the string "0" to the number 0 and they are, trivially, unequal.
So, contrary to what you say, the loop is actually looping; but the if statement will never be true.
Of course, also, you should probably add when the number is 1, not when it's 0.
def bin_to_dec(bin):
bin = bin[::-1]
res = 0
for i in range(len(bin)):
if int(bin[i]) == 1:
res += 2**i
return res
Notice the addition of int().
if bin[i] == '1'
This will correct the problem. bin[i] is a character and you are comparing it to a number which always results in false.
You can just use the built in int function:
def binaryToDecimal(n):
return int(n,2)
For example, if I want to detect all odd numbers in an array and set them to zero, I can use:
def setToZeroIfOdd(n):
if n % 2 == 0:
pass
else:
return 0
numbers = range(1,1000)
numbers = map(setToZeroIfOdd, numbers)
which works like a charm.
But when I try something like
def setToZeroIfDivisibleBy(n, divisor):
if n % divisor == 0:
return 0
else:
pass
numbers = map(setToZeroIfDivisibleBy(divisor=3), numbers)
it expects two arguments. Likewise,
numbers = map(setToZeroIfDivisibleBy, numbers, divisor=3)
does not work. How can I pass that divisor argument from within map()?
You can use functools.partial to make partial functions
from functools import partial
def setToZeroIfDivisibleBy(n, divisor):
if n % divisor == 0:
return 0
else:
pass
numbers = range(1,1000)
numbers = map(partial(setToZeroIfDivisibleBy, divisor=3), numbers)
Try using lambda function
numbers = map(lambda n: setToZeroIfDivisibleBy(n, divisor=3), numbers)
And rather than pass did you mean return n?
You make a function which returns a function:
def setToZeroIfDivisibleBy(divisor):
def callback(n):
if n % divisor == 0:
return 0
else:
pass
return callback
numbers = map(setToZeroIfDivisibleBy(3), numbers)
BTW, you can entirely omit empty branches like else: pass; it doesn't do anything. Since it results in a None, I don't think that's what you want either. You probably want return n there instead.
Another approach, instead of using partial, is to supply an infinite (or at least, long enough) sequence of 2nd arguments for the two-argument function:
from itertools import repeat
numbers = map(setToZeroIfDivisibleBy, numbers, repeat(3))
In Python 2, map will append None as necessary to the shorter of the two sequences to make them the same length. Assuming that will cause problems (either because your function cannot handle None as an input value or you end up with an infinite loop), you can either use itertools.imap, which stops after exhausting the shorter sequence:
from itertools import imap, repeat
numbers = list(imap(setToZeroIfDivisibleBy, numbers, repeat(3)))
or pass the length of numbers as a second argument to repeat so that the two sequences are the same length.
from itertools import repeat
numbers = map(setToZeroIfDivisibleBy, numbers, repeat(3, len(numbers)))
I'm trying to convert below code to recursive function but seems i'm quite confusing how could i write below in recursive function. could help me to give some thoughts?
Basically, what I'm generating below is the sum of the first n odd numbers.
def sum_odd_n(n):
total=0
j=2*n-1
i=1
if i>j:
return 1
else:
total =((j+1)/2)**2
i+=2
return total
> >>> sum_odd_n(5)
> 25.0
> >>> sum_odd_n(4)
> 16.0
> >>> sum_odd_n(1)
> 1.0
This smells somewhat like homework so I'm going to offer some advice instead of a solution.
Recursion is about expressing a problem in terms of itself.
Suppose you know the sum of the odd numbers from N to N - 2.
Can you write the total sum in terms of this sum and the function itself (or a related helper function)?
Recursive functions have at least one base case and at least one recursive call. Here are some hints:
def f(n):
# Base case - for which
# n do we already know the answer
# and can return it without
# more function calls? (Clearly,
# this must also terminate any
# recursive sequence.)
if n == ???:
return ???
# Otherwise, lets say we know the answer
# to f(n - 1) and assign it to
# the variable, 'rest'
rest = f(n - 1)
# What do we need to do with 'rest'
# to return the complete result
return rest + ???
Fill out the question marks and you'll have the answer.
Try:
def sum_of_odd(n):
if n>0:
x=(n*2)-1
return x+sum_of_odd(n-1)
else:
return 0
The answer of this:
sum_of_odd(5)
will be:
25
Try :
def sum_odd_n(n):
if n>0:
if n==1:
return 1
else:
return 2*n-1 + sum_odd_n(n-1)
def binary(n):
if n>0:
return binary(n//2)*10 + (n%2)
else:
return 0
The answer is 10000000 but it have to be "10000000". How can I put str on the answer?
In python, to change the type of anything, the only thing you have to do is wrap that in str(), int(), float()... or whatever type you want to change it to.
more details: http://usingpython.com/python-casting/
edit: sorry, didn't realize the recursive problem, still, I don't see why you can't just change the type after the function returns, seems the easiest thing to do.
if you insist though:
def binary(n):
if int(n)>0:
return str( int(binary(str(int(n)//2)))*10 + (int(n)%2))
else:
return str(0)
I will suggest that you use nested/inner functions as shown below to solve you problem. This allows you to compute the result using integers, and then convert the result to a string, or any other type, before returning it.
def binary(n):
def inner(m):
if m>0:
return inner(m//2)*10 + (m%2)
else:
return 0
return str(inner(n))
Alternatively you can cast n to the correct type whenever you use it. This is, in my opinion, a horrible to solve the problem, but it should still do the trick.
def binary(n):
if int(n) > 0:
return str(int(binary(n//2))*10 + n%2)
else:
return "0"
The problem with simply wrapping your return in a cast to str(), is that the subsequent calls to binary() will be affected, not just the top-level call. After the deepest recursive call to binary has finished, it will return its result - which is a string - to the next deepest recursive call to the expression:
binary(n // 2) * 10 + (n % 2)
But since the return value is a string, it fails when used in the expression.
The obvious solution is to simply cast the return value of binary() to a string from wherever it's being called. However, if for some reason you cannot do this, you can make a second function nested under binary, call this function inside of binary(), and cast its result to a string:
>>> def binary(n):
def _binary(n):
if n > 0:
return _binary(n // 2) * 10 + (n % 2)
else:
return 0
return str(_binary(n))
>>> binary(128)
'10000000'
>>> binary(64)
'1000000'
>>> binary(32)
'100000'
>>> binary(16)
'10000'
>>> binary(8)
'1000'
>>> binary(4)
'100'
>>> binary(2)
'10'
>>>
As #StefanPochmann pointed out, this method would also work:
def binary(n):
return binary(n // 2) + str(n % 2) if n > 1 else str(n)
This is a codewars challenge where you have to return the sum of the given arguments.
Instructions:
Calculate the sum of all the arguments passed to a function.
Note: If any of the arguments is not a finite number the function should return false/False instead of the sum of the arguments.
Here's my code:
def sum_all(*args):
sum = 0
for str(num) in args:
if not num.isdigit():
return False
else:
int(sum) += num
return sum
Currently I'm getting this error:
File "", line 9
SyntaxError: can't assign to function call
That error message is actually happening in two places:
Here:
for str(num) in args:
and here:
int(sum) += num
You cannot cast to string and int the way you are attempting to.
What you should do instead, is keep your iteration as:
for num in args:
Then, you can check if digit by doing this:
if not str(num).isdigit():
Finally, when you get to summing everything, simply cast num to int(num) to handle the case if you pass something like [1, 2, '3', 4] (not the 3 as a string):
sum += num
So, with that in mind, your code will look like this:
def sum_all(*args):
sum = 0
for num in args:
if not str(num).isdigit():
return False
else:
sum += int(num)
return sum
However, as you pointed out in your comment, there is a test case for negative numbers. This is where the above code breaks. Because, negative numbers as a string:
"-343"
Do not pass isdigit.
If you put this in your interpreter, it will return False:
"-343".isdigit()
So, with all this in mind, you can actually further simplify your code when you remove that to just have this:
def sum_all(*args):
try:
return sum(int(i) for i in args)
except:
return False
Demo:
print(sum_all(1,2,3,4,5))
Outputs:
15
First of all, don't overwrite existing functions such as sum. Use a different variable name (e.g. sum_). Second, your problem is on the for str(num) in args line. This needs to be for num in args:, with a modification of the following line for str(num).
def sum_all(*args):
sum_ = 0
for num in args:
if not str(num).isdigit():
return False
else:
sum_ += float(num)
return sum_
>>> sum_all('a', 2)
False
>>> sum_all(1, 2)
3.0
>>> sum_all(1, 2, '4')
7.0
Here is an alternative approach to coding the function that uses a generator comprehension to try and sum the arguments but returns False if it fails:
def sum_all2(*args):
try:
return sum(i for i in args)
except:
return False
>>> sum_all2(1, 2, '4.5')
False
>>> sum_all2(1, 2, '4') # I argue that '4' is a string, not a finite number.
False
>>> sum_all2(1, 2)
3
>>> sum_all2(1, 2, 3.5)
6.5
>>> sum_all2(1, 2, -3.5)
-0.5
You're overthinking this. There are two parts to this to note:
sum is a function that already takes in a collection and gives you its sum.
You want to reject the collection if none of them are numeric types.
Here's a start: this particular method will reject if none of the values are int. I leave expanding this out to float types as an exercise for the reader.
def sum_or_reject(li):
return sum(li) if all([isinstance(i, int) for i in li]) else False
The minimal change to make your logic run is:
def sum_all(*args):
sum = 0
for s in args:
num = str(s)
if not num.isdigit():
return False
else:
sum += int(num)
return sum
The line number in the question is misleading, and there are two issues: First, you need a variable for the iteration. Second, you need a variable for the += assignment. In both cases you try to apply a cast, bust the result of that would not longer be a variable and thus cannot be used as target for the assignment.
Likewise a minimal change to your code:
def sum_all(*args):
try:
return sum(int(i) for i in args)
except:
return False
print(sum_all(10,'-1',10))